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.

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