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.

115 lines
4.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 XCG Consulting (http://odoo.consulting)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import os
  5. from py3o.formats import Formats
  6. from openerp import api, fields, models
  7. from openerp.report.interface import report_int
  8. from openerp.exceptions import ValidationError
  9. from openerp import addons
  10. from ..py3o_parser import Py3oParser
  11. class ReportXml(models.Model):
  12. """ Inherit from ir.actions.report.xml to allow customizing the template
  13. file. The user cam chose a template from a list.
  14. The list is configurable in the configuration tab, see py3o_template.py
  15. """
  16. _inherit = 'ir.actions.report.xml'
  17. @api.one
  18. @api.constrains("py3o_fusion_filetype", "report_type")
  19. def _check_py3o_fusion_filetype(self):
  20. if self.report_type == "py3o" and not self.py3o_fusion_filetype:
  21. raise ValidationError(
  22. "Field 'Output Format' is required for Py3O report")
  23. @api.one
  24. @api.constrains("py3o_is_local_fusion", "py3o_server_id",
  25. "py3o_fusion_filetype")
  26. def _check_py3o_server_id(self):
  27. is_native = Formats().get_format(self.py3o_fusion_filetype)
  28. if ((not is_native or not self.py3o_is_local_fusion) and
  29. not self.py3o_server_id):
  30. raise ValidationError(
  31. "Can not use not native format in local fusion. "
  32. "Please specify a Fusion Server")
  33. @api.model
  34. def _get_py3o_fusion_filetypes(self):
  35. formats = Formats()
  36. names = formats.get_known_format_names()
  37. selections = []
  38. for name in names:
  39. selections.append((name, name))
  40. return selections
  41. py3o_fusion_filetype = fields.Selection(
  42. selection="_get_py3o_fusion_filetypes",
  43. string="Output Format")
  44. py3o_template_id = fields.Many2one(
  45. 'py3o.template',
  46. "Template")
  47. py3o_is_local_fusion = fields.Boolean(
  48. "Local fusion",
  49. help="Odt to Odt will be processed without sever. You must use this "
  50. "mode if you call methods on your model into the template.",
  51. default=False)
  52. py3o_server_id = fields.Many2one(
  53. "py3o.server"
  54. "Fusion server")
  55. module = fields.Char(
  56. "Module",
  57. help="The implementer module that provides this report")
  58. py3o_template_fallback = fields.Char(
  59. "Fallback",
  60. size=128,
  61. help=(
  62. "If the user does not provide a template this will be used "
  63. "it should be a relative path to root of YOUR module "
  64. "or an absolute path on your server."
  65. ))
  66. report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
  67. @api.cr
  68. def _lookup_report(self, cr, name):
  69. """Look up a report definition.
  70. """
  71. # First lookup in the deprecated place, because if the report
  72. # definition has not been updated, it is more likely the correct
  73. # definition is there. Only reports with custom parser
  74. # specified in Python are still there.
  75. if 'report.' + name in report_int._reports:
  76. new_report = report_int._reports['report.' + name]
  77. if not isinstance(new_report, Py3oParser):
  78. new_report = None
  79. else:
  80. cr.execute(
  81. 'SELECT * '
  82. 'FROM ir_act_report_xml '
  83. 'WHERE report_name=%s AND report_type=%s',
  84. (name, 'py3o')
  85. )
  86. r = cr.dictfetchone()
  87. if r:
  88. kwargs = {}
  89. if r['parser']:
  90. kwargs['parser'] = getattr(addons, r['parser'])
  91. new_report = Py3oParser(
  92. 'report.' + r['report_name'],
  93. r['model'],
  94. os.path.join('addons', r['report_rml'] or '/'),
  95. header=r['header'],
  96. register=False,
  97. **kwargs
  98. )
  99. else:
  100. new_report = None
  101. if new_report:
  102. return new_report
  103. else:
  104. return super(ReportXml, self)._lookup_report(cr, name)