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.

66 lines
2.4 KiB

  1. from base64 import b64decode
  2. from tempfile import NamedTemporaryFile
  3. from openerp import pooler
  4. from openerp.report.report_sxw import *
  5. from openerp.tools.translate import _
  6. from openerp.osv.osv import except_osv
  7. from py3o.template import Template
  8. class py3o_report(report_sxw):
  9. # def __init__(self, name, table):
  10. # super(py3o_report, self).__init__(name, table)
  11. def get_values(self, cr, uid, ids, data, context):
  12. ''' Override this function to customize the dictionary given to the
  13. py3o.template renderer. '''
  14. return {
  15. 'lang': self.get_lang(cr, uid, context),
  16. 'objects': self.getObjects(cr, uid, ids, context),
  17. }
  18. def get_lang(self, cr, uid, context):
  19. pool = pooler.get_pool(cr.dbname)
  20. lang_obj = pool.get('res.lang')
  21. user_obj = pool.get('res.users')
  22. lang_code = user_obj.browse(cr, uid, uid, context=context).lang
  23. lang = lang_obj.search(cr, uid,
  24. [('code', '=', lang_code)],
  25. context=context)[0]
  26. return lang_obj.browse(cr, uid, lang, context=context)
  27. def create(self, cr, uid, ids, data, context=None):
  28. # Find the report definition to get its settings.
  29. pool = pooler.get_pool(cr.dbname)
  30. report_xml_obj = pool.get('ir.actions.report.xml')
  31. report_xml_ids = report_xml_obj.search(cr, uid,
  32. [('report_name', '=', self.name[7:])], # Ignore "report."
  33. context=context)
  34. if not report_xml_ids:
  35. return super(py3o_report, self).create(cr, uid, ids, data,
  36. context=context)
  37. report_xml = report_xml_obj.browse(cr, uid,
  38. report_xml_ids[0],
  39. context=context)
  40. # py3o.template operates on filenames so create temporary files.
  41. with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as \
  42. in_temp, \
  43. NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \
  44. out_temp:
  45. in_temp.write(b64decode(report_xml.py3o_template))
  46. in_temp.flush()
  47. template = Template(in_temp.name, out_temp.name)
  48. template.render(self.get_values(cr, uid, ids, data, context))
  49. out_temp.seek(0)
  50. return out_temp.read(), 'odt'
  51. return False, False