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.

54 lines
2.4 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_name = report.with_context(context).render_excel(
  29. docids, data=data
  30. )
  31. excel = base64.decodestring(excel)
  32. if report.print_report_name and not len(docids) > 1:
  33. obj = request.env[report.model].browse(docids[0])
  34. file_ext = report_name.split('.')[-1:].pop()
  35. report_name = safe_eval(report.print_report_name,
  36. {'object': obj, 'time': time})
  37. report_name = '%s.%s' % (report_name, file_ext)
  38. excelhttpheaders = [
  39. ('Content-Type', 'application/vnd.openxmlformats-'
  40. 'officedocument.spreadsheetml.sheet'),
  41. ('Content-Length', len(excel)),
  42. (
  43. 'Content-Disposition',
  44. content_disposition(report_name)
  45. )
  46. ]
  47. return request.make_response(excel, headers=excelhttpheaders)
  48. return super(ReportController, self).report_routes(
  49. reportname, docids, converter, **data
  50. )