Browse Source

Now we call fusion server just to convert reports into the right format

pull/80/head
Anael Lorimier 10 years ago
parent
commit
483dbe7acc
  1. 3
      __init__.py
  2. 6
      __openerp__.py
  3. 49
      ir_report.py
  4. 8
      ir_report.xml
  5. 35
      py3o_report.py

3
__init__.py

@ -1,2 +1,5 @@
import ir_report import ir_report
import py3o_report import py3o_report
import py3o_template
import py3o_server
import py3o_fusion_filetype

6
__openerp__.py

@ -16,10 +16,14 @@ The py3o.template package is required; install it with:
'base' 'base'
], ],
'external_dependencies': { 'external_dependencies': {
'python': ['py3o.template']
'python': ['py3o.template', 'oe_json_serializer']
}, },
'data': [ 'data': [
'menu.xml',
'ir_report.xml', 'ir_report.xml',
'py3o_template.xml',
'py3o_server.xml',
'data/py3o.fusion.filetype.csv',
], ],
'installable': True, 'installable': True,
} }

49
ir_report.py

@ -1,46 +1,21 @@
from base64 import b64encode
from openerp import addons
from openerp.osv import fields, osv from openerp.osv import fields, osv
from openerp.tools.translate import _
class report_xml(osv.osv):
class report_xml(osv.Model):
''' Inherit from ir.actions.report.xml to allow customizing the template ''' 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. '''
file. The user cam chose a template from a list.
The list is configurable in the configuration tab, see py3o_template.py
'''
_inherit = 'ir.actions.report.xml' _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 or
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 = { _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')),
'py3o_fusion_filetype': fields.many2one(
'py3o.fusion.filetype',
u"Output Format",
),
'py3o_template_id': fields.many2one(
'py3o.template',
u"Template",
),
} }

8
ir_report.xml

@ -13,10 +13,10 @@
<page string="LibreOffice template" <page string="LibreOffice template"
attrs="{'invisible': [('report_type', '!=', 'py3o')]}"> attrs="{'invisible': [('report_type', '!=', 'py3o')]}">
<field name="py3o_filename" invisible="1" />
<field name="py3o_template" filename="py3o_filename"
attrs="{'invisible': [('py3o_template_data', '!=', False)]}" />
<field name="py3o_template_data" filename="py3o_filename" />
<group>
<field name="py3o_fusion_filetype" />
<field name="py3o_template_id" />
</group>
</page> </page>
</xpath> </xpath>

35
py3o_report.py

@ -8,6 +8,8 @@ from openerp.osv.osv import except_osv
from py3o.template import Template from py3o.template import Template
import requests
class py3o_report(report_sxw): class py3o_report(report_sxw):
# def __init__(self, name, table): # def __init__(self, name, table):
@ -52,13 +54,16 @@ class py3o_report(report_sxw):
report_xml_ids[0], report_xml_ids[0],
context=context) context=context)
template = report_xml.py3o_template_id
filetype = report_xml.py3o_fusion_filetype
# py3o.template operates on filenames so create temporary files. # py3o.template operates on filenames so create temporary files.
with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as \ with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as \
in_temp, \ in_temp, \
NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \ NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \
out_temp: out_temp:
in_temp.write(b64decode(report_xml.py3o_template))
in_temp.write(b64decode(template.py3o_template_data))
in_temp.flush() in_temp.flush()
template = Template(in_temp.name, out_temp.name) template = Template(in_temp.name, out_temp.name)
@ -66,6 +71,34 @@ class py3o_report(report_sxw):
template.render(self.get_values(cr, uid, ids, data, context)) template.render(self.get_values(cr, uid, ids, data, context))
out_temp.seek(0) out_temp.seek(0)
if filetype.human_ext != 'odt':
# Now we ask fusion server to convert our template
fusion_server_obj = pool['py3o.server']
fusion_server_id = fusion_server_obj.search(
cr, uid, [], context=context
)[0]
fusion_server = fusion_server_obj.browse(
cr, uid, fusion_server_id, context=context
)
files = {
'tmpl_file': out_temp,
}
fields = {
"targetformat": filetype.fusion_ext,
"datadict": "{}",
"image_mapping": "{}",
}
r = requests.post(fusion_server.url, data=fields, files=files)
chunk_size = 1024
with NamedTemporaryFile(
suffix=filetype.human_ext,
prefix='py3o-template-') as fd:
for chunk in r.iter_content(chunk_size):
fd.write(chunk)
fd.seek(0)
return fd.read(), filetype.human_ext
return out_temp.read(), 'odt' return out_temp.read(), 'odt'
return False, False return False, False
Loading…
Cancel
Save