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.

101 lines
3.3 KiB

  1. from openerp.report.report_sxw import report_sxw, rml_parse
  2. from openerp import registry
  3. _extender_functions = {}
  4. def py3o_report_extender(report_name):
  5. """
  6. A decorator to define function to extend the context sent to a template.
  7. This will be called at the creation of the report.
  8. The following arguments will be passed to it:
  9. - pool: the model pool
  10. - cr: the database cursor
  11. - uid: the id of the user that call the renderer
  12. - localcontext: The context that will be passed to the report engine
  13. - context: the Odoo context
  14. Method copied from CampToCamp report_webkit module.
  15. :param report_name: xml id of the report
  16. :return:
  17. """
  18. def fct1(fct):
  19. lst = _extender_functions.get(report_name)
  20. if not lst:
  21. lst = []
  22. _extender_functions[report_name] = lst
  23. lst.append(fct)
  24. return fct
  25. return fct1
  26. class Py3oParser(report_sxw):
  27. """Custom class that use Py3o to render libroffice reports.
  28. Code partially taken from CampToCamp."""
  29. def __init__(self, name, table, rml=False, parser=rml_parse,
  30. header=False, store=False, register=True):
  31. self.localcontext = {}
  32. super(Py3oParser, self).__init__(
  33. name, table, rml=rml, parser=parser,
  34. header=header, store=store, register=register
  35. )
  36. def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
  37. """ Overide this function to generate our py3o report
  38. """
  39. if report_xml.report_type != 'py3o':
  40. return super(Py3oParser, self).create_single_pdf(
  41. cr, uid, ids, data, report_xml, context=context
  42. )
  43. pool = registry(cr.dbname)
  44. model_data_ids = pool['ir.model.data'].search(
  45. cr, uid, [
  46. ('model', '=', 'ir.actions.report.xml'),
  47. ('res_id', '=', report_xml.id),
  48. ]
  49. )
  50. xml_id = None
  51. if model_data_ids:
  52. model_data = pool['ir.model.data'].browse(
  53. cr, uid, model_data_ids[0], context=context
  54. )
  55. xml_id = '%s,%s' % (model_data.module, model_data.name)
  56. parser_instance = self.parser(cr, uid, self.name2, context=context)
  57. parser_instance.set_context(
  58. self.getObjects(cr, uid, ids, context),
  59. data, ids, report_xml.report_type
  60. )
  61. if xml_id in _extender_functions:
  62. for fct in _extender_functions[xml_id]:
  63. pass
  64. def create(self, cr, uid, ids, data, context=None):
  65. """ Override this function to handle our py3o report
  66. """
  67. pool = registry(cr.dbname)
  68. ir_action_report_obj = pool['ir.actions.report.xml']
  69. report_xml_ids = ir_action_report_obj.search(
  70. cr, uid, [('report_name', '=', self.name[7:])], context=context
  71. )
  72. if not report_xml_ids:
  73. return super(Py3oParser, self).create(
  74. cr, uid, ids, data, context=context
  75. )
  76. report_xml = ir_action_report_obj.browse(
  77. cr, uid, report_xml_ids[0], context=context
  78. )
  79. result = self.create_source_pdf(
  80. cr, uid, ids, data, report_xml, context
  81. )
  82. if not result:
  83. return False, False
  84. return result