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.

72 lines
3.1 KiB

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