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.

240 lines
8.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 XCG Consulting (http://odoo.consulting)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
  4. from cStringIO import StringIO
  5. import json
  6. import pkg_resources
  7. import os
  8. import sys
  9. from base64 import b64decode
  10. import requests
  11. from tempfile import NamedTemporaryFile
  12. from openerp import _
  13. from openerp import exceptions
  14. from openerp.report.report_sxw import report_sxw, rml_parse
  15. from openerp import registry
  16. from py3o.template.helpers import Py3oConvertor
  17. from py3o.template import Template
  18. _extender_functions = {}
  19. class TemplateNotFound(Exception):
  20. pass
  21. def py3o_report_extender(report_name):
  22. """
  23. A decorator to define function to extend the context sent to a template.
  24. This will be called at the creation of the report.
  25. The following arguments will be passed to it:
  26. - pool: the model pool
  27. - cr: the database cursor
  28. - uid: the id of the user that call the renderer
  29. - localcontext: The context that will be passed to the report engine
  30. - context: the Odoo context
  31. Method copied from CampToCamp report_webkit module.
  32. :param report_name: xml id of the report
  33. :return: a decorated class
  34. """
  35. def fct1(fct):
  36. lst = _extender_functions.get(report_name)
  37. if not lst:
  38. lst = []
  39. _extender_functions[report_name] = lst
  40. lst.append(fct)
  41. return fct
  42. return fct1
  43. class Py3oParser(report_sxw):
  44. """Custom class that use Py3o to render libroffice reports.
  45. Code partially taken from CampToCamp's webkit_report."""
  46. def __init__(self, name, table, rml=False, parser=rml_parse,
  47. header=False, store=False, register=True):
  48. self.localcontext = {}
  49. super(Py3oParser, self).__init__(
  50. name, table, rml=rml, parser=parser,
  51. header=header, store=store, register=register
  52. )
  53. def get_template(self, report_obj):
  54. """private helper to fetch the template data either from the database
  55. or from the default template file provided by the implementer.
  56. ATM this method takes a report definition recordset
  57. to try and fetch the report template from database. If not found it
  58. will fallback to the template file referenced in the report definition.
  59. @param report_obj: a recordset representing the report defintion
  60. @type report_obj: openerp.model.recordset instance
  61. @returns: string or buffer containing the template data
  62. @raises: TemplateNotFound which is a subclass of
  63. openerp.exceptions.DeferredException
  64. """
  65. tmpl_data = None
  66. if report_obj.py3o_template_id and report_obj.py3o_template_id.id:
  67. # if a user gave a report template
  68. tmpl_data = b64decode(
  69. report_obj.py3o_template_id.py3o_template_data
  70. )
  71. elif report_obj.py3o_template_fallback:
  72. tmpl_name = report_obj.py3o_template_fallback
  73. flbk_filename = None
  74. if report_obj.module:
  75. # if the default is defined
  76. flbk_filename = pkg_resources.resource_filename(
  77. "openerp.addons.%s" % report_obj.module,
  78. tmpl_name,
  79. )
  80. elif os.path.isabs(tmpl_name):
  81. # It is an absolute path
  82. flbk_filename = os.path.normcase(os.path.normpath(tmpl_name))
  83. if flbk_filename and os.path.exists(flbk_filename):
  84. # and it exists on the fileystem
  85. with open(flbk_filename, 'r') as tmpl:
  86. tmpl_data = tmpl.read()
  87. if tmpl_data is None:
  88. # if for any reason the template is not found
  89. raise TemplateNotFound(
  90. _(u'No template found. Aborting.'),
  91. sys.exc_info(),
  92. )
  93. return tmpl_data
  94. def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
  95. """ Overide this function to generate our py3o report
  96. """
  97. if report_xml.report_type != 'py3o':
  98. return super(Py3oParser, self).create_single_pdf(
  99. cr, uid, ids, data, report_xml, context=context
  100. )
  101. pool = registry(cr.dbname)
  102. model_data_ids = pool['ir.model.data'].search(
  103. cr, uid, [
  104. ('model', '=', 'ir.actions.report.xml'),
  105. ('res_id', '=', report_xml.id),
  106. ]
  107. )
  108. xml_id = None
  109. if model_data_ids:
  110. model_data = pool['ir.model.data'].browse(
  111. cr, uid, model_data_ids[0], context=context
  112. )
  113. xml_id = '%s.%s' % (model_data.module, model_data.name)
  114. parser_instance = self.parser(cr, uid, self.name2, context=context)
  115. parser_instance.set_context(
  116. self.getObjects(cr, uid, ids, context),
  117. data, ids, report_xml.report_type
  118. )
  119. if xml_id in _extender_functions:
  120. for fct in _extender_functions[xml_id]:
  121. fct(pool, cr, uid, parser_instance.localcontext, context)
  122. tmpl_data = self.get_template(report_xml)
  123. in_stream = StringIO(tmpl_data)
  124. out_stream = StringIO()
  125. template = Template(in_stream, out_stream)
  126. expressions = template.get_all_user_python_expression()
  127. py_expression = template.convert_py3o_to_python_ast(expressions)
  128. convertor = Py3oConvertor()
  129. data_struct = convertor(py_expression)
  130. filetype = report_xml.py3o_fusion_filetype
  131. datadict = parser_instance.localcontext
  132. parsed_datadict = data_struct.render(datadict)
  133. fusion_server_obj = pool.get('py3o.server')
  134. fusion_server_ids = fusion_server_obj.search(
  135. cr, uid, [('is_active', '=', True)], context=context, limit=1
  136. )
  137. if not fusion_server_ids:
  138. if filetype.fusion_ext == report_xml.py3o_template_id.filetype:
  139. # No format conversion is needed, render the template directly
  140. template.render(parsed_datadict)
  141. res = out_stream.getvalue()
  142. else:
  143. raise exceptions.MissingError(
  144. _(u"No Py3o server configuration found")
  145. )
  146. else: # Call py3o.server to render the template in the desired format
  147. fusion_server_id = fusion_server_ids[0]
  148. fusion_server = fusion_server_obj.browse(
  149. cr, uid, fusion_server_id, context=context
  150. )
  151. in_stream.seek(0)
  152. files = {
  153. 'tmpl_file': in_stream,
  154. }
  155. fields = {
  156. "targetformat": filetype.fusion_ext,
  157. "datadict": json.dumps(parsed_datadict),
  158. "image_mapping": "{}",
  159. }
  160. r = requests.post(fusion_server.url, data=fields, files=files)
  161. if r.status_code != 200:
  162. # server says we have an issue... let's tell that to enduser
  163. raise exceptions.Warning(
  164. _('Fusion server error %s') % r.text,
  165. )
  166. # Here is a little joke about Odoo
  167. # we do nice chunked reading from the network...
  168. chunk_size = 1024
  169. with NamedTemporaryFile(
  170. suffix=filetype.human_ext,
  171. prefix='py3o-template-'
  172. ) as fd:
  173. for chunk in r.iter_content(chunk_size):
  174. fd.write(chunk)
  175. fd.seek(0)
  176. # ... but odoo wants the whole data in memory anyways :)
  177. res = fd.read()
  178. return res, filetype.human_ext
  179. def create(self, cr, uid, ids, data, context=None):
  180. """ Override this function to handle our py3o report
  181. """
  182. pool = registry(cr.dbname)
  183. ir_action_report_obj = pool['ir.actions.report.xml']
  184. report_xml_ids = ir_action_report_obj.search(
  185. cr, uid, [('report_name', '=', self.name[7:])], context=context
  186. )
  187. if not report_xml_ids:
  188. return super(Py3oParser, self).create(
  189. cr, uid, ids, data, context=context
  190. )
  191. report_xml = ir_action_report_obj.browse(
  192. cr, uid, report_xml_ids[0], context=context
  193. )
  194. result = self.create_source_pdf(
  195. cr, uid, ids, data, report_xml, context
  196. )
  197. if not result:
  198. return False, False
  199. return result