中国本土应用
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.

105 lines
4.2KB

  1. # Copyright (C) 2017 Creu Blanca
  2. # Copyright 2021 ForgeFlow S.L.
  3. # License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
  4. import json
  5. import logging
  6. from werkzeug.urls import url_decode
  7. from odoo.http import (
  8. content_disposition,
  9. request,
  10. route,
  11. serialize_exception as _serialize_exception,
  12. )
  13. from odoo.tools import html_escape
  14. from odoo.tools.safe_eval import safe_eval, time
  15. from odoo.addons.web.controllers import main as report
  16. _logger = logging.getLogger(__name__)
  17. class ReportController(report.ReportController):
  18. @route()
  19. def report_routes(self, reportname, docids=None, converter=None, **data):
  20. if converter == "xlsx":
  21. report = request.env["ir.actions.report"]._get_report_from_name(reportname)
  22. context = dict(request.env.context)
  23. if docids:
  24. docids = [int(i) for i in docids.split(",")]
  25. if data.get("options"):
  26. data.update(json.loads(data.pop("options")))
  27. if data.get("context"):
  28. data["context"] = json.loads(data["context"])
  29. context.update(data["context"])
  30. xlsx = report.with_context(**context)._render_xlsx(docids, data=data)[0]
  31. xlsxhttpheaders = [
  32. (
  33. "Content-Type",
  34. "application/vnd.openxmlformats-"
  35. "officedocument.spreadsheetml.sheet",
  36. ),
  37. ("Content-Length", len(xlsx)),
  38. ]
  39. return request.make_response(xlsx, headers=xlsxhttpheaders)
  40. return super(ReportController, self).report_routes(
  41. reportname, docids, converter, **data
  42. )
  43. @route()
  44. def report_download(self, data, context=None):
  45. requestcontent = json.loads(data)
  46. url, report_type = requestcontent[0], requestcontent[1]
  47. try:
  48. if report_type == "xlsx":
  49. reportname = url.split("/report/xlsx/")[1].split("?")[0]
  50. docids = None
  51. if "/" in reportname:
  52. reportname, docids = reportname.split("/")
  53. if docids:
  54. # Generic report:
  55. response = self.report_routes(
  56. reportname, docids=docids, converter="xlsx", context=context
  57. )
  58. else:
  59. # Particular report:
  60. data = dict(
  61. url_decode(url.split("?")[1]).items()
  62. ) # decoding the args represented in JSON
  63. if "context" in data:
  64. context, data_context = json.loads(context or "{}"), json.loads(
  65. data.pop("context")
  66. )
  67. context = json.dumps({**context, **data_context})
  68. response = self.report_routes(
  69. reportname, converter="xlsx", context=context, **data
  70. )
  71. report = request.env["ir.actions.report"]._get_report_from_name(
  72. reportname
  73. )
  74. filename = "%s.%s" % (report.name, "xlsx")
  75. if docids:
  76. ids = [int(x) for x in docids.split(",")]
  77. obj = request.env[report.model].browse(ids)
  78. if report.print_report_name and not len(obj) > 1:
  79. report_name = safe_eval(
  80. report.print_report_name, {"object": obj, "time": time}
  81. )
  82. filename = "%s.%s" % (report_name, "xlsx")
  83. if not response.headers.get("Content-Disposition"):
  84. response.headers.add(
  85. "Content-Disposition", content_disposition(filename)
  86. )
  87. return response
  88. else:
  89. return super(ReportController, self).report_download(data, context)
  90. except Exception as e:
  91. _logger.exception("Error while generating report %s", reportname)
  92. se = _serialize_exception(e)
  93. error = {"code": 200, "message": "Odoo Server Error", "data": se}
  94. return request.make_response(html_escape(json.dumps(error)))
上海开阖软件有限公司 沪ICP备12045867号-1