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.

135 lines
5.4 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
  7. #
  8. # The licence is in the file __openerp__.py
  9. #
  10. ##############################################################################
  11. from openerp import api, models, fields, _
  12. from openerp.exceptions import Warning as UserError
  13. from openerp.tools.safe_eval import safe_eval
  14. class MassMailing(models.Model):
  15. """ Add a direct link to an e-mail template in order to retrieve all
  16. Sendgrid configuration into the e-mails. Add ability to force a
  17. template language.
  18. """
  19. _inherit = 'mail.mass_mailing'
  20. email_template_id = fields.Many2one(
  21. 'mail.template', 'Sengdrid Template',
  22. )
  23. lang = fields.Many2one(
  24. comodel_name="res.lang", string="Force language")
  25. body_sendgrid = fields.Html(compute='_compute_sendgrid_view')
  26. # Trick to save html when taken from the e-mail template
  27. html_copy = fields.Html(
  28. compute='_compute_sendgrid_view', inverse='_inverse_html_copy')
  29. enable_unsubscribe = fields.Boolean()
  30. unsubscribe_text = fields.Char(
  31. default='If you would like to unsubscribe and stop receiving these '
  32. 'emails <% clickhere %>.')
  33. unsubscribe_tag = fields.Char()
  34. @api.depends('body_html')
  35. def _compute_sendgrid_view(self):
  36. for wizard in self:
  37. template = wizard.email_template_id.with_context(
  38. lang=self.lang.code or self.env.context['lang'])
  39. sendgrid_template = template.sendgrid_localized_template
  40. if sendgrid_template and wizard.body_html:
  41. res_id = self.env[wizard.mailing_model].search(safe_eval(
  42. wizard.mailing_domain), limit=1).id
  43. if res_id:
  44. body = template.render_template(
  45. wizard.body_html, template.model, [res_id],
  46. post_process=True)[res_id]
  47. wizard.body_sendgrid = \
  48. sendgrid_template.html_content.replace('<%body%>',
  49. body)
  50. else:
  51. wizard.body_sendgrid = wizard.body_html
  52. wizard.html_copy = wizard.body_html
  53. def _inverse_html_copy(self):
  54. for wizard in self:
  55. wizard.body_html = wizard.html_copy
  56. @api.onchange('email_template_id')
  57. def onchange_email_template_id(self):
  58. if self.email_template_id:
  59. template = self.email_template_id.with_context(
  60. lang=self.lang.code or self.env.context['lang'])
  61. if template.email_from:
  62. self.email_from = template.email_from
  63. self.name = template.subject
  64. self.body_html = template.body_html
  65. @api.onchange('lang')
  66. def onchange_lang(self):
  67. if self.lang and self.mailing_model == 'res.partner':
  68. domain = safe_eval(self.mailing_domain)
  69. lang_tuple = False
  70. for tuple in domain:
  71. if tuple[0] == 'lang':
  72. lang_tuple = tuple
  73. break
  74. if lang_tuple:
  75. domain.remove(lang_tuple)
  76. domain.append(('lang', '=', self.lang.code))
  77. self.mailing_domain = str(domain)
  78. self.onchange_email_template_id()
  79. @api.multi
  80. def action_test_mailing(self):
  81. wizard = self
  82. if self.email_template_id:
  83. wizard = self.with_context(
  84. lang=self.lang.code or self.env.context['lang'])
  85. return super(MassMailing, wizard).action_test_mailing()
  86. @api.multi
  87. def send_mail(self):
  88. self.ensure_one()
  89. if self.email_template_id:
  90. # use E-mail Template
  91. res_ids = self.get_recipients(self)
  92. if not res_ids:
  93. raise UserError(_('Please select recipients.'))
  94. template = self.email_template_id
  95. composer_values = {
  96. 'template_id': template.id,
  97. 'composition_mode': 'mass_mail',
  98. 'model': template.model,
  99. 'author_id': self.env.user.partner_id.id,
  100. 'res_id': res_ids[0],
  101. 'attachment_ids': [(4, attachment.id) for attachment in
  102. self.attachment_ids],
  103. 'email_from': self.email_from,
  104. 'body': self.body_html,
  105. 'subject': self.name,
  106. 'record_name': False,
  107. 'mass_mailing_id': self.id,
  108. 'mailing_list_ids': [(4, l.id) for l in
  109. self.contact_list_ids],
  110. 'no_auto_thread': self.reply_to_mode != 'thread',
  111. }
  112. if self.reply_to_mode == 'email':
  113. composer_values['reply_to'] = self.reply_to
  114. composer = self.env['mail.compose.message'].with_context(
  115. lang=self.lang.code or self.env.context.get('lang', 'en_US'),
  116. active_ids=res_ids)
  117. emails = composer.mass_mailing_sendgrid(res_ids, composer_values)
  118. self.write({
  119. 'state': 'done',
  120. 'sent_date': fields.Datetime.now(),
  121. })
  122. return emails
  123. else:
  124. # Traditional sending
  125. return super(MassMailing, self).send_mail()