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.

94 lines
3.3 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.exceptions import ValidationError
  7. logger = logging.getLogger(__name__)
  8. try:
  9. from py3o.formats import Formats
  10. except ImportError:
  11. logger.debug('Cannot import py3o.formats')
  12. class IrActionsReportXml(models.Model):
  13. """ Inherit from ir.actions.report.xml to allow customizing the template
  14. file. The user cam chose a template from a list.
  15. The list is configurable in the configuration tab, see py3o_template.py
  16. """
  17. _inherit = 'ir.actions.report.xml'
  18. @api.one
  19. @api.constrains("py3o_filetype", "report_type")
  20. def _check_py3o_filetype(self):
  21. if self.report_type == "py3o" and not self.py3o_filetype:
  22. raise ValidationError(_(
  23. "Field 'Output Format' is required for Py3O report"))
  24. @api.one
  25. @api.constrains("py3o_is_local_fusion", "py3o_server_id",
  26. "py3o_filetype")
  27. def _check_py3o_server_id(self):
  28. if self.report_type != "py3o":
  29. return
  30. is_native = Formats().get_format(self.py3o_filetype).native
  31. if ((not is_native or not self.py3o_is_local_fusion) and
  32. not self.py3o_server_id):
  33. raise ValidationError(_(
  34. "Can not use not native format in local fusion. "
  35. "Please specify a Fusion Server"))
  36. @api.model
  37. def _get_py3o_filetypes(self):
  38. formats = Formats()
  39. names = formats.get_known_format_names()
  40. selections = []
  41. for name in names:
  42. description = name
  43. if formats.get_format(name).native:
  44. description = description + " " + _("(Native)")
  45. selections.append((name, description))
  46. return selections
  47. py3o_filetype = fields.Selection(
  48. selection="_get_py3o_filetypes",
  49. string="Output Format")
  50. py3o_template_id = fields.Many2one(
  51. 'py3o.template',
  52. "Template")
  53. py3o_is_local_fusion = fields.Boolean(
  54. "Local Fusion",
  55. help="Native formats will be processed without a server. "
  56. "You must use this mode if you call methods on your model into "
  57. "the template.",
  58. default=True)
  59. py3o_server_id = fields.Many2one(
  60. "py3o.server",
  61. "Fusion Server")
  62. module = fields.Char(
  63. "Module",
  64. help="The implementer module that provides this report")
  65. py3o_template_fallback = fields.Char(
  66. "Fallback",
  67. size=128,
  68. help=(
  69. "If the user does not provide a template this will be used "
  70. "it should be a relative path to root of YOUR module "
  71. "or an absolute path on your server."
  72. ))
  73. report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
  74. @api.model
  75. def render_report(self, res_ids, name, data):
  76. action_py3o_report = self.search(
  77. [("report_name", "=", name),
  78. ("report_type", "=", "py3o")])
  79. if action_py3o_report:
  80. return self.env['py3o.report'].create({
  81. 'ir_actions_report_xml_id': action_py3o_report.id
  82. }).create_report(res_ids, data)
  83. return super(IrActionsReportXml, self).render_report(
  84. res_ids, name, data)