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.

140 lines
5.2 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.model
  71. def render_report(self, res_ids, name, data):
  72. action_py3o_report = self.get_from_report_name(name, "py3o")
  73. if action_py3o_report:
  74. return action_py3o_report._render_py3o(res_ids, data)
  75. return super(IrActionsReport, self).render_report(
  76. res_ids, name, data)
  77. @api.multi
  78. def _render_py3o(self, res_ids, data):
  79. self.ensure_one()
  80. if self.report_type != "py3o":
  81. raise RuntimeError(
  82. "py3o rendition is only available on py3o report.\n"
  83. "(current: '{}', expected 'py3o'".format(self.report_type))
  84. return self.env['py3o.report'].create({
  85. 'ir_actions_report_id': self.id
  86. }).create_report(res_ids, data)
  87. @api.multi
  88. def gen_report_download_filename(self, res_ids, data):
  89. """Override this function to change the name of the downloaded report
  90. """
  91. self.ensure_one()
  92. report = self.get_from_report_name(self.report_name, self.report_type)
  93. if report.print_report_name and not len(res_ids) > 1:
  94. obj = self.env[self.model].browse(res_ids)
  95. return safe_eval(report.print_report_name,
  96. {'object': obj, 'time': time})
  97. return "%s.%s" % (self.name, self.py3o_filetype)
  98. @api.model
  99. def _get_report_from_name(self, report_name):
  100. """Get the first record of ir.actions.report having the
  101. ``report_name`` as value for the field report_name.
  102. """
  103. res = super(IrActionsReport, self)._get_report_from_name(report_name)
  104. if res:
  105. return res
  106. # maybe a py3o report
  107. context = self.env['res.users'].context_get()
  108. return self.with_context(context).search(
  109. [('report_type', '=', 'py3o'),
  110. ('report_name', '=', report_name)], limit=1)
  111. @api.multi
  112. def _get_attachments(self, res_ids):
  113. """ Return the report already generated for the given res_ids
  114. """
  115. self.ensure_one()
  116. save_in_attachment = {}
  117. if res_ids:
  118. # Dispatch the records by ones having an attachment
  119. Model = self.env[self.model]
  120. record_ids = Model.browse(res_ids)
  121. if self.attachment:
  122. for record_id in record_ids:
  123. attachment_id = self.retrieve_attachment(record_id)
  124. if attachment_id:
  125. save_in_attachment[record_id.id] = attachment_id
  126. return save_in_attachment