OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
4.1 KiB

  1. # Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
  2. # License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
  3. import json
  4. from werkzeug.urls import url_decode
  5. from odoo.http import content_disposition, request, route, serialize_exception
  6. from odoo.tools import html_escape
  7. from odoo.tools.safe_eval import safe_eval, time
  8. from odoo.addons.web.controllers import main as report
  9. class ReportController(report.ReportController):
  10. @route()
  11. def report_routes(self, reportname, docids=None, converter=None, **data):
  12. if converter == "xml":
  13. report = request.env["ir.actions.report"]._get_report_from_name(reportname)
  14. context = dict(request.env.context)
  15. if docids:
  16. docids = [int(i) for i in docids.split(",")]
  17. if data.get("options"):
  18. data.update(json.loads(data.pop("options")))
  19. if data.get("context"):
  20. # Ignore 'lang' here, because the context in data is the one
  21. # from the webclient *but* if the user explicitely wants to
  22. # change the lang, this mechanism overwrites it.
  23. data["context"] = json.loads(data["context"])
  24. if data["context"].get("lang"):
  25. del data["context"]["lang"]
  26. context.update(data["context"])
  27. xml = report.with_context(context)._render_qweb_xml(docids, data=data)[0]
  28. xmlhttpheaders = [
  29. ("Content-Type", "text/xml"),
  30. ("Content-Length", len(xml)),
  31. ]
  32. return request.make_response(xml, headers=xmlhttpheaders)
  33. else:
  34. return super().report_routes(reportname, docids, converter, **data)
  35. @route()
  36. def report_download(self, data, token, context=None):
  37. requestcontent = json.loads(data)
  38. url, report_type = requestcontent[0], requestcontent[1]
  39. if report_type == "qweb-xml":
  40. try:
  41. reportname = url.split("/report/xml/")[1].split("?")[0]
  42. docids = None
  43. if "/" in reportname:
  44. reportname, docids = reportname.split("/")
  45. if docids:
  46. # Generic report:
  47. response = self.report_routes(
  48. reportname, docids=docids, converter="xml", context=context
  49. )
  50. else:
  51. # Particular report:
  52. # decoding the args represented in JSON
  53. data = dict(url_decode(url.split("?")[1]).items())
  54. if "context" in data:
  55. context = json.loads(context or "{}")
  56. data_context = json.loads(data.pop("context"))
  57. context = json.dumps({**context, **data_context})
  58. response = self.report_routes(
  59. reportname, converter="xml", context=context, **data
  60. )
  61. report_obj = request.env["ir.actions.report"]
  62. report = report_obj._get_report_from_name(reportname)
  63. filename = "%s.xml" % (report.name)
  64. if docids:
  65. ids = [int(doc_id) for doc_id in docids.split(",")]
  66. records = request.env[report.model].browse(ids)
  67. if report.print_report_name and not len(records) > 1:
  68. report_name = safe_eval(
  69. report.print_report_name, {"object": records, "time": time}
  70. )
  71. filename = "{}.xml".format(report_name)
  72. response.headers.add(
  73. "Content-Disposition", content_disposition(filename)
  74. )
  75. response.set_cookie("fileToken", token)
  76. return response
  77. except Exception as e:
  78. se = serialize_exception(e)
  79. error = {"code": 200, "message": "Odoo Server Error", "data": se}
  80. return request.make_response(html_escape(json.dumps(error)))
  81. else:
  82. return super().report_download(data, token, context)