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