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.

155 lines
5.3 KiB

  1. from base64 import b64decode
  2. import requests
  3. from tempfile import NamedTemporaryFile
  4. from openerp.report.report_sxw import report_sxw, rml_parse
  5. from openerp import registry
  6. from py3o.template import Template
  7. _extender_functions = {}
  8. def py3o_report_extender(report_name):
  9. """
  10. A decorator to define function to extend the context sent to a template.
  11. This will be called at the creation of the report.
  12. The following arguments will be passed to it:
  13. - pool: the model pool
  14. - cr: the database cursor
  15. - uid: the id of the user that call the renderer
  16. - localcontext: The context that will be passed to the report engine
  17. - context: the Odoo context
  18. Method copied from CampToCamp report_webkit module.
  19. :param report_name: xml id of the report
  20. :return:
  21. """
  22. def fct1(fct):
  23. lst = _extender_functions.get(report_name)
  24. if not lst:
  25. lst = []
  26. _extender_functions[report_name] = lst
  27. lst.append(fct)
  28. return fct
  29. return fct1
  30. class Py3oParser(report_sxw):
  31. """Custom class that use Py3o to render libroffice reports.
  32. Code partially taken from CampToCamp."""
  33. def __init__(self, name, table, rml=False, parser=rml_parse,
  34. header=False, store=False, register=True):
  35. self.localcontext = {}
  36. super(Py3oParser, self).__init__(
  37. name, table, rml=rml, parser=parser,
  38. header=header, store=store, register=register
  39. )
  40. def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
  41. """ Overide this function to generate our py3o report
  42. """
  43. if report_xml.report_type != 'py3o':
  44. return super(Py3oParser, self).create_single_pdf(
  45. cr, uid, ids, data, report_xml, context=context
  46. )
  47. pool = registry(cr.dbname)
  48. model_data_ids = pool['ir.model.data'].search(
  49. cr, uid, [
  50. ('model', '=', 'ir.actions.report.xml'),
  51. ('res_id', '=', report_xml.id),
  52. ]
  53. )
  54. xml_id = None
  55. if model_data_ids:
  56. model_data = pool['ir.model.data'].browse(
  57. cr, uid, model_data_ids[0], context=context
  58. )
  59. xml_id = '%s.%s' % (model_data.module, model_data.name)
  60. parser_instance = self.parser(cr, uid, self.name2, context=context)
  61. parser_instance.set_context(
  62. self.getObjects(cr, uid, ids, context),
  63. data, ids, report_xml.report_type
  64. )
  65. if xml_id in _extender_functions:
  66. for fct in _extender_functions[xml_id]:
  67. fct(pool, cr, uid, parser_instance.localcontext, context)
  68. template = report_xml.py3o_template_id
  69. filetype = report_xml.py3o_fusion_filetype
  70. # py3o.template operates on filenames so create temporary files.
  71. with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as \
  72. in_temp, \
  73. NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \
  74. out_temp:
  75. in_temp.write(b64decode(template.py3o_template_data))
  76. in_temp.flush()
  77. template = Template(in_temp.name, out_temp.name)
  78. print parser_instance.localcontext
  79. template.render(parser_instance.localcontext)
  80. out_temp.seek(0)
  81. if filetype.human_ext != 'odt':
  82. # Now we ask fusion server to convert our template
  83. fusion_server_obj = pool['py3o.server']
  84. fusion_server_id = fusion_server_obj.search(
  85. cr, uid, [], context=context
  86. )[0]
  87. fusion_server = fusion_server_obj.browse(
  88. cr, uid, fusion_server_id, context=context
  89. )
  90. files = {
  91. 'tmpl_file': out_temp,
  92. }
  93. fields = {
  94. "targetformat": filetype.fusion_ext,
  95. "datadict": "{}",
  96. "image_mapping": "{}",
  97. "skipfusion": True,
  98. }
  99. r = requests.post(fusion_server.url, data=fields, files=files)
  100. chunk_size = 1024
  101. with NamedTemporaryFile(
  102. suffix=filetype.human_ext,
  103. prefix='py3o-template-'
  104. ) as fd:
  105. for chunk in r.iter_content(chunk_size):
  106. fd.write(chunk)
  107. fd.seek(0)
  108. return fd.read(), filetype.human_ext
  109. return out_temp.read(), 'odt'
  110. def create(self, cr, uid, ids, data, context=None):
  111. """ Override this function to handle our py3o report
  112. """
  113. pool = registry(cr.dbname)
  114. ir_action_report_obj = pool['ir.actions.report.xml']
  115. report_xml_ids = ir_action_report_obj.search(
  116. cr, uid, [('report_name', '=', self.name[7:])], context=context
  117. )
  118. if not report_xml_ids:
  119. return super(Py3oParser, self).create(
  120. cr, uid, ids, data, context=context
  121. )
  122. report_xml = ir_action_report_obj.browse(
  123. cr, uid, report_xml_ids[0], context=context
  124. )
  125. result = self.create_source_pdf(
  126. cr, uid, ids, data, report_xml, context
  127. )
  128. if not result:
  129. return False, False
  130. return result