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.

195 lines
7.3 KiB

  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.misc import find_in_path
  9. from odoo.tools.safe_eval import safe_eval
  10. logger = logging.getLogger(__name__)
  11. try:
  12. from py3o.formats import Formats
  13. except ImportError:
  14. logger.debug("Cannot import py3o.formats")
  15. PY3O_CONVERSION_COMMAND_PARAMETER = "py3o.conversion_command"
  16. class IrActionsReport(models.Model):
  17. """ Inherit from ir.actions.report to allow customizing the template
  18. file. The user cam chose a template from a list.
  19. The list is configurable in the configuration tab, see py3o_template.py
  20. """
  21. _inherit = "ir.actions.report"
  22. @api.constrains("py3o_filetype", "report_type")
  23. def _check_py3o_filetype(self):
  24. for report in self:
  25. if report.report_type == "py3o" and not report.py3o_filetype:
  26. raise ValidationError(
  27. _("Field 'Output Format' is required for Py3O report")
  28. )
  29. @api.model
  30. def _get_py3o_filetypes(self):
  31. formats = Formats()
  32. names = formats.get_known_format_names()
  33. selections = []
  34. for name in names:
  35. description = name
  36. if formats.get_format(name).native:
  37. description = description + " " + _("(Native)")
  38. selections.append((name, description))
  39. return selections
  40. report_type = fields.Selection(selection_add=[("py3o", "py3o")])
  41. py3o_filetype = fields.Selection(
  42. selection="_get_py3o_filetypes", string="Output Format"
  43. )
  44. is_py3o_native_format = fields.Boolean(compute="_compute_is_py3o_native_format")
  45. py3o_template_id = fields.Many2one("py3o.template", "Template")
  46. module = fields.Char(
  47. "Module", help="The implementer module that provides this report"
  48. )
  49. py3o_template_fallback = fields.Char(
  50. "Fallback",
  51. size=128,
  52. help=(
  53. "If the user does not provide a template this will be used "
  54. "it should be a relative path to root of YOUR module "
  55. "or an absolute path on your server."
  56. ),
  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. )
  66. lo_bin_path = fields.Char(
  67. string="Path to the libreoffice runtime", compute="_compute_lo_bin_path"
  68. )
  69. is_py3o_report_not_available = fields.Boolean(
  70. compute="_compute_py3o_report_not_available"
  71. )
  72. msg_py3o_report_not_available = fields.Char(
  73. compute="_compute_py3o_report_not_available"
  74. )
  75. @api.model
  76. def _register_hook(self):
  77. self._validate_reports()
  78. @api.model
  79. def _validate_reports(self):
  80. """Check if the existing py3o reports should work with the current
  81. installation.
  82. This method log a warning message into the logs for each report
  83. that should not work.
  84. """
  85. for report in self.search([("report_type", "=", "py3o")]):
  86. if report.is_py3o_report_not_available:
  87. logger.warning(report.msg_py3o_report_not_available)
  88. @api.model
  89. def _get_lo_bin(self):
  90. lo_bin = (
  91. self.env["ir.config_parameter"]
  92. .sudo()
  93. .get_param(PY3O_CONVERSION_COMMAND_PARAMETER, "libreoffice")
  94. )
  95. try:
  96. lo_bin = find_in_path(lo_bin)
  97. except IOError:
  98. lo_bin = None
  99. return lo_bin
  100. @api.depends("report_type", "py3o_filetype")
  101. def _compute_is_py3o_native_format(self):
  102. fmt = Formats()
  103. for rec in self:
  104. rec.is_py3o_native_format = False
  105. if not rec.report_type == "py3o" or not rec.py3o_filetype:
  106. continue
  107. filetype = rec.py3o_filetype
  108. rec.is_py3o_native_format = fmt.get_format(filetype).native
  109. def _compute_lo_bin_path(self):
  110. lo_bin = self._get_lo_bin()
  111. for rec in self:
  112. rec.lo_bin_path = lo_bin
  113. @api.depends("lo_bin_path", "is_py3o_native_format", "report_type")
  114. def _compute_py3o_report_not_available(self):
  115. for rec in self:
  116. rec.is_py3o_report_not_available = False
  117. rec.msg_py3o_report_not_available = ""
  118. if not rec.report_type == "py3o":
  119. continue
  120. if not rec.is_py3o_native_format and not rec.lo_bin_path:
  121. rec.is_py3o_report_not_available = True
  122. rec.msg_py3o_report_not_available = (
  123. _(
  124. "The libreoffice runtime is required to genereate the "
  125. "py3o report '%s' but is not found into the bin path. You "
  126. "must install the libreoffice runtime on the server. If "
  127. "the runtime is already installed and is not found by "
  128. "Odoo, you can provide the full path to the runtime by "
  129. "setting the key 'py3o.conversion_command' into the "
  130. "configuration parameters."
  131. )
  132. % rec.name
  133. )
  134. @api.model
  135. def get_from_report_name(self, report_name, report_type):
  136. return self.search(
  137. [("report_name", "=", report_name), ("report_type", "=", report_type)]
  138. )
  139. def render_py3o(self, res_ids, data):
  140. self.ensure_one()
  141. if self.report_type != "py3o":
  142. raise RuntimeError(
  143. "py3o rendition is only available on py3o report.\n"
  144. "(current: '{}', expected 'py3o'".format(self.report_type)
  145. )
  146. return (
  147. self.env["py3o.report"]
  148. .create({"ir_actions_report_id": self.id})
  149. .create_report(res_ids, data)
  150. )
  151. def gen_report_download_filename(self, res_ids, data):
  152. """Override this function to change the name of the downloaded report
  153. """
  154. self.ensure_one()
  155. report = self.get_from_report_name(self.report_name, self.report_type)
  156. if report.print_report_name and not len(res_ids) > 1:
  157. obj = self.env[self.model].browse(res_ids)
  158. return safe_eval(report.print_report_name, {"object": obj, "time": time})
  159. return "{}.{}".format(self.name, self.py3o_filetype)
  160. def _get_attachments(self, res_ids):
  161. """ Return the report already generated for the given res_ids
  162. """
  163. self.ensure_one()
  164. save_in_attachment = {}
  165. if res_ids:
  166. # Dispatch the records by ones having an attachment
  167. Model = self.env[self.model]
  168. record_ids = Model.browse(res_ids)
  169. if self.attachment:
  170. for record_id in record_ids:
  171. attachment_id = self.retrieve_attachment(record_id)
  172. if attachment_id:
  173. save_in_attachment[record_id.id] = attachment_id
  174. return save_in_attachment