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.

44 lines
1.8 KiB

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