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.

200 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(
  41. selection_add=[("py3o", "py3o")],
  42. ondelete={
  43. 'py3o': 'cascade',
  44. },
  45. )
  46. py3o_filetype = fields.Selection(
  47. selection="_get_py3o_filetypes", string="Output Format"
  48. )
  49. is_py3o_native_format = fields.Boolean(compute="_compute_is_py3o_native_format")
  50. py3o_template_id = fields.Many2one("py3o.template", "Template")
  51. module = fields.Char(
  52. "Module", help="The implementer module that provides this report"
  53. )
  54. py3o_template_fallback = fields.Char(
  55. "Fallback",
  56. size=128,
  57. help=(
  58. "If the user does not provide a template this will be used "
  59. "it should be a relative path to root of YOUR module "
  60. "or an absolute path on your server."
  61. ),
  62. )
  63. py3o_multi_in_one = fields.Boolean(
  64. string="Multiple Records in a Single Report",
  65. help="If you execute a report on several records, "
  66. "by default Odoo will generate a ZIP file that contains as many "
  67. "files as selected records. If you enable this option, Odoo will "
  68. "generate instead a single report for the selected records.",
  69. )
  70. lo_bin_path = fields.Char(
  71. string="Path to the libreoffice runtime", compute="_compute_lo_bin_path"
  72. )
  73. is_py3o_report_not_available = fields.Boolean(
  74. compute="_compute_py3o_report_not_available"
  75. )
  76. msg_py3o_report_not_available = fields.Char(
  77. compute="_compute_py3o_report_not_available"
  78. )
  79. @api.model
  80. def _register_hook(self):
  81. self._validate_reports()
  82. @api.model
  83. def _validate_reports(self):
  84. """Check if the existing py3o reports should work with the current
  85. installation.
  86. This method log a warning message into the logs for each report
  87. that should not work.
  88. """
  89. for report in self.search([("report_type", "=", "py3o")]):
  90. if report.is_py3o_report_not_available:
  91. logger.warning(report.msg_py3o_report_not_available)
  92. @api.model
  93. def _get_lo_bin(self):
  94. lo_bin = (
  95. self.env["ir.config_parameter"]
  96. .sudo()
  97. .get_param(PY3O_CONVERSION_COMMAND_PARAMETER, "libreoffice")
  98. )
  99. try:
  100. lo_bin = find_in_path(lo_bin)
  101. except IOError:
  102. lo_bin = None
  103. return lo_bin
  104. @api.depends("report_type", "py3o_filetype")
  105. def _compute_is_py3o_native_format(self):
  106. fmt = Formats()
  107. for rec in self:
  108. rec.is_py3o_native_format = False
  109. if not rec.report_type == "py3o" or not rec.py3o_filetype:
  110. continue
  111. filetype = rec.py3o_filetype
  112. rec.is_py3o_native_format = fmt.get_format(filetype).native
  113. def _compute_lo_bin_path(self):
  114. lo_bin = self._get_lo_bin()
  115. for rec in self:
  116. rec.lo_bin_path = lo_bin
  117. @api.depends("lo_bin_path", "is_py3o_native_format", "report_type")
  118. def _compute_py3o_report_not_available(self):
  119. for rec in self:
  120. rec.is_py3o_report_not_available = False
  121. rec.msg_py3o_report_not_available = ""
  122. if not rec.report_type == "py3o":
  123. continue
  124. if not rec.is_py3o_native_format and not rec.lo_bin_path:
  125. rec.is_py3o_report_not_available = True
  126. rec.msg_py3o_report_not_available = (
  127. _(
  128. "The libreoffice runtime is required to genereate the "
  129. "py3o report '%s' but is not found into the bin path. You "
  130. "must install the libreoffice runtime on the server. If "
  131. "the runtime is already installed and is not found by "
  132. "Odoo, you can provide the full path to the runtime by "
  133. "setting the key 'py3o.conversion_command' into the "
  134. "configuration parameters."
  135. )
  136. % rec.name
  137. )
  138. @api.model
  139. def get_from_report_name(self, report_name, report_type):
  140. return self.search(
  141. [("report_name", "=", report_name), ("report_type", "=", report_type)]
  142. )
  143. def _render_py3o(self, res_ids, data):
  144. self.ensure_one()
  145. if self.report_type != "py3o":
  146. raise RuntimeError(
  147. "py3o rendition is only available on py3o report.\n"
  148. "(current: '{}', expected 'py3o'".format(self.report_type)
  149. )
  150. return (
  151. self.env["py3o.report"]
  152. .create({"ir_actions_report_id": self.id})
  153. .create_report(res_ids, data)
  154. )
  155. def gen_report_download_filename(self, res_ids, data):
  156. """Override this function to change the name of the downloaded report
  157. """
  158. self.ensure_one()
  159. report = self.get_from_report_name(self.report_name, self.report_type)
  160. if report.print_report_name and not len(res_ids) > 1:
  161. obj = self.env[self.model].browse(res_ids)
  162. return safe_eval(report.print_report_name, {"object": obj,})
  163. return "{}.{}".format(self.name, self.py3o_filetype)
  164. def _get_attachments(self, res_ids):
  165. """ Return the report already generated for the given res_ids
  166. """
  167. self.ensure_one()
  168. save_in_attachment = {}
  169. if res_ids:
  170. # Dispatch the records by ones having an attachment
  171. Model = self.env[self.model]
  172. record_ids = Model.browse(res_ids)
  173. if self.attachment:
  174. for record_id in record_ids:
  175. attachment_id = self.retrieve_attachment(record_id)
  176. if attachment_id:
  177. save_in_attachment[record_id.id] = attachment_id
  178. return save_in_attachment