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.

71 lines
2.7 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 format_date(self, date, values):
  28. ''' Return a date formatted according to the language extracted from
  29. the "values" argument (which should be the result of get_values). '''
  30. return date.strftime(values['lang'].date_format)
  31. def create(self, cr, uid, ids, data, context=None):
  32. # Find the report definition to get its settings.
  33. pool = pooler.get_pool(cr.dbname)
  34. report_xml_obj = pool.get('ir.actions.report.xml')
  35. report_xml_ids = report_xml_obj.search(cr, uid,
  36. [('report_name', '=', self.name[7:])], # Ignore "report."
  37. context=context)
  38. if not report_xml_ids:
  39. return super(py3o_report, self).create(cr, uid, ids, data,
  40. context=context)
  41. report_xml = report_xml_obj.browse(cr, uid,
  42. report_xml_ids[0],
  43. context=context)
  44. # py3o.template operates on filenames so create temporary files.
  45. with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as \
  46. in_temp, \
  47. NamedTemporaryFile(suffix='.odt', prefix='py3o-report-') as \
  48. out_temp:
  49. in_temp.write(b64decode(report_xml.py3o_template))
  50. in_temp.flush()
  51. template = Template(in_temp.name, out_temp.name)
  52. template.render(self.get_values(cr, uid, ids, data, context))
  53. out_temp.seek(0)
  54. return out_temp.read(), 'odt'
  55. return False, False