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.

85 lines
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from openerp.addons.web.http import Controller, route, request
  4. from openerp.addons.web.controllers.main import (
  5. _serialize_exception,
  6. content_disposition,
  7. )
  8. from openerp.tools import html_escape
  9. import json
  10. import time
  11. from werkzeug import url_decode
  12. from werkzeug.test import Client
  13. from werkzeug.wrappers import BaseResponse
  14. from werkzeug.datastructures import Headers
  15. from openerp.addons.report.controllers.main import ReportController
  16. from openerp.tools.safe_eval import safe_eval
  17. class ReportCustom(ReportController):
  18. @route(["/report/download"], type="http", auth="user")
  19. def report_download(self, data, token):
  20. """This function is used by 'qwebactionmanager.js' in order to trigger the download of
  21. a pdf/controller report.
  22. :param data: a javascript array JSON.stringified containg report internal url ([0]) and
  23. type [1]
  24. :returns: Response with a filetoken cookie and an attachment header
  25. """
  26. requestcontent = json.loads(data)
  27. url, type = requestcontent[0], requestcontent[1]
  28. try:
  29. if type == "qweb-pdf":
  30. reportname = url.split("/report/pdf/")[1].split("?")[0]
  31. docids = None
  32. if "/" in reportname:
  33. reportname, docids = reportname.split("/")
  34. if docids:
  35. # Generic report:
  36. response = self.report_routes(
  37. reportname, docids=docids, converter="pdf"
  38. )
  39. else:
  40. # Particular report:
  41. data = url_decode(
  42. url.split("?")[1]
  43. ).items() # decoding the args represented in JSON
  44. response = self.report_routes(
  45. reportname, converter="pdf", **dict(data)
  46. )
  47. cr, uid = request.cr, request.uid
  48. report = request.registry["report"]._get_report_from_name(
  49. cr, uid, reportname
  50. )
  51. filename = "%s.%s" % (report.name, "pdf")
  52. if docids:
  53. ids = [int(x) for x in docids.split(",")]
  54. obj = request.env[report.model].browse(ids)
  55. if report.attachment and not len(obj) > 1:
  56. filename = safe_eval(
  57. report.attachment, {"object": obj, "time": time}
  58. )
  59. response.headers.add(
  60. "Content-Disposition", content_disposition(filename)
  61. )
  62. response.set_cookie("fileToken", token)
  63. return response
  64. elif type == "controller":
  65. reqheaders = Headers(request.httprequest.headers)
  66. response = Client(request.httprequest.app, BaseResponse).get(
  67. url, headers=reqheaders, follow_redirects=True
  68. )
  69. response.set_cookie("fileToken", token)
  70. return response
  71. else:
  72. return
  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)))