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.

97 lines
3.6 KiB

8 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 XCG Consulting (http://odoo.consulting)
  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.safe_eval import safe_eval
  9. logger = logging.getLogger(__name__)
  10. try:
  11. from py3o.formats import Formats
  12. except ImportError:
  13. logger.debug('Cannot import py3o.formats')
  14. class IrActionsReportXml(models.Model):
  15. """ Inherit from ir.actions.report.xml to allow customizing the template
  16. file. The user cam chose a template from a list.
  17. The list is configurable in the configuration tab, see py3o_template.py
  18. """
  19. _inherit = 'ir.actions.report.xml'
  20. @api.multi
  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. @api.model
  28. def _get_py3o_filetypes(self):
  29. formats = Formats()
  30. names = formats.get_known_format_names()
  31. selections = []
  32. for name in names:
  33. description = name
  34. if formats.get_format(name).native:
  35. description = description + " " + _("(Native)")
  36. selections.append((name, description))
  37. return selections
  38. py3o_filetype = fields.Selection(
  39. selection="_get_py3o_filetypes",
  40. string="Output Format")
  41. py3o_template_id = fields.Many2one(
  42. 'py3o.template',
  43. "Template")
  44. module = fields.Char(
  45. "Module",
  46. help="The implementer module that provides this report")
  47. py3o_template_fallback = fields.Char(
  48. "Fallback",
  49. size=128,
  50. help=(
  51. "If the user does not provide a template this will be used "
  52. "it should be a relative path to root of YOUR module "
  53. "or an absolute path on your server."
  54. ))
  55. report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
  56. py3o_multi_in_one = fields.Boolean(
  57. string='Multiple Records in a Single Report',
  58. help="If you execute a report on several records, "
  59. "by default Odoo will generate a ZIP file that contains as many "
  60. "files as selected records. If you enable this option, Odoo will "
  61. "generate instead a single report for the selected records.")
  62. @api.model
  63. def get_from_report_name(self, report_name, report_type):
  64. return self.search(
  65. [("report_name", "=", report_name),
  66. ("report_type", "=", report_type)])
  67. @api.model
  68. def render_report(self, res_ids, name, data):
  69. action_py3o_report = self.get_from_report_name(name, "py3o")
  70. if action_py3o_report:
  71. return self.env['py3o.report'].create({
  72. 'ir_actions_report_xml_id': action_py3o_report.id
  73. }).create_report(res_ids, data)
  74. return super(IrActionsReportXml, self).render_report(
  75. res_ids, name, data)
  76. @api.multi
  77. def gen_report_download_filename(self, res_ids, data):
  78. """Override this function to change the name of the downloaded report
  79. """
  80. self.ensure_one()
  81. report = self.get_from_report_name(self.report_name, self.report_type)
  82. if report.print_report_name and not len(res_ids) > 1:
  83. obj = self.env[self.model].browse(res_ids)
  84. return safe_eval(report.print_report_name,
  85. {'object': obj, 'time': time})
  86. return "%s.%s" % (self.name, self.py3o_filetype)