diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index 488250ce4a1..1fca997541f 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -12,7 +12,7 @@ from math import ceil from odoo import fields, http, _ from odoo.addons.base.models.ir_ui_view import keep_query from odoo.exceptions import UserError -from odoo.http import request +from odoo.http import request, content_disposition from odoo.tools import ustr _logger = logging.getLogger(__name__) @@ -501,6 +501,37 @@ class Survey(http.Controller): # filter_finish: boolean => only finished surveys or not # + @http.route(['/survey//get_certification'], type='http', auth='user', methods=['POST'], website=True) + def survey_get_certification(self, survey_id, **kwargs): + """ The certification document can be downloaded as long as the user has succeeded the certification """ + survey = request.env['survey.survey'].sudo().search([ + ('id', '=', survey_id), + ('certificate', '=', True) + ]) + + if not survey: + # no certification found + return werkzeug.utils.redirect("/") + + succeeded_attempt = request.env['survey.user_input'].sudo().search([ + ('partner_id', '=', request.env.user.partner_id.id), + ('survey_id', '=', survey_id), + ('quizz_passed', '=', True) + ], limit=1) + + if not succeeded_attempt: + raise UserError(_("The user has not succeeded the certification")) + + report_sudo = request.env.ref('survey.certification_report').sudo() + + report = report_sudo.render_qweb_pdf([succeeded_attempt.id], data={'report_type': 'pdf'})[0] + reporthttpheaders = [ + ('Content-Type', 'application/pdf'), + ('Content-Length', len(report)), + ] + reporthttpheaders.append(('Content-Disposition', content_disposition('Certification.pdf'))) + return request.make_response(report, headers=reporthttpheaders) + def _prepare_result_dict(self, survey, current_filters=None): """Returns dictionary having values for rendering template""" current_filters = current_filters if current_filters else [] diff --git a/addons/survey/views/survey_reports.xml b/addons/survey/views/survey_reports.xml index fb8783b5136..28a41c6bffe 100644 --- a/addons/survey/views/survey_reports.xml +++ b/addons/survey/views/survey_reports.xml @@ -10,7 +10,7 @@ name="survey.certification_report_view" file="survey.certification_report_view" attachment="'certification.pdf'" - print_report_name="'Certification - %s' % (object.survey_id.name)" + print_report_name="'Certification - %s' % (object.survey_id.display_name)" />