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.

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