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.

96 lines
3.6 KiB

  1. from base64 import b64decode
  2. from tempfile import NamedTemporaryFile
  3. from openerp.report.report_sxw import *
  4. from py3o.template import Template
  5. import requests
  6. class py3o_report(report_sxw):
  7. # def __init__(self, name, table):
  8. # super(py3o_report, self).__init__(name, table)
  9. def get_values(self, cr, uid, ids, data, context):
  10. """ Override this function to customize the dictionary given to the
  11. py3o.template renderer. """
  12. return {
  13. 'lang': self.get_lang(cr, uid, context),
  14. 'objects': self.getObjects(cr, uid, ids, context),
  15. }
  16. @staticmethod
  17. def get_lang(cr, uid, context):
  18. pool = pooler.get_pool(cr.dbname)
  19. lang_obj = pool.get('res.lang')
  20. user_obj = pool.get('res.users')
  21. lang_code = user_obj.browse(cr, uid, uid, context=context).lang
  22. lang = lang_obj.search(cr, uid,
  23. [('code', '=', lang_code)],
  24. context=context)[0]
  25. return lang_obj.browse(cr, uid, lang, context=context)
  26. @staticmethod
  27. def format_date(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. template = report_xml.py3o_template_id
  45. filetype = report_xml.py3o_fusion_filetype
  46. # py3o.template operates on filenames so create temporary files.
  47. with NamedTemporaryFile(suffix='.odt', prefix='py3o-template-') as in_temp:
  48. in_temp.write(b64decode(template.py3o_template_data))
  49. in_temp.flush()
  50. in_temp.seek(0)
  51. template = Template(in_temp.name, None)
  52. user_instruction_mapping = template.get_user_instructions_mapping()
  53. values = user_instruction_mapping.jsonify(
  54. self.get_values(cr, uid, ids, data, context)
  55. )
  56. fusion_server_obj = pool['py3o.server']
  57. fusion_server_id = fusion_server_obj.search(
  58. cr, uid, [], context=context
  59. )[0]
  60. fusion_server = fusion_server_obj.browse(
  61. cr, uid, fusion_server_id, context=context
  62. )
  63. files = {
  64. 'tmpl_file': in_temp,
  65. }
  66. fields = {
  67. "targetformat": filetype.fusion_ext,
  68. "datadict": values,
  69. "image_mapping": "{}",
  70. }
  71. r = requests.post(fusion_server.url, data=fields, files=files)
  72. chunk_size = 1024
  73. with NamedTemporaryFile(
  74. suffix=filetype.human_ext,
  75. prefix='py3o-template-') as fd:
  76. for chunk in r.iter_content(chunk_size):
  77. fd.write(chunk)
  78. fd.seek(0)
  79. return fd.read(), filetype.human_ext