From 483dbe7accd45fed592763a3ebf8a1cdc1bbca91 Mon Sep 17 00:00:00 2001 From: Anael Lorimier Date: Tue, 14 Oct 2014 10:19:12 +0200 Subject: [PATCH] Now we call fusion server just to convert reports into the right format --- __init__.py | 3 +++ __openerp__.py | 6 +++++- ir_report.py | 49 ++++++++++++------------------------------------- ir_report.xml | 8 ++++---- py3o_report.py | 35 ++++++++++++++++++++++++++++++++++- 5 files changed, 58 insertions(+), 43 deletions(-) diff --git a/__init__.py b/__init__.py index a6aad7f2..9071ae81 100644 --- a/__init__.py +++ b/__init__.py @@ -1,2 +1,5 @@ import ir_report import py3o_report +import py3o_template +import py3o_server +import py3o_fusion_filetype diff --git a/__openerp__.py b/__openerp__.py index d44ab36b..07c12921 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -16,10 +16,14 @@ The py3o.template package is required; install it with: 'base' ], 'external_dependencies': { - 'python': ['py3o.template'] + 'python': ['py3o.template', 'oe_json_serializer'] }, 'data': [ + 'menu.xml', 'ir_report.xml', + 'py3o_template.xml', + 'py3o_server.xml', + 'data/py3o.fusion.filetype.csv', ], 'installable': True, } diff --git a/ir_report.py b/ir_report.py index c4bd8595..2e545bbf 100644 --- a/ir_report.py +++ b/ir_report.py @@ -1,46 +1,21 @@ -from base64 import b64encode - -from openerp import addons from openerp.osv import fields, osv -from openerp.tools.translate import _ -class report_xml(osv.osv): +class report_xml(osv.Model): ''' Inherit from ir.actions.report.xml to allow customizing the template - file. By default, the file defined when registering the report is used; - but the user can download / upload a new one. ''' + file. The user cam chose a template from a list. + The list is configurable in the configuration tab, see py3o_template.py + ''' _inherit = 'ir.actions.report.xml' - def _get_filename(self, cr, uid, ids, field_name, arg, context): - return { - br.id: br.name + '.odt' - for br in self.browse(cr, uid, ids, context=context) - if br.report_type == 'py3o' - } - - def _get_template_data(self, cr, uid, ids, field_name, arg, context): - ''' Just return the data stored in the binary field, unless it is - empty; in that case, read the template file. ''' - - return { - br.id: (br.py3o_template_data or - b64encode(file(addons.get_module_resource( - *br.report_file.split('/')), 'rb').read())) - for br in self.browse(cr, uid, ids, context=context) - if br.report_type == 'py3o' - } - _columns = { - 'py3o_filename': fields.function(_get_filename, - type='char', - method=True, - readonly=True), - - 'py3o_template': fields.function(_get_template_data, - type='binary', - method=True, - readonly=True), - - 'py3o_template_data': fields.binary(_('LibreOffice template')), + 'py3o_fusion_filetype': fields.many2one( + 'py3o.fusion.filetype', + u"Output Format", + ), + 'py3o_template_id': fields.many2one( + 'py3o.template', + u"Template", + ), } diff --git a/ir_report.xml b/ir_report.xml index 5810a745..d4521c76 100644 --- a/ir_report.xml +++ b/ir_report.xml @@ -13,10 +13,10 @@ - - - + + + + diff --git a/py3o_report.py b/py3o_report.py index 0ff89e1f..9b3fecb1 100644 --- a/py3o_report.py +++ b/py3o_report.py @@ -8,6 +8,8 @@ from openerp.osv.osv import except_osv from py3o.template import Template +import requests + class py3o_report(report_sxw): # def __init__(self, name, table): @@ -52,13 +54,16 @@ class py3o_report(report_sxw): report_xml_ids[0], context=context) + template = report_xml.py3o_template_id + filetype = report_xml.py3o_fusion_filetype + # py3o.template operates on filenames so create temporary files. with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as \ in_temp, \ NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \ out_temp: - in_temp.write(b64decode(report_xml.py3o_template)) + in_temp.write(b64decode(template.py3o_template_data)) in_temp.flush() template = Template(in_temp.name, out_temp.name) @@ -66,6 +71,34 @@ class py3o_report(report_sxw): template.render(self.get_values(cr, uid, ids, data, context)) out_temp.seek(0) + + if filetype.human_ext != 'odt': + # Now we ask fusion server to convert our template + fusion_server_obj = pool['py3o.server'] + fusion_server_id = fusion_server_obj.search( + cr, uid, [], context=context + )[0] + fusion_server = fusion_server_obj.browse( + cr, uid, fusion_server_id, context=context + ) + files = { + 'tmpl_file': out_temp, + } + fields = { + "targetformat": filetype.fusion_ext, + "datadict": "{}", + "image_mapping": "{}", + } + r = requests.post(fusion_server.url, data=fields, files=files) + chunk_size = 1024 + with NamedTemporaryFile( + suffix=filetype.human_ext, + prefix='py3o-template-') as fd: + for chunk in r.iter_content(chunk_size): + fd.write(chunk) + fd.seek(0) + return fd.read(), filetype.human_ext + return out_temp.read(), 'odt' return False, False