diff --git a/__init__.py b/__init__.py index e75b483b..2c4eac3f 100644 --- a/__init__.py +++ b/__init__.py @@ -1 +1 @@ -import ir_report +import models \ No newline at end of file diff --git a/__openerp__.py b/__openerp__.py index 49cb56cb..40958720 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -41,9 +41,12 @@ The py3o.template package is required; install it with: 'python': ['py3o.template'] }, 'data': [ - 'menu.xml', - 'ir_report.xml', 'security/ir.model.access.csv', + + 'views/py3o_template.xml', + 'views/py3o_server.xml', + 'views/ir_report.xml', + 'views/menu.xml', ], 'installable': True, } diff --git a/models/__init__.py b/models/__init__.py new file mode 100644 index 00000000..28989d9f --- /dev/null +++ b/models/__init__.py @@ -0,0 +1,4 @@ +import ir_report +import py3o_fusion_filetype +import py3o_template +import py3o_server \ No newline at end of file diff --git a/ir_report.py b/models/ir_report.py similarity index 98% rename from ir_report.py rename to models/ir_report.py index 7cc5458e..8ad1f999 100644 --- a/ir_report.py +++ b/models/ir_report.py @@ -1,7 +1,7 @@ import os from openerp.osv import fields, osv from openerp.report.interface import report_int -from .py3o_parser import Py3oParser +from ..py3o_parser import Py3oParser from openerp import addons diff --git a/py3o_fusion_filetype.py b/models/py3o_fusion_filetype.py similarity index 100% rename from py3o_fusion_filetype.py rename to models/py3o_fusion_filetype.py diff --git a/py3o_server.py b/models/py3o_server.py similarity index 100% rename from py3o_server.py rename to models/py3o_server.py diff --git a/py3o_template.py b/models/py3o_template.py similarity index 100% rename from py3o_template.py rename to models/py3o_template.py diff --git a/py3o_parser.py b/py3o_parser.py index 9e6e3aa5..8f1d6de1 100644 --- a/py3o_parser.py +++ b/py3o_parser.py @@ -1,5 +1,9 @@ +from base64 import b64decode +import requests +from tempfile import NamedTemporaryFile from openerp.report.report_sxw import report_sxw, rml_parse from openerp import registry +from py3o.template import Template _extender_functions = {} @@ -64,7 +68,7 @@ class Py3oParser(report_sxw): model_data = pool['ir.model.data'].browse( cr, uid, model_data_ids[0], context=context ) - xml_id = '%s,%s' % (model_data.module, model_data.name) + xml_id = '%s.%s' % (model_data.module, model_data.name) parser_instance = self.parser(cr, uid, self.name2, context=context) parser_instance.set_context( @@ -74,7 +78,57 @@ class Py3oParser(report_sxw): if xml_id in _extender_functions: for fct in _extender_functions[xml_id]: - pass + fct(pool, cr, uid, parser_instance.localcontext, 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(template.py3o_template_data)) + in_temp.flush() + + template = Template(in_temp.name, out_temp.name) + + print parser_instance.localcontext + template.render(parser_instance.localcontext) + + 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": "{}", + "skipfusion": True, + } + 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' def create(self, cr, uid, ids, data, context=None): """ Override this function to handle our py3o report diff --git a/ir_report.xml b/views/ir_report.xml similarity index 100% rename from ir_report.xml rename to views/ir_report.xml diff --git a/menu.xml b/views/menu.xml similarity index 60% rename from menu.xml rename to views/menu.xml index 23589c9e..4f26473f 100644 --- a/menu.xml +++ b/views/menu.xml @@ -2,7 +2,7 @@ + name="Py3o" + parent="report.reporting_menuitem" /> diff --git a/py3o_server.xml b/views/py3o_server.xml similarity index 100% rename from py3o_server.xml rename to views/py3o_server.xml diff --git a/py3o_template.xml b/views/py3o_template.xml similarity index 100% rename from py3o_template.xml rename to views/py3o_template.xml