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.

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