Browse Source

Merge pull request #5 from acsone/9.0-import_report_py3o

9.0 import report py3o
pull/80/head
Florent Aide 8 years ago
committed by GitHub
parent
commit
36c546e0ec
  1. 14
      report_py3o/models/ir_report.py
  2. 71
      report_py3o/py3o_parser.py
  3. 4
      report_py3o/security/ir.model.access.csv

14
report_py3o/models/ir_report.py

@ -3,7 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import os import os
from py3o.formats import Formats from py3o.formats import Formats
from openerp import api, fields, models
from openerp import api, fields, models, _
from openerp.report.interface import report_int from openerp.report.interface import report_int
from openerp.exceptions import ValidationError from openerp.exceptions import ValidationError
from openerp import addons from openerp import addons
@ -42,7 +42,10 @@ class ReportXml(models.Model):
names = formats.get_known_format_names() names = formats.get_known_format_names()
selections = [] selections = []
for name in names: for name in names:
selections.append((name, name))
description = name
if formats.get_format(name).native:
description = description + " " + _("(Native)")
selections.append((name, description))
return selections return selections
py3o_fusion_filetype = fields.Selection( py3o_fusion_filetype = fields.Selection(
@ -53,9 +56,10 @@ class ReportXml(models.Model):
"Template") "Template")
py3o_is_local_fusion = fields.Boolean( py3o_is_local_fusion = fields.Boolean(
"Local fusion", "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)
help="Native formats will be processed without a server. "
"You must use this mode if you call methods on your model into "
"the template.",
default=True)
py3o_server_id = fields.Many2one( py3o_server_id = fields.Many2one(
"py3o.server" "py3o.server"
"Fusion server") "Fusion server")

71
report_py3o/py3o_parser.py

@ -15,7 +15,7 @@ from py3o.formats import Formats
from openerp import _ from openerp import _
from openerp import exceptions from openerp import exceptions
from openerp.report.report_sxw import report_sxw, rml_parse
from openerp.report.report_sxw import report_sxw
from openerp import registry from openerp import registry
@ -26,44 +26,39 @@ class TemplateNotFound(Exception):
pass pass
def py3o_report_extender(report_name):
def py3o_report_extender(report_xml_id=None):
""" """
A decorator to define function to extend the context sent to a template. A decorator to define function to extend the context sent to a template.
This will be called at the creation of the report. This will be called at the creation of the report.
The following arguments will be passed to it: The following arguments will be passed to it:
- pool: the model pool
- cr: the database cursor
- uid: the id of the user that call the renderer
- ir_report: report instance
- localcontext: The context that will be passed to the report engine - localcontext: The context that will be passed to the report engine
- context: the Odoo context
If no report_xml_id is given the extender is registered for all py3o
reports
Idea copied from CampToCamp report_webkit module.
Method copied from CampToCamp report_webkit module.
:param report_name: xml id of the report
:param report_xml_id: xml id of the report
:return: a decorated class :return: a decorated class
""" """
global _extender_functions
def fct1(fct): def fct1(fct):
lst = _extender_functions.get(report_name)
if not lst:
lst = []
_extender_functions[report_name] = lst
lst.append(fct)
_extender_functions.setdefault(report_xml_id, []).append(fct)
return fct return fct
return fct1 return fct1
@py3o_report_extender()
def defautl_extend(report_xml, localcontext):
# add the base64decode function to be able do decode binary fields into
# the template
localcontext['b64decode'] = b64decode
class Py3oParser(report_sxw): class Py3oParser(report_sxw):
"""Custom class that use Py3o to render libroffice reports. """Custom class that use Py3o to render libroffice reports.
Code partially taken from CampToCamp's webkit_report.""" Code partially taken from CampToCamp's webkit_report."""
def __init__(self, name, table, rml=False, parser=rml_parse,
header=False, store=False, register=True):
self.localcontext = {}
super(Py3oParser, self).__init__(
name, table, rml=rml, parser=parser,
header=header, store=store, register=register
)
def get_template(self, report_obj): def get_template(self, report_obj):
"""private helper to fetch the template data either from the database """private helper to fetch the template data either from the database
or from the default template file provided by the implementer. or from the default template file provided by the implementer.
@ -115,6 +110,16 @@ class Py3oParser(report_sxw):
return tmpl_data return tmpl_data
def _extend_parser_context(self, parser_instance, report_xml):
# add default extenders
for fct in _extender_functions.get(None, []):
fct(report_xml, parser_instance.localcontext)
# add extenders for registered on the template
xml_id = report_xml.get_external_id().get(report_xml.id)
if xml_id in _extender_functions:
for fct in _extender_functions[xml_id]:
fct(report_xml, parser_instance.localcontext)
def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None): def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
""" Overide this function to generate our py3o report """ Overide this function to generate our py3o report
""" """
@ -123,30 +128,12 @@ class Py3oParser(report_sxw):
cr, uid, ids, data, report_xml, context=context cr, uid, ids, data, report_xml, context=context
) )
pool = registry(cr.dbname)
model_data_ids = pool['ir.model.data'].search(
cr, uid, [
('model', '=', 'ir.actions.report.xml'),
('res_id', '=', report_xml.id),
]
)
xml_id = None
if model_data_ids:
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)
parser_instance = self.parser(cr, uid, self.name2, context=context) parser_instance = self.parser(cr, uid, self.name2, context=context)
parser_instance.set_context( parser_instance.set_context(
self.getObjects(cr, uid, ids, context), self.getObjects(cr, uid, ids, context),
data, ids, report_xml.report_type data, ids, report_xml.report_type
) )
if xml_id in _extender_functions:
for fct in _extender_functions[xml_id]:
fct(pool, cr, uid, parser_instance.localcontext, context)
self._extend_parser_context(parser_instance, report_xml)
tmpl_data = self.get_template(report_xml) tmpl_data = self.get_template(report_xml)
@ -165,7 +152,7 @@ class Py3oParser(report_sxw):
input = data_struct.render(localcontext) input = data_struct.render(localcontext)
filetype = report_xml.py3o_fusion_filetype filetype = report_xml.py3o_fusion_filetype
is_native = Formats().get_format(filetype)
is_native = Formats().get_format(filetype).native
if is_native: if is_native:
res = input res = input
else: # Call py3o.server to render the template in the desired format else: # Call py3o.server to render the template in the desired format

4
report_py3o/security/ir.model.access.csv

@ -2,6 +2,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_py3o_template_admin,access_py3o_template_admin,model_py3o_template,base.group_no_one,1,1,1,1 access_py3o_template_admin,access_py3o_template_admin,model_py3o_template,base.group_no_one,1,1,1,1
access_py3o_template_user,access_py3o_template_user,model_py3o_template,base.group_user,1,0,0,0 access_py3o_template_user,access_py3o_template_user,model_py3o_template,base.group_user,1,0,0,0
access_py3o_server_admin,access_py3o_server_admin,model_py3o_server,base.group_no_one,1,1,1,1 access_py3o_server_admin,access_py3o_server_admin,model_py3o_server,base.group_no_one,1,1,1,1
access_py3o_server_user,access_py3o_server_user,model_py3o_server,base.group_user,1,0,0,0
access_py3o_fusion_filetype_admin,access_py3o_fusion_filetype_admin,model_py3o_fusion_filetype,base.group_no_one,1,1,1,1
access_py3o_fusion_filetype_user,access_py3o_fusion_filetype_user,model_py3o_fusion_filetype,base.group_user,1,0,0,0
access_py3o_server_user,access_py3o_server_user,model_py3o_server,base.group_user,1,0,0,0
Loading…
Cancel
Save