diff --git a/odex25_takaful/odex_takaful/controllers/report_api.py b/odex25_takaful/odex_takaful/controllers/report_api.py index 2ed6ba3f3..a8fe50d5c 100644 --- a/odex25_takaful/odex_takaful/controllers/report_api.py +++ b/odex25_takaful/odex_takaful/controllers/report_api.py @@ -1,69 +1,45 @@ import json -import logging + import werkzeug +import werkzeug.exceptions + from odoo import http from odoo.http import request +from odoo import api, SUPERUSER_ID -_logger = logging.getLogger(__name__) -class YourController(http.Controller): +class ReportController(http.Controller): @http.route([ '/sponsorship//', '/sponsorship///', - ], type='http', auth='public', website=True, csrf=False) + ], type='http', auth='public', website=True) def report_routes(self, reportname, docids=None, converter=None, **data): - try: - # 1. Setup Report and Context - report = request.env['ir.actions.report'].sudo()._get_report_from_name(reportname) - if not report: - return request.not_found() + env = api.Environment(request.cr, SUPERUSER_ID, {}) + report = request.env['ir.actions.report']._get_report_from_name(reportname) + context = dict(request.env.context) - context = dict(request.env.context) - - # FORCE RENDERING: This is crucial to bypass the "Attachment" check - # which is a common cause of 403 errors even with sudo() - context.update({'force_report_rendering': True}) - - if docids: - docids = [int(i) for i in docids.split(',')] - - if data.get('options'): - data.update(json.loads(data.pop('options'))) - - if data.get('context'): - data['context'] = json.loads(data['context']) - if data['context'].get('lang') and not data.get('force_context_lang'): - del data['context']['lang'] - context.update(data['context']) - - # 2. Rendering Logic with SUDO - # We use the updated context here - if converter == 'html': - html = report.with_context(context).sudo()._render_qweb_html(docids, data=data)[0] - return request.make_response(html) - - elif converter == 'pdf': - pdf = report.with_context(context).sudo()._render_qweb_pdf(docids, data=data)[0] - pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))] - return request.make_response(pdf, headers=pdfhttpheaders) - - elif converter == 'text': - text = report.with_context(context).sudo()._render_qweb_text(docids, data=data)[0] - texthttpheaders = [('Content-Type', 'text/plain'), ('Content-Length', len(text))] - return request.make_response(text, headers=texthttpheaders) - - else: - raise werkzeug.exceptions.HTTPException(description='Converter %s not implemented.' % converter) - - except Exception as e: - # 3. DEBUGGING BLOCK - # Crucial: Rollback the database transaction so we can send a response - request.env.cr.rollback() - - # Log the full stack trace to your Odoo terminal/logfile - _logger.exception("Error generating public report for %s", reportname) - - # Return the error message directly to the browser - error_msg = f"DEBUG ERROR: {str(e)}" - return request.make_response(error_msg, headers=[('Content-Type', 'text/plain')]) \ No newline at end of file + if docids: + docids = [int(i) for i in docids.split(',')] + if data.get('options'): + data.update(json.loads(data.pop('options'))) + if data.get('context'): + # Ignore 'lang' here, because the context in data is the one from the webclient *but* if + # the user explicitely wants to change the lang, this mechanism overwrites it. + data['context'] = json.loads(data['context']) + if data['context'].get('lang') and not data.get('force_context_lang'): + del data['context']['lang'] + context.update(data['context']) + if converter == 'html': + html = report.with_env(env).with_context(context)._render_qweb_html(docids, data=data)[0] + return request.make_response(html) + elif converter == 'pdf': + pdf = report.with_env(env).with_context(context)._render_qweb_pdf(docids) + pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))] + return request.make_response(pdf, headers=pdfhttpheaders) + elif converter == 'text': + text = report.with_env(env).with_context(context)._render_qweb_text(docids, data=data)[0] + texthttpheaders = [('Content-Type', 'text/plain'), ('Content-Length', len(text))] + return request.make_response(text, headers=texthttpheaders) + else: + raise werkzeug.exceptions.HTTPException(description='Converter %s not implemented.' % converter)