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.

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