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.

53 lines
2.3 KiB

  1. # Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
  3. import json
  4. import base64
  5. import time
  6. from odoo.addons.web.controllers import main as report
  7. from odoo.http import content_disposition, route, request
  8. from odoo.tools.safe_eval import safe_eval
  9. class ReportController(report.ReportController):
  10. @route()
  11. def report_routes(self, reportname, docids=None, converter=None, **data):
  12. if converter == 'excel':
  13. report = request.env['ir.actions.report']._get_report_from_name(
  14. reportname)
  15. context = dict(request.env.context)
  16. if docids:
  17. docids = [int(i) for i in docids.split(',')]
  18. if data.get('options'):
  19. data.update(json.loads(data.pop('options')))
  20. if data.get('context'):
  21. # Ignore 'lang' here, because the context in data is the one
  22. # from the webclient *but* if the user explicitely wants to
  23. # change the lang, this mechanism overwrites it.
  24. data['context'] = json.loads(data['context'])
  25. if data['context'].get('lang'):
  26. del data['context']['lang']
  27. context.update(data['context'])
  28. excel = report.with_context(context).render_excel(
  29. docids, data=data
  30. )[0]
  31. excel = base64.decodestring(excel)
  32. report_name = report.report_file
  33. if report.print_report_name and not len(docids) > 1:
  34. obj = request.env[report.model].browse(docids[0])
  35. report_name = safe_eval(report.print_report_name,
  36. {'object': obj, 'time': time})
  37. excelhttpheaders = [
  38. ('Content-Type', 'application/vnd.openxmlformats-'
  39. 'officedocument.spreadsheetml.sheet'),
  40. ('Content-Length', len(excel)),
  41. (
  42. 'Content-Disposition',
  43. content_disposition(report_name + '.xlsx')
  44. )
  45. ]
  46. return request.make_response(excel, headers=excelhttpheaders)
  47. return super(ReportController, self).report_routes(
  48. reportname, docids, converter, **data
  49. )