92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Tour Genius Controllers
|
|
"""
|
|
|
|
from odoo import http
|
|
from odoo.http import request
|
|
import base64
|
|
|
|
|
|
class TourGeniusCertificateController(http.Controller):
|
|
"""Controller for certificate operations"""
|
|
|
|
@http.route('/tour_genius/certificate/download/<int:attempt_id>', type='http', auth='user')
|
|
def download_certificate(self, attempt_id, **kwargs):
|
|
"""
|
|
Download certificate PDF for a passed quiz attempt.
|
|
Always regenerates PDF to ensure latest design is used.
|
|
"""
|
|
attempt = request.env['genius.quiz.attempt'].browse(attempt_id)
|
|
|
|
# Security check: user can only download their own certificates
|
|
if not attempt.exists() or attempt.user_id.id != request.env.user.id:
|
|
return request.not_found()
|
|
|
|
# Check if passed
|
|
if not attempt.is_passed:
|
|
return request.not_found()
|
|
|
|
# Delete any existing attachment to force regeneration with new design
|
|
old_attachments = request.env['ir.attachment'].search([
|
|
('res_model', '=', 'genius.quiz.attempt'),
|
|
('res_id', '=', attempt.id),
|
|
('mimetype', '=', 'application/pdf'),
|
|
])
|
|
if old_attachments:
|
|
old_attachments.unlink()
|
|
|
|
# Generate new certificate with latest design
|
|
attachment_id = attempt.generate_certificate_pdf()
|
|
if not attachment_id:
|
|
return request.not_found()
|
|
|
|
attachment = request.env['ir.attachment'].browse(attachment_id)
|
|
|
|
# Return PDF file
|
|
filename = f'Certificate_{attempt.quiz_id.name}_{attempt.user_id.name}.pdf'
|
|
return request.make_response(
|
|
base64.b64decode(attachment.datas),
|
|
headers=[
|
|
('Content-Type', 'application/pdf'),
|
|
('Content-Disposition', f'inline; filename={filename}')
|
|
]
|
|
)
|
|
|
|
@http.route('/tour_genius/certificate/view/<int:attempt_id>', type='http', auth='user')
|
|
def view_certificate(self, attempt_id, **kwargs):
|
|
"""
|
|
View certificate PDF inline (for preview).
|
|
"""
|
|
attempt = request.env['genius.quiz.attempt'].browse(attempt_id)
|
|
|
|
# Security check
|
|
if not attempt.exists() or attempt.user_id.id != request.env.user.id:
|
|
return request.not_found()
|
|
|
|
if not attempt.is_passed:
|
|
return request.not_found()
|
|
|
|
# Check for existing attachment
|
|
attachment = request.env['ir.attachment'].search([
|
|
('res_model', '=', 'genius.quiz.attempt'),
|
|
('res_id', '=', attempt.id),
|
|
('mimetype', '=', 'application/pdf'),
|
|
], limit=1)
|
|
|
|
if not attachment:
|
|
attachment_id = attempt.generate_certificate_pdf()
|
|
if attachment_id:
|
|
attachment = request.env['ir.attachment'].browse(attachment_id)
|
|
else:
|
|
return request.not_found()
|
|
|
|
# Return PDF for inline viewing
|
|
return request.make_response(
|
|
base64.b64decode(attachment.datas),
|
|
headers=[
|
|
('Content-Type', 'application/pdf'),
|
|
('Content-Disposition', f'inline; filename="Certificate.pdf"'),
|
|
]
|
|
)
|