reporting-engine/report_xlsx/controllers/main.py

57 lines
2.5 KiB
Python
Raw Normal View History

2021-03-23 19:14:27 +00:00
# Copyright (C) 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
import json
2021-11-14 03:12:55 +00:00
from flectra.http import content_disposition, request, route, serialize_exception
from flectra.tools import html_escape
2021-03-23 19:14:27 +00:00
from flectra.tools.safe_eval import safe_eval
from flectra.addons.web.controllers import main as report
class ReportController(report.ReportController):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
if converter == "xlsx":
2021-11-14 03:12:55 +00:00
return self._report_routes_xlsx(reportname, docids, converter, **data)
return super(ReportController, self).report_routes(
reportname, docids, converter, **data
)
def _report_routes_xlsx(self, reportname, docids=None, converter=None, **data):
try:
2021-03-23 19:14:27 +00:00
report = request.env["ir.actions.report"]._get_report_from_name(reportname)
context = dict(request.env.context)
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"):
del data["context"]["lang"]
context.update(data["context"])
xlsx = report.with_context(context)._render_xlsx(docids, data=data)[0]
report_name = report.report_file
if report.print_report_name and not len(docids) > 1:
obj = request.env[report.model].browse(docids[0])
report_name = safe_eval(report.print_report_name, {"object": obj})
xlsxhttpheaders = [
(
"Content-Type",
"application/vnd.openxmlformats-"
"officedocument.spreadsheetml.sheet",
),
("Content-Length", len(xlsx)),
("Content-Disposition", content_disposition(report_name + ".xlsx")),
]
return request.make_response(xlsx, headers=xlsxhttpheaders)
2021-11-14 03:12:55 +00:00
except Exception as e:
se = serialize_exception(e)
error = {"code": 200, "message": "Flectra Server Error", "data": se}
return request.make_response(html_escape(json.dumps(error)))