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.

127 lines
4.6 KiB

8 years ago
  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. import logging
  6. from odoo import api, fields, models, _
  7. from odoo.report.interface import report_int
  8. from odoo.exceptions import ValidationError
  9. from odoo import addons
  10. from ..py3o_parser import Py3oParser
  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. if self.report_type != "py3o":
  33. return
  34. is_native = Formats().get_format(self.py3o_filetype).native
  35. if ((not is_native or not self.py3o_is_local_fusion) and
  36. not self.py3o_server_id):
  37. raise ValidationError(_(
  38. "Can not use not native format in local fusion. "
  39. "Please specify a Fusion Server"))
  40. @api.model
  41. def _get_py3o_filetypes(self):
  42. formats = Formats()
  43. names = formats.get_known_format_names()
  44. selections = []
  45. for name in names:
  46. description = name
  47. if formats.get_format(name).native:
  48. description = description + " " + _("(Native)")
  49. selections.append((name, description))
  50. return selections
  51. py3o_filetype = fields.Selection(
  52. selection="_get_py3o_filetypes",
  53. string="Output Format")
  54. py3o_template_id = fields.Many2one(
  55. 'py3o.template',
  56. "Template")
  57. py3o_is_local_fusion = fields.Boolean(
  58. "Local Fusion",
  59. help="Native formats will be processed without a server. "
  60. "You must use this mode if you call methods on your model into "
  61. "the template.",
  62. default=True)
  63. py3o_server_id = fields.Many2one(
  64. "py3o.server",
  65. "Fusion Server")
  66. module = fields.Char(
  67. "Module",
  68. help="The implementer module that provides this report")
  69. py3o_template_fallback = fields.Char(
  70. "Fallback",
  71. size=128,
  72. help=(
  73. "If the user does not provide a template this will be used "
  74. "it should be a relative path to root of YOUR module "
  75. "or an absolute path on your server."
  76. ))
  77. report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
  78. @api.model_cr
  79. def _lookup_report(self, name):
  80. """Look up a report definition.
  81. """
  82. # START section copied from odoo/addons/base/ir/ir_actions.py
  83. # with small adaptations
  84. # First lookup in the deprecated place, because if the report
  85. # definition has not been updated, it is more likely the correct
  86. # definition is there. Only reports with custom parser
  87. # specified in Python are still there.
  88. if 'report.' + name in report_int._reports:
  89. new_report = report_int._reports['report.' + name]
  90. if not isinstance(new_report, Py3oParser):
  91. new_report = None
  92. else:
  93. self._cr.execute(
  94. "SELECT * FROM ir_act_report_xml "
  95. "WHERE report_name=%s AND report_type=%s", (name, 'py3o'))
  96. report_data = self._cr.dictfetchone()
  97. # END section copied from odoo/addons/base/ir/ir_actions.py
  98. if report_data:
  99. kwargs = {}
  100. if report_data['parser']:
  101. kwargs['parser'] = getattr(addons, report_data['parser'])
  102. new_report = Py3oParser(
  103. 'report.' + report_data['report_name'],
  104. report_data['model'],
  105. os.path.join('addons', report_data['report_rml'] or '/'),
  106. header=report_data['header'],
  107. register=False,
  108. **kwargs
  109. )
  110. else:
  111. new_report = None
  112. if new_report:
  113. return new_report
  114. else:
  115. return super(IrActionsReportXml, self)._lookup_report(name)