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.

228 lines
7.6 KiB

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