diff --git a/__init__.py b/__init__.py index 739a4843..a6aad7f2 100644 --- a/__init__.py +++ b/__init__.py @@ -1 +1,2 @@ +import ir_report import py3o_report diff --git a/__openerp__.py b/__openerp__.py index 654f7e82..cacc7deb 100644 --- a/__openerp__.py +++ b/__openerp__.py @@ -16,6 +16,7 @@ The py3o.template package is required; install it with: 'base' ], 'data': [ + 'ir_report.xml', ], 'installable': True, } diff --git a/ir_report.py b/ir_report.py new file mode 100644 index 00000000..aedfb01c --- /dev/null +++ b/ir_report.py @@ -0,0 +1,46 @@ +from base64 import b64encode + +from openerp import addons +from openerp.osv import fields, osv +from openerp.tools.translate import _ + + +class report_xml(osv.osv): + ''' Inherit from ir.actions.report.xml to allow customizing the template + file. By default, the file defined when registering the report is used; + but the user can download / upload a new one. ''' + + _inherit = 'ir.actions.report.xml' + + def _get_filename(self, cr, uid, ids, field_name, arg, context): + return { + br.id: br.name + '.odt' + for br in self.browse(cr, uid, ids, context=context) + if br.report_type == 'py3o' + } + + def _get_template_data(self, cr, uid, ids, field_name, arg, context): + ''' Just return the data stored in the binary field, unless it is + empty; in that case, read the template file. ''' + + return { + br.id: (br.py3o_template_data if br.py3o_template_data + else b64encode(file(addons.get_module_resource( + *br.report_file.split('/')), 'rb').read())) + for br in self.browse(cr, uid, ids, context=context) + if br.report_type == 'py3o' + } + + _columns = { + 'py3o_filename': fields.function(_get_filename, + type='char', + method=True, + readonly=True), + + 'py3o_template': fields.function(_get_template_data, + type='binary', + method=True, + readonly=True), + + 'py3o_template_data': fields.binary(_('LibreOffice template')), + } diff --git a/ir_report.xml b/ir_report.xml new file mode 100644 index 00000000..fad7ceab --- /dev/null +++ b/ir_report.xml @@ -0,0 +1,26 @@ + + + + + + + py3o_report_view + ir.actions.report.xml + + + + + + + + + + + + + + + + + diff --git a/py3o_report.py b/py3o_report.py index 51b9eaa8..f84578ad 100644 --- a/py3o_report.py +++ b/py3o_report.py @@ -1,6 +1,7 @@ +from base64 import b64decode from tempfile import NamedTemporaryFile -from openerp import addons, pooler +from openerp import pooler from openerp.report.report_sxw import * from openerp.tools.translate import _ from openerp.osv.osv import except_osv @@ -46,19 +47,20 @@ class py3o_report(report_sxw): report_xml_ids[0], context=context) - # Get the template file. - template_path = addons.get_module_resource( - *report_xml.report_file.split('/')) + # py3o.template operates on filenames so create temporary files. + with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as \ + in_temp, \ + NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \ + out_temp: - # py3o.template operates on filenames so create a temporary file. - with NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \ - temp_file: + in_temp.write(b64decode(report_xml.py3o_template)) + in_temp.flush() - template = Template(template_path, temp_file.name) + template = Template(in_temp.name, out_temp.name) template.render(self.get_values(cr, uid, ids, data, context)) - temp_file.seek(0) - return temp_file.read(), 'odt' + out_temp.seek(0) + return out_temp.read(), 'odt' return False, False