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.3 KiB

8 years ago
  1. # Copyright 2013 XCG Consulting (http://odoo.consulting)
  2. # Copyright 2018 ACSONE SA/NV
  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 IrActionsReport(models.Model):
  15. """ Inherit from ir.actions.report 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'
  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.model
  28. def _get_py3o_filetypes(self):
  29. formats = Formats()
  30. names = formats.get_known_format_names()
  31. selections = []
  32. for name in names:
  33. description = name
  34. if formats.get_format(name).native:
  35. description = description + " " + _("(Native)")
  36. selections.append((name, description))
  37. return selections
  38. report_type = fields.Selection(
  39. selection_add=[("py3o", "py3o")]
  40. )
  41. py3o_filetype = fields.Selection(
  42. selection="_get_py3o_filetypes",
  43. string="Output Format")
  44. py3o_template_id = fields.Many2one(
  45. 'py3o.template',
  46. "Template")
  47. module = fields.Char(
  48. "Module",
  49. help="The implementer module that provides this report")
  50. py3o_template_fallback = fields.Char(
  51. "Fallback",
  52. size=128,
  53. help=(
  54. "If the user does not provide a template this will be used "
  55. "it should be a relative path to root of YOUR module "
  56. "or an absolute path on your server."
  57. ))
  58. report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
  59. py3o_multi_in_one = fields.Boolean(
  60. string='Multiple Records in a Single Report',
  61. help="If you execute a report on several records, "
  62. "by default Odoo will generate a ZIP file that contains as many "
  63. "files as selected records. If you enable this option, Odoo will "
  64. "generate instead a single report for the selected records.")
  65. @api.model
  66. def get_from_report_name(self, report_name, report_type):
  67. return self.search(
  68. [("report_name", "=", report_name),
  69. ("report_type", "=", report_type)])
  70. @api.multi
  71. def render_py3o(self, res_ids, data):
  72. self.ensure_one()
  73. if self.report_type != "py3o":
  74. raise RuntimeError(
  75. "py3o rendition is only available on py3o report.\n"
  76. "(current: '{}', expected 'py3o'".format(self.report_type))
  77. return self.env['py3o.report'].create({
  78. 'ir_actions_report_id': self.id
  79. }).create_report(res_ids, data)
  80. @api.multi
  81. def gen_report_download_filename(self, res_ids, data):
  82. """Override this function to change the name of the downloaded report
  83. """
  84. self.ensure_one()
  85. report = self.get_from_report_name(self.report_name, self.report_type)
  86. if report.print_report_name and not len(res_ids) > 1:
  87. obj = self.env[self.model].browse(res_ids)
  88. return safe_eval(report.print_report_name,
  89. {'object': obj, 'time': time})
  90. return "%s.%s" % (self.name, self.py3o_filetype)
  91. @api.multi
  92. def _get_attachments(self, res_ids):
  93. """ Return the report already generated for the given res_ids
  94. """
  95. self.ensure_one()
  96. save_in_attachment = {}
  97. if res_ids:
  98. # Dispatch the records by ones having an attachment
  99. Model = self.env[self.model]
  100. record_ids = Model.browse(res_ids)
  101. if self.attachment:
  102. for record_id in record_ids:
  103. attachment_id = self.retrieve_attachment(record_id)
  104. if attachment_id:
  105. save_in_attachment[record_id.id] = attachment_id
  106. return save_in_attachment