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.

49 lines
1.9 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2016 Compassion CH (http://www.compassion.ch)
  5. # Releasing children from poverty in Jesus' name
  6. # @author: Emanuel Cino <ecino@compassion.ch>
  7. #
  8. # The licence is in the file __openerp__.py
  9. #
  10. ##############################################################################
  11. from openerp import models, fields, api
  12. class EmailComposeMessage(models.TransientModel):
  13. """ Email message sent through SendGrid """
  14. _inherit = 'mail.compose.message'
  15. body_sendgrid = fields.Html(compute='_compute_sendgrid_view')
  16. @api.depends('body')
  17. def _compute_sendgrid_view(self):
  18. for wizard in self:
  19. template = wizard.template_id
  20. sendgrid_template = template.sendgrid_localized_template
  21. res_id = self.env.context.get('active_id')
  22. render_body = self.render_template(
  23. wizard.body, wizard.model, [res_id], post_process=True)[res_id]
  24. if sendgrid_template and wizard.body:
  25. wizard.body_sendgrid = sendgrid_template.html_content.replace(
  26. '<%body%>', render_body)
  27. else:
  28. wizard.body_sendgrid = render_body
  29. @api.multi
  30. def get_mail_values(self, res_ids):
  31. """ Attach sendgrid template to e-mail and render substitutions """
  32. mail_values = super(EmailComposeMessage, self).get_mail_values(res_ids)
  33. template = self.template_id
  34. sendgrid_template_id = template.sendgrid_localized_template.id
  35. if sendgrid_template_id:
  36. substitutions = template.render_substitutions(res_ids)
  37. for res_id, value in mail_values.iteritems():
  38. value['sendgrid_template_id'] = sendgrid_template_id
  39. value['substitution_ids'] = substitutions[res_id]
  40. return mail_values