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.

128 lines
5.1 KiB

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