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.

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