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.

57 lines
1.9 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 openerp import api, fields, models
  6. from openerp.tools.safe_eval import safe_eval
  7. class EmailTemplate(models.Model):
  8. _inherit = 'email.template'
  9. report_line_ids = fields.One2many(
  10. 'email.template.report.line', 'template_id', string='Other Reports')
  11. @api.model
  12. def generate_email_batch(self, template_id, res_ids, fields=None):
  13. results = super(EmailTemplate, self).generate_email_batch(
  14. template_id, res_ids, fields=fields)
  15. template = self.browse(template_id)
  16. report_ext = '.pdf'
  17. for report_line in template.report_line_ids:
  18. records = self.env[template.model_id.model].browse(res_ids)
  19. for rec in records:
  20. condition = report_line.condition
  21. if condition and condition.strip():
  22. condition_result = self.render_template(
  23. condition, template.model, rec.id)
  24. if not condition_result or not safe_eval(condition_result):
  25. continue
  26. report_name = self.render_template(
  27. report_line.report_name, template.model, rec.id)
  28. report = report_line.report_template_id
  29. report_service = report.report_name
  30. result = self.env['report'].get_pdf(rec, report_service)
  31. result = base64.b64encode(result)
  32. if not report_name:
  33. report_name = 'report.' + report_service
  34. if not report_name.endswith(report_ext):
  35. report_name += report_ext
  36. results[rec.id].setdefault('attachments', [])
  37. results[rec.id]['attachments'].append((report_name, result))
  38. return results