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.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. 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. lo_bin_path = fields.Char(
  70. string="Path to the libreoffice runtime",
  71. 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 = self.env['ir.config_parameter'].sudo().get_param(
  95. PY3O_CONVERSION_COMMAND_PARAMETER, 'libreoffice',
  96. )
  97. try:
  98. lo_bin = find_in_path(lo_bin)
  99. except IOError:
  100. lo_bin = None
  101. return lo_bin
  102. @api.depends("report_type", "py3o_filetype")
  103. @api.multi
  104. def _compute_is_py3o_native_format(self):
  105. format = Formats()
  106. for rec in self:
  107. if not rec.report_type == "py3o":
  108. continue
  109. filetype = rec.py3o_filetype
  110. rec.is_py3o_native_format = format.get_format(filetype).native
  111. @api.multi
  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. @api.multi
  118. def _compute_py3o_report_not_available(self):
  119. for rec in self:
  120. if not rec.report_type == "py3o":
  121. continue
  122. if not rec.is_py3o_native_format and not rec.lo_bin_path:
  123. rec.is_py3o_report_not_available = True
  124. rec.msg_py3o_report_not_available = _(
  125. "The libreoffice runtime is required to genereate the "
  126. "py3o report '%s' but is not found into the bin path. You "
  127. "must install the libreoffice runtime on the server. If "
  128. "the runtime is already installed and is not found by "
  129. "Odoo, you can provide the full path to the runtime by "
  130. "setting the key 'py3o.conversion_command' into the "
  131. "configuration parameters."
  132. ) % rec.name
  133. @api.model
  134. def get_from_report_name(self, report_name, report_type):
  135. return self.search(
  136. [("report_name", "=", report_name),
  137. ("report_type", "=", report_type)])
  138. @api.multi
  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. return self.env['py3o.report'].create({
  146. 'ir_actions_report_id': self.id
  147. }).create_report(res_ids, data)
  148. @api.multi
  149. def gen_report_download_filename(self, res_ids, data):
  150. """Override this function to change the name of the downloaded report
  151. """
  152. self.ensure_one()
  153. report = self.get_from_report_name(self.report_name, self.report_type)
  154. if report.print_report_name and not len(res_ids) > 1:
  155. obj = self.env[self.model].browse(res_ids)
  156. return safe_eval(report.print_report_name,
  157. {'object': obj, 'time': time})
  158. return "%s.%s" % (self.name, self.py3o_filetype)
  159. @api.multi
  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