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.

118 lines
4.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. import time
  6. from odoo import api, fields, models, _
  7. from odoo.exceptions import ValidationError
  8. from odoo.tools.safe_eval import safe_eval
  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. py3o_multi_in_one = fields.Boolean(
  77. string='Multiple Records in a Single Report',
  78. help="If you execute a report on several records, "
  79. "by default Odoo will generate a ZIP file that contains as many "
  80. "files as selected records. If you enable this option, Odoo will "
  81. "generate instead a single report for the selected records.")
  82. @api.model
  83. def get_from_report_name(self, report_name, report_type):
  84. return self.search(
  85. [("report_name", "=", report_name),
  86. ("report_type", "=", report_type)])
  87. @api.model
  88. def render_report(self, res_ids, name, data):
  89. action_py3o_report = self.get_from_report_name(name, "py3o")
  90. if action_py3o_report:
  91. return self.env['py3o.report'].create({
  92. 'ir_actions_report_xml_id': action_py3o_report.id
  93. }).create_report(res_ids, data)
  94. return super(IrActionsReportXml, self).render_report(
  95. res_ids, name, data)
  96. @api.multi
  97. def gen_report_download_filename(self, res_ids, data):
  98. """Override this function to change the name of the downloaded report
  99. """
  100. self.ensure_one()
  101. report = self.get_from_report_name(self.report_name, self.report_type)
  102. if report.print_report_name and not len(res_ids) > 1:
  103. obj = self.env[self.model].browse(res_ids)
  104. return safe_eval(report.print_report_name,
  105. {'object': obj, 'time': time})
  106. return "%s.%s" % (self.name, self.py3o_filetype)