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.

68 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2015 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import http
  22. from openerp.addons.email_template import email_template
  23. from openerp.addons.report.controllers.main import ReportController
  24. from openerp.addons.web.controllers.main import content_disposition
  25. class ReportController(ReportController):
  26. @http.route([
  27. '/report/<path:converter>/<reportname>',
  28. '/report/<path:converter>/<reportname>/<docids>',
  29. ])
  30. def report_routes(self, reportname, docids=None, converter=None, **data):
  31. response = super(ReportController, self).report_routes(
  32. reportname, docids=docids, converter=converter, **data)
  33. if docids:
  34. docids = [int(i) for i in docids.split(',')]
  35. report_xml = http.request.session.model('ir.actions.report.xml')
  36. report_ids = report_xml.search(
  37. [('report_name', '=', reportname)])
  38. for report in report_xml.browse(report_ids):
  39. if not report.download_filename:
  40. continue
  41. objects = http.request.session.model(report.model)\
  42. .browse(docids or [])
  43. generated_filename = email_template.mako_template_env\
  44. .from_string(report.download_filename)\
  45. .render({
  46. 'objects': objects,
  47. 'o': objects[:1],
  48. 'object': objects[:1],
  49. 'ext': report.report_type.replace('qweb-', ''),
  50. })
  51. response.headers['Content-Disposition'] = content_disposition(
  52. generated_filename)
  53. return response
  54. @http.route(['/report/download'])
  55. def report_download(self, data, token):
  56. response = super(ReportController, self).report_download(data, token)
  57. # if we got another content disposition before, ditch the one added
  58. # by super()
  59. last_index = None
  60. for i in range(len(response.headers) - 1, -1, -1):
  61. if response.headers[i][0] == 'Content-Disposition':
  62. if last_index:
  63. response.headers.pop(last_index)
  64. last_index = i
  65. return response