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.

42 lines
1.7 KiB

  1. # Copyright 2016 Therp BV <http://therp.nl>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, tools, models
  4. class MailTemplate(models.Model):
  5. _inherit = 'mail.template'
  6. body_type = fields.Selection(
  7. [('jinja2', 'Jinja2'), ('qweb', 'QWeb')], 'Body templating engine',
  8. default='jinja2', required=True)
  9. body_view_id = fields.Many2one(
  10. 'ir.ui.view', 'Body view', domain=[('type', '=', 'qweb')])
  11. body_view_arch = fields.Text(related='body_view_id.arch')
  12. @api.multi
  13. def generate_email(self, res_ids, fields=None):
  14. multi_mode = True
  15. if isinstance(res_ids, int):
  16. res_ids = [res_ids]
  17. multi_mode = False
  18. result = super(MailTemplate, self).generate_email(
  19. res_ids, fields=fields
  20. )
  21. for res_id, template in self.get_email_template(res_ids).items():
  22. if template.body_type == 'qweb' and\
  23. (not fields or 'body_html' in fields):
  24. for record in self.env[template.model].browse(res_id):
  25. body_html = template.body_view_id.render({
  26. 'object': record,
  27. 'email_template': template,
  28. })
  29. # Some wizards, like when sending a sales order, need this
  30. # fix to display accents correctly
  31. body_html = tools.ustr(body_html)
  32. result[res_id]['body_html'] = self.render_post_process(
  33. body_html
  34. )
  35. result[res_id]['body'] = tools.html_sanitize(
  36. result[res_id]['body_html']
  37. )
  38. return multi_mode and result or result[res_ids[0]]