OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.8 KiB

  1. from tempfile import NamedTemporaryFile
  2. from openerp import addons, pooler
  3. from openerp.report.report_sxw import *
  4. from openerp.tools.translate import _
  5. from openerp.osv.osv import except_osv
  6. from py3o.template import Template
  7. class py3o_report(report_sxw):
  8. # def __init__(self, name, table):
  9. # super(py3o_report, self).__init__(name, table)
  10. def get_values(self, cr, uid, ids, data, context):
  11. ''' Override this function to customize the dictionary given to the
  12. py3o.template renderer. '''
  13. return {
  14. 'objects': self.getObjects(cr, uid, ids, context),
  15. }
  16. def create(self, cr, uid, ids, data, context=None):
  17. # Find the report definition to get its settings.
  18. pool = pooler.get_pool(cr.dbname)
  19. report_xml_obj = pool.get('ir.actions.report.xml')
  20. report_xml_ids = report_xml_obj.search(cr, uid,
  21. [('report_name', '=', self.name[7:])], # Ignore "report."
  22. context=context)
  23. if not report_xml_ids:
  24. return super(py3o_report, self).create(cr, uid, ids, data,
  25. context=context)
  26. report_xml = report_xml_obj.browse(cr, uid,
  27. report_xml_ids[0],
  28. context=context)
  29. # Get the template file.
  30. template_path = addons.get_module_resource(
  31. *report_xml.report_file.split('/'))
  32. # py3o.template operates on filenames so create a temporary file.
  33. with NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \
  34. temp_file:
  35. template = Template(template_path, temp_file.name)
  36. template.render(self.get_values(cr, uid, ids, data, context))
  37. temp_file.seek(0)
  38. return temp_file.read(), 'odt'
  39. return False, False