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.7 KiB

  1. # Copyright (C) 2017 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.exceptions import UserError, ValidationError
  6. from odoo.tools.safe_eval import safe_eval
  7. import json
  8. import time
  9. import werkzeug
  10. class ReportController(report.ReportController):
  11. @route()
  12. def report_routes(self, reportname, docids=None, converter=None, **data):
  13. if converter == 'xlsx':
  14. try:
  15. report = request.env['ir.actions.report']._get_report_from_name(
  16. reportname)
  17. context = dict(request.env.context)
  18. if docids:
  19. docids = [int(i) for i in docids.split(',')]
  20. if data.get('options'):
  21. data.update(json.loads(data.pop('options')))
  22. if data.get('context'):
  23. # Ignore 'lang' here, because the context in data is the one
  24. # from the webclient *but* if the user explicitely wants to
  25. # change the lang, this mechanism overwrites it.
  26. data['context'] = json.loads(data['context'])
  27. if data['context'].get('lang'):
  28. del data['context']['lang']
  29. context.update(data['context'])
  30. xlsx = report.with_context(context).render_xlsx(
  31. docids, data=data
  32. )[0]
  33. report_name = report.report_file
  34. if report.print_report_name and not len(docids) > 1:
  35. obj = request.env[report.model].browse(docids[0])
  36. report_name = safe_eval(report.print_report_name,
  37. {'object': obj, 'time': time})
  38. except (UserError, ValidationError) as odoo_error:
  39. raise werkzeug.exceptions.HTTPException(
  40. description='{error_name}. {error_value}'.format(
  41. error_name=odoo_error.name,
  42. error_value=odoo_error.value,
  43. ))
  44. xlsxhttpheaders = [
  45. ('Content-Type', 'application/vnd.openxmlformats-'
  46. 'officedocument.spreadsheetml.sheet'),
  47. ('Content-Length', len(xlsx)),
  48. (
  49. 'Content-Disposition',
  50. content_disposition(report_name + '.xlsx')
  51. )
  52. ]
  53. return request.make_response(xlsx, headers=xlsxhttpheaders)
  54. return super(ReportController, self).report_routes(
  55. reportname, docids, converter, **data
  56. )