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.

60 lines
2.6 KiB

  1. # Copyright (C) 2019 Creu Blanca
  2. # License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
  3. from odoo.addons.web.controllers import main as report
  4. from odoo.http import content_disposition, route, request
  5. from odoo.tools.safe_eval import safe_eval
  6. import json
  7. import time
  8. class ReportController(report.ReportController):
  9. @route()
  10. def report_routes(self, reportname, docids=None, converter=None, **data):
  11. if converter == 'csv':
  12. report = request.env['ir.actions.report']._get_report_from_name(
  13. reportname)
  14. context = dict(request.env.context)
  15. if docids:
  16. docids = [int(i) for i in docids.split(',')]
  17. if data.get('options'):
  18. data.update(json.loads(data.pop('options')))
  19. if data.get('context'):
  20. # Ignore 'lang' here, because the context in data is the one
  21. # from the webclient *but* if the user explicitely wants to
  22. # change the lang, this mechanism overwrites it.
  23. data['context'] = json.loads(data['context'])
  24. if data['context'].get('lang'):
  25. del data['context']['lang']
  26. context.update(data['context'])
  27. csv = report.with_context(context).render_csv(
  28. docids, data=data
  29. )[0]
  30. filename = "%s.%s" % (report.name, "csv")
  31. if docids:
  32. obj = request.env[report.model].browse(docids)
  33. if report.print_report_name and not len(obj) > 1:
  34. report_name = safe_eval(
  35. report.print_report_name,
  36. {'object': obj, 'time': time, 'multi': False})
  37. filename = "%s.%s" % (report_name, "csv")
  38. # When we print multiple records we still allow a custom
  39. # filename.
  40. elif report.print_report_name and len(obj) > 1:
  41. report_name = safe_eval(
  42. report.print_report_name,
  43. {'objects': obj, 'time': time, 'multi': True})
  44. filename = "%s.%s" % (report_name, "csv")
  45. csvhttpheaders = [
  46. ('Content-Type', 'text/csv'),
  47. ('Content-Length', len(csv)),
  48. (
  49. 'Content-Disposition',
  50. content_disposition(filename)
  51. )
  52. ]
  53. return request.make_response(csv, headers=csvhttpheaders)
  54. return super(ReportController, self).report_routes(
  55. reportname, docids, converter, **data
  56. )