Added report extender + demo and doc
bzr revid: nicolas.vanhoren@openerp.com-20130226153047-0qzs21v7xiybrjmj
This commit is contained in:
parent
49b1fa4b52
commit
048ec2774a
|
@ -36,5 +36,6 @@ import webkit_report
|
||||||
import ir_report
|
import ir_report
|
||||||
import wizard
|
import wizard
|
||||||
import convert
|
import convert
|
||||||
|
import report
|
||||||
|
|
||||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
import webkit_report_demo
|
|
@ -11,6 +11,7 @@
|
||||||
% endfor
|
% endfor
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<p>The administrator name is: ${admin_name}</p>
|
||||||
<p>If this report does not contain headers, it is because you have a badly compiled wkhtmltopdf. Consider installing
|
<p>If this report does not contain headers, it is because you have a badly compiled wkhtmltopdf. Consider installing
|
||||||
the static version distributed on the official web site: <a href="https://code.google.com/p/wkhtmltopdf/">https://code.google.com/p/wkhtmltopdf/</a>.</p>
|
the static version distributed on the official web site: <a href="https://code.google.com/p/wkhtmltopdf/">https://code.google.com/p/wkhtmltopdf/</a>.</p>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
|
||||||
|
from openerp.addons.report_webkit.webkit_report import webkit_report_extender
|
||||||
|
from openerp import SUPERUSER_ID
|
||||||
|
|
||||||
|
@webkit_report_extender("report_webkit.webkit_demo_report")
|
||||||
|
def extend_demo(pool, cr, uid, localcontext, context):
|
||||||
|
admin = pool.get("res.users").browse(cr, uid, SUPERUSER_ID, context)
|
||||||
|
localcontext.update({
|
||||||
|
"admin_name": admin.name,
|
||||||
|
})
|
|
@ -87,6 +87,32 @@ def mako_template(text):
|
||||||
|
|
||||||
return mako_template_env.from_string(text)
|
return mako_template_env.from_string(text)
|
||||||
|
|
||||||
|
_extender_functions = {}
|
||||||
|
|
||||||
|
def webkit_report_extender(report_name):
|
||||||
|
"""
|
||||||
|
A decorator to define functions to extend the context used in a template rendering.
|
||||||
|
report_name must be the xml id of the desired report (it is mandatory to indicate the
|
||||||
|
module in that xml id).
|
||||||
|
|
||||||
|
The given function will be called at the creation of the report. The following arguments
|
||||||
|
will be passed to it (in this order):
|
||||||
|
- pool The model pool.
|
||||||
|
- cr The cursor.
|
||||||
|
- uid The user id.
|
||||||
|
- localcontext The context given to the template engine to render the templates for the
|
||||||
|
current report. This is the context that should be modified.
|
||||||
|
- context The OpenERP context.
|
||||||
|
"""
|
||||||
|
def fct1(fct):
|
||||||
|
lst = _extender_functions.get(report_name)
|
||||||
|
if not lst:
|
||||||
|
lst = []
|
||||||
|
_extender_functions[report_name] = lst
|
||||||
|
lst.append(fct)
|
||||||
|
return fct
|
||||||
|
return fct1
|
||||||
|
|
||||||
class WebKitParser(report_sxw):
|
class WebKitParser(report_sxw):
|
||||||
"""Custom class that use webkit to render HTML reports
|
"""Custom class that use webkit to render HTML reports
|
||||||
Code partially taken from report openoffice. Thanks guys :)
|
Code partially taken from report openoffice. Thanks guys :)
|
||||||
|
@ -227,6 +253,17 @@ class WebKitParser(report_sxw):
|
||||||
def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
|
def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
|
||||||
"""generate the PDF"""
|
"""generate the PDF"""
|
||||||
|
|
||||||
|
# just try to find an xml id for the report
|
||||||
|
cr = cursor
|
||||||
|
import openerp.pooler as pooler
|
||||||
|
pool = pooler.get_pool(cr.dbname)
|
||||||
|
found_xml_ids = pool.get("ir.model.data").search(cr, uid, [["model", "=", "ir.actions.report.xml"], \
|
||||||
|
["res_id", "=", report_xml.id]], context=context)
|
||||||
|
xml_id = None
|
||||||
|
if found_xml_ids:
|
||||||
|
xml_id = pool.get("ir.model.data").read(cr, uid, found_xml_ids[0], ["module", "name"])
|
||||||
|
xml_id = "%s.%s" % (xml_id["module"], xml_id["name"])
|
||||||
|
|
||||||
if context is None:
|
if context is None:
|
||||||
context={}
|
context={}
|
||||||
htmls = []
|
htmls = []
|
||||||
|
@ -270,14 +307,22 @@ class WebKitParser(report_sxw):
|
||||||
|
|
||||||
body_mako_tpl = mako_template(template)
|
body_mako_tpl = mako_template(template)
|
||||||
helper = WebKitHelper(cursor, uid, report_xml.id, context)
|
helper = WebKitHelper(cursor, uid, report_xml.id, context)
|
||||||
|
self.parser_instance.localcontext['helper'] = helper
|
||||||
|
self.parser_instance.localcontext['css'] = css
|
||||||
|
self.parser_instance.localcontext['_'] = self.translate_call
|
||||||
|
|
||||||
|
# apply extender functions
|
||||||
|
additional = {}
|
||||||
|
if xml_id in _extender_functions:
|
||||||
|
for fct in _extender_functions[xml_id]:
|
||||||
|
fct(pool, cr, uid, self.parser_instance.localcontext, context)
|
||||||
|
|
||||||
if report_xml.precise_mode:
|
if report_xml.precise_mode:
|
||||||
for obj in objs:
|
ctx = dict(self.parser_instance.localcontext)
|
||||||
self.parser_instance.localcontext['objects'] = [obj]
|
for obj in self.parser_instance.localcontext['objects']:
|
||||||
|
ctx['objects'] = [obj]
|
||||||
try :
|
try :
|
||||||
html = body_mako_tpl.render(dict({"helper":helper,
|
html = body_mako_tpl.render(dict(ctx))
|
||||||
"css":css,
|
|
||||||
"_":self.translate_call},
|
|
||||||
**self.parser_instance.localcontext))
|
|
||||||
htmls.append(html)
|
htmls.append(html)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
msg = u"%s" % e
|
msg = u"%s" % e
|
||||||
|
@ -285,10 +330,7 @@ class WebKitParser(report_sxw):
|
||||||
raise except_osv(_('Webkit render!'), msg)
|
raise except_osv(_('Webkit render!'), msg)
|
||||||
else:
|
else:
|
||||||
try :
|
try :
|
||||||
html = body_mako_tpl.render(dict({"helper":helper,
|
html = body_mako_tpl.render(dict(self.parser_instance.localcontext))
|
||||||
"css":css,
|
|
||||||
"_":self.translate_call},
|
|
||||||
**self.parser_instance.localcontext))
|
|
||||||
htmls.append(html)
|
htmls.append(html)
|
||||||
except Exception:
|
except Exception:
|
||||||
msg = u"%s" % e
|
msg = u"%s" % e
|
||||||
|
@ -296,20 +338,14 @@ class WebKitParser(report_sxw):
|
||||||
raise except_osv(_('Webkit render!'), msg)
|
raise except_osv(_('Webkit render!'), msg)
|
||||||
head_mako_tpl = mako_template(header)
|
head_mako_tpl = mako_template(header)
|
||||||
try :
|
try :
|
||||||
head = head_mako_tpl.render(dict({"helper":helper,
|
head = head_mako_tpl.render(dict(self.parser_instance.localcontext, _debug=False))
|
||||||
"css":css,
|
|
||||||
"_":self.translate_call,
|
|
||||||
"_debug":False},
|
|
||||||
**self.parser_instance.localcontext))
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
raise except_osv(_('Webkit render!'), u"%s" % e)
|
raise except_osv(_('Webkit render!'), u"%s" % e)
|
||||||
foot = False
|
foot = False
|
||||||
if footer :
|
if footer :
|
||||||
foot_mako_tpl = mako_template(footer)
|
foot_mako_tpl = mako_template(footer)
|
||||||
try :
|
try :
|
||||||
foot = foot_mako_tpl.render(dict({"helper":helper,
|
foot = foot_mako_tpl.render(dict({},
|
||||||
"css":css,
|
|
||||||
"_":self.translate_call},
|
|
||||||
**self.parser_instance.localcontext))
|
**self.parser_instance.localcontext))
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
msg = u"%s" % e
|
msg = u"%s" % e
|
||||||
|
@ -317,11 +353,7 @@ class WebKitParser(report_sxw):
|
||||||
raise except_osv(_('Webkit render!'), msg)
|
raise except_osv(_('Webkit render!'), msg)
|
||||||
if report_xml.webkit_debug :
|
if report_xml.webkit_debug :
|
||||||
try :
|
try :
|
||||||
deb = head_mako_tpl.render(dict({"helper":helper,
|
deb = head_mako_tpl.render(dict(self.parser_instance.localcontext, _debug=tools.ustr("\n".join(htmls))))
|
||||||
"css":css,
|
|
||||||
"_":self.translate_call,
|
|
||||||
"_debug":tools.ustr("\n".join(htmls))},
|
|
||||||
**self.parser_instance.localcontext))
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
msg = u"%s" % e
|
msg = u"%s" % e
|
||||||
_logger.error(msg)
|
_logger.error(msg)
|
||||||
|
|
Loading…
Reference in New Issue