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.

91 lines
3.8 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. import time
  5. from werkzeug.urls import url_decode
  6. from odoo.http import content_disposition, request, route, serialize_exception
  7. from odoo.tools import html_escape, safe_eval
  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):
  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"
  49. )
  50. else:
  51. # Particular report:
  52. # decoding the args represented in JSON
  53. data = url_decode(url.split("?")[1]).items()
  54. response = self.report_routes(
  55. reportname, converter="xml", **dict(data)
  56. )
  57. report_obj = request.env["ir.actions.report"]
  58. report = report_obj._get_report_from_name(reportname)
  59. filename = "%s.xml" % (report.name)
  60. if docids:
  61. ids = [int(doc_id) for doc_id in docids.split(",")]
  62. records = request.env[report.model].browse(ids)
  63. if report.print_report_name and not len(records) > 1:
  64. report_name = safe_eval(
  65. report.print_report_name, {"object": records, "time": time}
  66. )
  67. filename = "{}.xml".format(report_name)
  68. response.headers.add(
  69. "Content-Disposition", content_disposition(filename)
  70. )
  71. response.set_cookie("fileToken", token)
  72. return response
  73. except Exception as e:
  74. se = serialize_exception(e)
  75. error = {"code": 200, "message": "Odoo Server Error", "data": se}
  76. return request.make_response(html_escape(json.dumps(error)))
  77. else:
  78. return super().report_download(data, token)