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.

96 lines
3.4 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 logging
  5. from odoo import api, fields, models, _
  6. from odoo.report.interface import report_int
  7. from odoo.exceptions import ValidationError
  8. from odoo import addons
  9. logger = logging.getLogger(__name__)
  10. try:
  11. from py3o.formats import Formats
  12. except ImportError:
  13. logger.debug('Cannot import py3o.formats')
  14. class IrActionsReportXml(models.Model):
  15. """ Inherit from ir.actions.report.xml to allow customizing the template
  16. file. The user cam chose a template from a list.
  17. The list is configurable in the configuration tab, see py3o_template.py
  18. """
  19. _inherit = 'ir.actions.report.xml'
  20. @api.one
  21. @api.constrains("py3o_filetype", "report_type")
  22. def _check_py3o_filetype(self):
  23. if self.report_type == "py3o" and not self.py3o_filetype:
  24. raise ValidationError(_(
  25. "Field 'Output Format' is required for Py3O report"))
  26. @api.one
  27. @api.constrains("py3o_is_local_fusion", "py3o_server_id",
  28. "py3o_filetype")
  29. def _check_py3o_server_id(self):
  30. if self.report_type != "py3o":
  31. return
  32. is_native = Formats().get_format(self.py3o_filetype).native
  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.model
  77. def render_report(self, res_ids, name, data):
  78. action_py3o_report = self.search(
  79. [("report_name", "=", name),
  80. ("report_type", "=", "py3o")])
  81. if action_py3o_report:
  82. return self.env['py3o.report'].create({
  83. 'ir_actions_report_xml_id': action_py3o_report.id
  84. }).create_report(res_ids, data)
  85. return super(IrActionsReportXml, self).render_report(
  86. res_ids, name, data)