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.

70 lines
2.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Savoir-faire Linux
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import base64
  5. from odoo import api, fields, models
  6. from odoo.tools.safe_eval import safe_eval
  7. from odoo import report as odoo_report
  8. class MailTemplate(models.Model):
  9. _inherit = 'mail.template'
  10. report_line_ids = fields.One2many(
  11. comodel_name='mail.template.report.line',
  12. inverse_name='template_id',
  13. string='Other Reports'
  14. )
  15. @api.multi
  16. def generate_email(self, res_ids, fields=None):
  17. self.ensure_one()
  18. results = super(MailTemplate, self).generate_email(
  19. res_ids, fields=fields)
  20. for report_line in self.report_line_ids:
  21. records = self.env[self.model_id.model].browse(res_ids)
  22. for rec in records:
  23. condition = report_line.condition
  24. if condition and condition.strip():
  25. condition_result = self.render_template(
  26. condition, self.model, rec.id)
  27. if not condition_result or not safe_eval(condition_result):
  28. continue
  29. report_name = self.render_template(
  30. report_line.report_name, self.model, rec.id)
  31. report = report_line.report_template_id
  32. report_service = report.report_name
  33. if report.report_type in ['qweb-html', 'qweb-pdf']:
  34. result, fmt = self.env['report'].get_pdf(
  35. [rec.id], report_service), 'pdf'
  36. else:
  37. result, fmt = odoo_report.render_report(
  38. self._cr, self._uid, [rec.id],
  39. report_service, {'model': self.model}, self._context)
  40. result = base64.b64encode(result)
  41. ext = "." + fmt
  42. if not report_name:
  43. report_name = 'report.' + report_service
  44. if not report_name.endswith(ext):
  45. report_name += ext
  46. results[rec.id].setdefault('attachments', [])
  47. results[rec.id]['attachments'].append((report_name, result))
  48. return results