From cbc1416f53aa5981d8523f9e9d6505ad1cdc1773 Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Tue, 4 Oct 2016 11:26:15 +0200 Subject: [PATCH] [IMP] Allow user to use local fusion --- report_py3o/__init__.py | 2 +- report_py3o/__openerp__.py | 5 +- report_py3o/data/py3o.fusion.filetype.csv | 6 --- report_py3o/models/__init__.py | 3 +- report_py3o/models/ir_report.py | 37 +++++++++++-- report_py3o/models/py3o_fusion_filetype.py | 13 ----- report_py3o/py3o_parser.py | 61 +++++++++------------- report_py3o/views/ir_report.xml | 2 + 8 files changed, 63 insertions(+), 66 deletions(-) delete mode 100644 report_py3o/data/py3o.fusion.filetype.csv delete mode 100644 report_py3o/models/py3o_fusion_filetype.py diff --git a/report_py3o/__init__.py b/report_py3o/__init__.py index 9a7e03ed..0650744f 100644 --- a/report_py3o/__init__.py +++ b/report_py3o/__init__.py @@ -1 +1 @@ -from . import models \ No newline at end of file +from . import models diff --git a/report_py3o/__openerp__.py b/report_py3o/__openerp__.py index 8a4d7990..b5b8d052 100644 --- a/report_py3o/__openerp__.py +++ b/report_py3o/__openerp__.py @@ -19,7 +19,8 @@ The py3o.template package is required; install it with: 'report' ], 'external_dependencies': { - 'python': ['py3o.template'] + 'python': ['py3o.template', + 'py3o.formats'] }, 'data': [ 'security/ir.model.access.csv', @@ -28,8 +29,6 @@ The py3o.template package is required; install it with: 'views/py3o_template.xml', 'views/py3o_server.xml', 'views/ir_report.xml', - - 'data/py3o.fusion.filetype.csv', ], 'installable': True, } diff --git a/report_py3o/data/py3o.fusion.filetype.csv b/report_py3o/data/py3o.fusion.filetype.csv deleted file mode 100644 index 503c6da1..00000000 --- a/report_py3o/data/py3o.fusion.filetype.csv +++ /dev/null @@ -1,6 +0,0 @@ -id,fusion_ext,human_ext -py3o_fusion_filetype_odt,odt,odt -py3o_fusion_filetype_ods,ods,ods -py3o_fusion_filetype_doc,doc,doc -py3o_fusion_filetype_docx,docx,docx -py3o_fusion_filetype_pdf,pdf,pdf diff --git a/report_py3o/models/__init__.py b/report_py3o/models/__init__.py index ccf621aa..68f18d48 100644 --- a/report_py3o/models/__init__.py +++ b/report_py3o/models/__init__.py @@ -1,4 +1,3 @@ from . import ir_report -from . import py3o_fusion_filetype from . import py3o_template -from . import py3o_server \ No newline at end of file +from . import py3o_server diff --git a/report_py3o/models/ir_report.py b/report_py3o/models/ir_report.py index 2f520b98..5097553a 100644 --- a/report_py3o/models/ir_report.py +++ b/report_py3o/models/ir_report.py @@ -2,11 +2,12 @@ # Copyright 2013 XCG Consulting (http://odoo.consulting) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import os +from py3o.formats import Formats from openerp import api, fields, models from openerp.report.interface import report_int -from ..py3o_parser import Py3oParser from openerp.exceptions import ValidationError from openerp import addons +from ..py3o_parser import Py3oParser class ReportXml(models.Model): @@ -24,12 +25,40 @@ class ReportXml(models.Model): raise ValidationError( "Field 'Output Format' is required for Py3O report") - py3o_fusion_filetype = fields.Many2one( - 'py3o.fusion.filetype', - "Output Format") + @api.one + @api.constrains("py3o_is_local_fusion", "py3o_server_id", + "py3o_fusion_filetype") + def _check_py3o_server_id(self): + is_native = Formats().get_format(self.py3o_fusion_filetype) + if ((not is_native or not self.py3o_is_local_fusion) and + not self.py3o_server_id): + raise ValidationError( + "Can not use not native format in local fusion. " + "Please specify a Fusion Server") + + @api.model + def _get_py3o_fusion_filetypes(self): + formats = Formats() + names = formats.get_known_format_names() + selections = [] + for name in names: + selections.append((name, name)) + return selections + + py3o_fusion_filetype = fields.Selection( + selection="_get_py3o_fusion_filetypes", + string="Output Format") py3o_template_id = fields.Many2one( 'py3o.template', "Template") + py3o_is_local_fusion = fields.Boolean( + "Local fusion", + help="Odt to Odt will be processed without sever. You must use this " + "mode if you call methods on your model into the template.", + default=False) + py3o_server_id = fields.Many2one( + "py3o.server" + "Fusion server") module = fields.Char( "Module", help="The implementer module that provides this report") diff --git a/report_py3o/models/py3o_fusion_filetype.py b/report_py3o/models/py3o_fusion_filetype.py deleted file mode 100644 index 009494fc..00000000 --- a/report_py3o/models/py3o_fusion_filetype.py +++ /dev/null @@ -1,13 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2013 XCG Consulting (http://odoo.consulting) -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import fields, models - - -class Py3oFusionFiletype(models.Model): - _name = 'py3o.fusion.filetype' - - _rec_name = 'human_ext' - - fusion_ext = fields.Char("Fusion Extension", siez=8) - human_ext = fields.Char("Human readble extension", size=8) diff --git a/report_py3o/py3o_parser.py b/report_py3o/py3o_parser.py index ab8c41c5..d0d20151 100644 --- a/report_py3o/py3o_parser.py +++ b/report_py3o/py3o_parser.py @@ -9,14 +9,15 @@ import sys from base64 import b64decode import requests from tempfile import NamedTemporaryFile +from py3o.template.helpers import Py3oConvertor +from py3o.template import Template +from py3o.formats import Formats + from openerp import _ from openerp import exceptions from openerp.report.report_sxw import report_sxw, rml_parse from openerp import registry -from py3o.template.helpers import Py3oConvertor -from py3o.template import Template - _extender_functions = {} @@ -101,9 +102,9 @@ class Py3oParser(report_sxw): # It is an absolute path flbk_filename = os.path.normcase(os.path.normpath(tmpl_name)) if flbk_filename and os.path.exists(flbk_filename): - # and it exists on the fileystem - with open(flbk_filename, 'r') as tmpl: - tmpl_data = tmpl.read() + # and it exists on the fileystem + with open(flbk_filename, 'r') as tmpl: + tmpl_data = tmpl.read() if tmpl_data is None: # if for any reason the template is not found @@ -152,47 +153,33 @@ class Py3oParser(report_sxw): in_stream = StringIO(tmpl_data) out_stream = StringIO() template = Template(in_stream, out_stream) - expressions = template.get_all_user_python_expression() - py_expression = template.convert_py3o_to_python_ast(expressions) - convertor = Py3oConvertor() - data_struct = convertor(py_expression) + localcontext = parser_instance.localcontext + if report_xml.py3o_is_local_fusion: + template.render(localcontext) + input = out_stream.getvalue() + else: + expressions = template.get_all_user_python_expression() + py_expression = template.convert_py3o_to_python_ast(expressions) + convertor = Py3oConvertor() + data_struct = convertor(py_expression) + input = data_struct.render(localcontext) filetype = report_xml.py3o_fusion_filetype - - datadict = parser_instance.localcontext - - parsed_datadict = data_struct.render(datadict) - - fusion_server_obj = pool.get('py3o.server') - fusion_server_ids = fusion_server_obj.search( - cr, uid, [('is_active', '=', True)], context=context, limit=1 - ) - if not fusion_server_ids: - if filetype.fusion_ext == report_xml.py3o_template_id.filetype: - # No format conversion is needed, render the template directly - template.render(parsed_datadict) - res = out_stream.getvalue() - else: - raise exceptions.MissingError( - _(u"No Py3o server configuration found") - ) - + is_native = Formats().get_format(filetype) + if is_native: + res = input else: # Call py3o.server to render the template in the desired format - fusion_server_id = fusion_server_ids[0] - - fusion_server = fusion_server_obj.browse( - cr, uid, fusion_server_id, context=context - ) in_stream.seek(0) files = { 'tmpl_file': in_stream, } fields = { "targetformat": filetype.fusion_ext, - "datadict": json.dumps(parsed_datadict), + "datadict": json.dumps(input), "image_mapping": "{}", } - r = requests.post(fusion_server.url, data=fields, files=files) + r = requests.post( + report_xml.py3o_server_id.url, data=fields, files=files) if r.status_code != 200: # server says we have an issue... let's tell that to enduser raise exceptions.Warning( @@ -212,7 +199,7 @@ class Py3oParser(report_sxw): # ... but odoo wants the whole data in memory anyways :) res = fd.read() - return res, filetype.human_ext + return res, "." + filetype def create(self, cr, uid, ids, data, context=None): """ Override this function to handle our py3o report diff --git a/report_py3o/views/ir_report.xml b/report_py3o/views/ir_report.xml index 1fbb52c4..e3469464 100644 --- a/report_py3o/views/ir_report.xml +++ b/report_py3o/views/ir_report.xml @@ -15,6 +15,8 @@ + +