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.

60 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. for report_line in template.report_line_ids:
  17. records = self.env[template.model_id.model].browse(res_ids)
  18. for rec in records:
  19. condition = report_line.condition
  20. if condition and condition.strip():
  21. condition_result = self.render_template(
  22. condition, template.model, rec.id)
  23. if not condition_result or not safe_eval(condition_result):
  24. continue
  25. report_name = self.render_template(
  26. report_line.report_name, template.model, rec.id)
  27. report = report_line.report_template_id
  28. report_service = report.report_name
  29. result = self.env['report'].get_pdf(rec, report_service)
  30. report_format = 'pdf'
  31. result = base64.b64encode(result)
  32. if not report_name:
  33. report_name = 'report.' + report_service
  34. ext = "." + report_format
  35. if not report_name.endswith(ext):
  36. report_name += ext
  37. results[rec.id].setdefault('attachments', [])
  38. results[rec.id]['attachments'].append((report_name, result))
  39. return results