|
| 1 | +# Copyright (C) 2019 Creu Blanca |
| 2 | +# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html). |
| 3 | + |
| 4 | +import json |
| 5 | +import logging |
| 6 | + |
| 7 | +from werkzeug.exceptions import InternalServerError |
| 8 | +from werkzeug.urls import url_decode |
| 9 | + |
| 10 | +from odoo.http import ( |
| 11 | + content_disposition, |
| 12 | + request, |
| 13 | + route, |
| 14 | +) |
| 15 | +from odoo.http import ( |
| 16 | + serialize_exception as _serialize_exception, |
| 17 | +) |
| 18 | +from odoo.tools import html_escape |
| 19 | +from odoo.tools.safe_eval import safe_eval, time |
| 20 | + |
| 21 | +from odoo.addons.web.controllers import report |
| 22 | + |
| 23 | +_logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +class ReportController(report.ReportController): |
| 27 | + @route() |
| 28 | + def report_routes(self, reportname, docids=None, converter=None, **data): |
| 29 | + if converter == "csv": |
| 30 | + report = request.env["ir.actions.report"]._get_report_from_name(reportname) |
| 31 | + context = dict(request.env.context) |
| 32 | + if docids: |
| 33 | + docids = [int(i) for i in docids.split(",")] |
| 34 | + if data.get("options"): |
| 35 | + data.update(json.loads(data.pop("options"))) |
| 36 | + if data.get("context"): |
| 37 | + # Ignore 'lang' here, because the context in data is the one |
| 38 | + # from the webclient *but* if the user explicitely wants to |
| 39 | + # change the lang, this mechanism overwrites it. |
| 40 | + data["context"] = json.loads(data["context"]) |
| 41 | + if data["context"].get("lang"): |
| 42 | + del data["context"]["lang"] |
| 43 | + context.update(data["context"]) |
| 44 | + csv = report.with_context(**context)._render_csv( |
| 45 | + reportname, docids, data=data |
| 46 | + )[0] |
| 47 | + csvhttpheaders = [ |
| 48 | + ("Content-Type", "text/csv"), |
| 49 | + ("Content-Length", len(csv)), |
| 50 | + ] |
| 51 | + return request.make_response(csv, headers=csvhttpheaders) |
| 52 | + return super().report_routes(reportname, docids, converter, **data) |
| 53 | + |
| 54 | + @route() |
| 55 | + def report_download(self, data, context=None, token=None): |
| 56 | + requestcontent = json.loads(data) |
| 57 | + url, report_type = requestcontent[0], requestcontent[1] |
| 58 | + reportname = "" |
| 59 | + try: |
| 60 | + if report_type == "csv": |
| 61 | + reportname = url.split("/report/csv/")[1].split("?")[0] |
| 62 | + docids = None |
| 63 | + if "/" in reportname: |
| 64 | + reportname, docids = reportname.split("/") |
| 65 | + if docids: |
| 66 | + # Generic report: |
| 67 | + response = self.report_routes( |
| 68 | + reportname, docids=docids, converter="csv", context=context |
| 69 | + ) |
| 70 | + else: |
| 71 | + # Particular report: |
| 72 | + data = dict( |
| 73 | + url_decode(url.split("?")[1]).items() |
| 74 | + ) # decoding the args represented in JSON |
| 75 | + if "context" in data: |
| 76 | + context, data_context = ( |
| 77 | + json.loads(context or "{}"), |
| 78 | + json.loads(data.pop("context")), |
| 79 | + ) |
| 80 | + context = json.dumps({**context, **data_context}) |
| 81 | + response = self.report_routes( |
| 82 | + reportname, converter="csv", context=context, **data |
| 83 | + ) |
| 84 | + |
| 85 | + report = request.env["ir.actions.report"]._get_report_from_name( |
| 86 | + reportname |
| 87 | + ) |
| 88 | + filename = f"{report.name}.csv" |
| 89 | + |
| 90 | + if docids: |
| 91 | + ids = [int(x) for x in docids.split(",")] |
| 92 | + obj = request.env[report.model].browse(ids) |
| 93 | + if report.print_report_name and not len(obj) > 1: |
| 94 | + report_name = safe_eval( |
| 95 | + report.print_report_name, {"object": obj, "time": time} |
| 96 | + ) |
| 97 | + filename = f"{report_name}.csv" |
| 98 | + response.headers.add( |
| 99 | + "Content-Disposition", content_disposition(filename) |
| 100 | + ) |
| 101 | + return response |
| 102 | + else: |
| 103 | + return super().report_download(data, context, token=token) |
| 104 | + except Exception as e: |
| 105 | + _logger.exception("Error while generating report %s", reportname) |
| 106 | + se = _serialize_exception(e) |
| 107 | + error = {"code": 200, "message": "Odoo Server Error", "data": se} |
| 108 | + res = request.make_response(html_escape(json.dumps(error))) |
| 109 | + raise InternalServerError(response=res) from e |
0 commit comments