Browse Source

[IMP] Allow user to use local fusion

pull/258/head
Laurent Mignon 8 years ago
committed by Laurent Mignon (ACSONE)
parent
commit
744fb50808
  1. 5
      report_py3o/__openerp__.py
  2. 6
      report_py3o/data/py3o.fusion.filetype.csv
  3. 1
      report_py3o/models/__init__.py
  4. 37
      report_py3o/models/ir_report.py
  5. 13
      report_py3o/models/py3o_fusion_filetype.py
  6. 61
      report_py3o/py3o_parser.py
  7. 2
      report_py3o/views/ir_report.xml

5
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,
}

6
report_py3o/data/py3o.fusion.filetype.csv

@ -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

1
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

37
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")

13
report_py3o/models/py3o_fusion_filetype.py

@ -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)

61
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

2
report_py3o/views/ir_report.xml

@ -15,6 +15,8 @@
<group>
<field name="py3o_fusion_filetype" />
<field name="py3o_is_local_fusion"/>
<field name="py3o_server_id" />
<field name="py3o_template_id" />
<field name="module" />
<field name="py3o_template_fallback" />

Loading…
Cancel
Save