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.5 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.multi
  21. @api.constrains("py3o_filetype", "report_type")
  22. def _check_py3o_filetype(self):
  23. for report in self:
  24. if report.report_type == "py3o" and not report.py3o_filetype:
  25. raise ValidationError(_(
  26. "Field 'Output Format' is required for Py3O report"))
  27. @api.multi
  28. @api.constrains("py3o_is_local_fusion", "py3o_server_id",
  29. "py3o_filetype")
  30. def _check_py3o_server_id(self):
  31. for report in self:
  32. if report.report_type == "py3o":
  33. is_native = Formats().get_format(report.py3o_filetype).native
  34. if ((not is_native or not report.py3o_is_local_fusion) and
  35. not report.py3o_server_id):
  36. raise ValidationError(_(
  37. "Can not use not native format in local fusion. "
  38. "Please specify a Fusion Server"))
  39. @api.model
  40. def _get_py3o_filetypes(self):
  41. formats = Formats()
  42. names = formats.get_known_format_names()
  43. selections = []
  44. for name in names:
  45. description = name
  46. if formats.get_format(name).native:
  47. description = description + " " + _("(Native)")
  48. selections.append((name, description))
  49. return selections
  50. py3o_filetype = fields.Selection(
  51. selection="_get_py3o_filetypes",
  52. string="Output Format")
  53. py3o_template_id = fields.Many2one(
  54. 'py3o.template',
  55. "Template")
  56. py3o_is_local_fusion = fields.Boolean(
  57. "Local Fusion",
  58. help="Native formats will be processed without a server. "
  59. "You must use this mode if you call methods on your model into "
  60. "the template.",
  61. default=True)
  62. py3o_server_id = fields.Many2one(
  63. "py3o.server",
  64. "Fusion Server")
  65. module = fields.Char(
  66. "Module",
  67. help="The implementer module that provides this report")
  68. py3o_template_fallback = fields.Char(
  69. "Fallback",
  70. size=128,
  71. help=(
  72. "If the user does not provide a template this will be used "
  73. "it should be a relative path to root of YOUR module "
  74. "or an absolute path on your server."
  75. ))
  76. report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
  77. py3o_multi_in_one = fields.Boolean(
  78. string='Multiple Records in a Single Report',
  79. help="If you execute a report on several records, "
  80. "by default Odoo will generate a ZIP file that contains as many "
  81. "files as selected records. If you enable this option, Odoo will "
  82. "generate instead a single report for the selected records.")
  83. @api.model
  84. def get_from_report_name(self, report_name, report_type):
  85. return self.search(
  86. [("report_name", "=", report_name),
  87. ("report_type", "=", report_type)])
  88. @api.model
  89. def render_report(self, res_ids, name, data):
  90. action_py3o_report = self.get_from_report_name(name, "py3o")
  91. if action_py3o_report:
  92. return self.env['py3o.report'].create({
  93. 'ir_actions_report_xml_id': action_py3o_report.id
  94. }).create_report(res_ids, data)
  95. return super(IrActionsReportXml, self).render_report(
  96. res_ids, name, data)
  97. @api.multi
  98. def gen_report_download_filename(self, res_ids, data):
  99. """Override this function to change the name of the downloaded report
  100. """
  101. self.ensure_one()
  102. report = self.get_from_report_name(self.report_name, self.report_type)
  103. if report.print_report_name and not len(res_ids) > 1:
  104. obj = self.env[self.model].browse(res_ids)
  105. return safe_eval(report.print_report_name,
  106. {'object': obj, 'time': time})
  107. return "%s.%s" % (self.name, self.py3o_filetype)