Newer
Older
import json
import mimetypes
from werkzeug import exceptions, url_decode
from odoo.http import request, route
from odoo.tools import html_escape
from odoo.addons.web.controllers import main
from odoo.addons.web.controllers.main import (
_serialize_exception,
content_disposition,
)
class ReportController(main.ReportController):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
if converter != "redner":
reportname=reportname,
docids=docids,
converter=converter,
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
)
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"])
ir_action = request.env["ir.actions.report"]
action_redner_report = ir_action.get_from_report_name(
reportname, "redner"
).with_context(context)
if not action_redner_report:
raise exceptions.HTTPException(
description="Redner action report not found for report_name "
"%s" % reportname
)
res, filetype = action_redner_report.render(docids, data)
filename = action_redner_report.gen_report_download_filename(
docids, data
)
if not filename.endswith(filetype):
filename = "{}.{}".format(filename, filetype)
content_type = mimetypes.guess_type("x." + filetype)[0]
http_headers = [
("Content-Type", content_type),
("Content-Length", len(res)),
("Content-Disposition", content_disposition(filename)),
]
return request.make_response(res, headers=http_headers)
@route()
def report_download(self, data, token):
"""This function is used by 'redneractionmanager.js' in order to
trigger the download of a redner/controller report.
:param data: a javascript array JSON.stringified containg report
internal url ([0]) and type [1]
:returns: Response with a filetoken cookie and an attachment header
"""
requestcontent = json.loads(data)
url, report_type = requestcontent[0], requestcontent[1]
if "redner" not in report_type:
try:
reportname = url.split("/report/redner/")[1].split("?")[0]
docids = None
if "/" in reportname:
reportname, docids = reportname.split("/")
if docids:
# Generic report:
response = self.report_routes(
reportname, docids=docids, converter="redner"
)
else:
# Particular report:
# decoding the args represented in JSON
data = list(url_decode(url.split("?")[1]).items())
response = self.report_routes(
reportname, converter="redner", **dict(data)
)
response.set_cookie("fileToken", token)
return response
except Exception as e:
se = _serialize_exception(e)
error = {"code": 200, "message": "Odoo Server Error", "data": se}
return request.make_response(html_escape(json.dumps(error)))