Add a simple way to extend the parser context
parent
decd731eb8
commit
c1e7bb3b07
|
@ -15,7 +15,7 @@ from py3o.formats import Formats
|
|||
|
||||
from openerp import _
|
||||
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
|
||||
|
||||
|
||||
|
@ -26,44 +26,39 @@ class TemplateNotFound(Exception):
|
|||
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.
|
||||
This will be called at the creation of the report.
|
||||
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
|
||||
- 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
|
||||
"""
|
||||
global _extender_functions
|
||||
|
||||
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 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):
|
||||
"""Custom class that use Py3o to render libroffice reports.
|
||||
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):
|
||||
"""private helper to fetch the template data either from the database
|
||||
or from the default template file provided by the implementer.
|
||||
|
@ -115,6 +110,16 @@ class Py3oParser(report_sxw):
|
|||
|
||||
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):
|
||||
""" Overide this function to generate our py3o report
|
||||
"""
|
||||
|
@ -123,30 +128,12 @@ class Py3oParser(report_sxw):
|
|||
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.set_context(
|
||||
self.getObjects(cr, uid, ids, context),
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue