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.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015-2017 Compassion CH (http://www.compassion.ch)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import models, fields, api
  5. class EmailComposeMessage(models.TransientModel):
  6. """ Email message sent through SendGrid """
  7. _inherit = 'mail.compose.message'
  8. body_sendgrid = fields.Html(compute='_compute_sendgrid_view')
  9. @api.depends('body')
  10. def _compute_sendgrid_view(self):
  11. for wizard in self:
  12. template = wizard.template_id
  13. sendgrid_template = template.sendgrid_localized_template
  14. res_id = self.env.context.get('active_id')
  15. render_body = self.render_template(
  16. wizard.body, wizard.model, [res_id], post_process=True)[res_id]
  17. if sendgrid_template and wizard.body:
  18. wizard.body_sendgrid = sendgrid_template.html_content.replace(
  19. '<%body%>', render_body)
  20. else:
  21. wizard.body_sendgrid = render_body
  22. @api.multi
  23. def get_mail_values(self, res_ids):
  24. """ Attach sendgrid template to e-mail and render substitutions """
  25. mail_values = super(EmailComposeMessage, self).get_mail_values(res_ids)
  26. template = self.template_id
  27. sendgrid_template_id = template.sendgrid_localized_template.id
  28. if sendgrid_template_id:
  29. substitutions = template.render_substitutions(res_ids)
  30. for res_id, value in mail_values.iteritems():
  31. value['sendgrid_template_id'] = sendgrid_template_id
  32. value['substitution_ids'] = substitutions[res_id]
  33. return mail_values