diff --git a/odex25_takaful/odex_takaful/controllers/report_api.py b/odex25_takaful/odex_takaful/controllers/report_api.py index fbb937f2e..2ed6ba3f3 100644 --- a/odex25_takaful/odex_takaful/controllers/report_api.py +++ b/odex25_takaful/odex_takaful/controllers/report_api.py @@ -1,53 +1,69 @@ - - import json import logging - - import werkzeug -import werkzeug.exceptions -import werkzeug.utils -import werkzeug.wrappers -import werkzeug.wsgi - -from odoo import http, tools -from odoo.http import content_disposition, dispatch_rpc, request, serialize_exception as _serialize_exception, Response +from odoo import http +from odoo.http import request _logger = logging.getLogger(__name__) - - -class ReportController(http.Controller): +class YourController(http.Controller): @http.route([ '/sponsorship//', '/sponsorship///', - ], type='http', auth='public', website=True) + ], type='http', auth='public', website=True, csrf=False) def report_routes(self, reportname, docids=None, converter=None, **data): - report = request.env['ir.actions.report'].sudo()._get_report_from_name(reportname) - context = dict(request.env.context) + 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() - 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_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) + 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