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.

52 lines
2.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 Therp BV (<http://therp.nl>).
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import http
  5. from openerp.addons.mail.models import mail_template
  6. from openerp.addons.report.controllers.main import ReportController
  7. from openerp.addons.web.controllers.main import content_disposition
  8. class ReportController(ReportController):
  9. @http.route([
  10. '/report/<path:converter>/<reportname>',
  11. '/report/<path:converter>/<reportname>/<docids>',
  12. ])
  13. def report_routes(self, reportname, docids=None, converter=None, **data):
  14. response = super(ReportController, self).report_routes(
  15. reportname, docids=docids, converter=converter, **data)
  16. if docids:
  17. docids = [int(i) for i in docids.split(',')]
  18. report_xml = http.request.session.model('ir.actions.report.xml')
  19. report_ids = report_xml.search(
  20. [('report_name', '=', reportname)])
  21. for report in report_xml.browse(report_ids):
  22. if not report.download_filename:
  23. continue
  24. objects = http.request.session.model(report.model)\
  25. .browse(docids or [])
  26. generated_filename = mail_template.mako_template_env\
  27. .from_string(report.download_filename)\
  28. .render({
  29. 'objects': objects,
  30. 'o': objects[:1],
  31. 'object': objects[:1],
  32. 'ext': report.report_type.replace('qweb-', ''),
  33. })
  34. response.headers['Content-Disposition'] = content_disposition(
  35. generated_filename)
  36. return response
  37. @http.route(['/report/download'])
  38. def report_download(self, data, token):
  39. response = super(ReportController, self).report_download(data, token)
  40. # if we got another content disposition before, ditch the one added
  41. # by super()
  42. last_index = None
  43. for i in range(len(response.headers) - 1, -1, -1):
  44. if response.headers[i][0] == 'Content-Disposition':
  45. if last_index:
  46. response.headers.pop(last_index)
  47. last_index = i
  48. return response