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.

140 lines
5.7 KiB

6 years ago
  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', 'Sendgrid 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. # Trick to display another widget when using Sendgrid
  23. html_unframe = fields.Html(related='body_html')
  24. enable_unsubscribe = fields.Boolean()
  25. unsubscribe_text = fields.Char(
  26. default='If you would like to unsubscribe and stop receiving these '
  27. 'emails <% clickhere %>.')
  28. unsubscribe_tag = fields.Char()
  29. @api.depends('body_html')
  30. def _compute_sendgrid_view(self):
  31. for wizard in self:
  32. template = wizard.email_template_id.with_context(
  33. lang=self.lang.code or self.env.context['lang'])
  34. sendgrid_template = template.sendgrid_localized_template
  35. if sendgrid_template and wizard.body_html:
  36. res_id = self.env[wizard.mailing_model].search(safe_eval(
  37. wizard.mailing_domain), limit=1).id
  38. if res_id:
  39. body = template.render_template(
  40. wizard.body_html, template.model, [res_id],
  41. post_process=True)[res_id]
  42. wizard.body_sendgrid = \
  43. sendgrid_template.html_content.replace('<%body%>',
  44. body)
  45. else:
  46. wizard.body_sendgrid = wizard.body_html
  47. wizard.html_copy = wizard.body_html
  48. def _inverse_html_copy(self):
  49. for wizard in self:
  50. wizard.body_html = wizard.html_copy
  51. @api.onchange('email_template_id')
  52. def onchange_email_template_id(self):
  53. if self.email_template_id:
  54. template = self.email_template_id.with_context(
  55. lang=self.lang.code or self.env.context['lang'])
  56. if template.email_from:
  57. self.email_from = template.email_from
  58. self.name = template.subject
  59. self.body_html = template.body_html
  60. @api.onchange('lang')
  61. def onchange_lang(self):
  62. if self.lang and self.mailing_model == 'res.partner':
  63. domain = safe_eval(self.mailing_domain)
  64. lang_tuple = False
  65. for tuple in domain:
  66. if tuple[0] == 'lang':
  67. lang_tuple = tuple
  68. break
  69. if lang_tuple:
  70. domain.remove(lang_tuple)
  71. domain.append(('lang', '=', self.lang.code))
  72. self.mailing_domain = str(domain)
  73. self.onchange_email_template_id()
  74. @api.multi
  75. def action_test_mailing(self):
  76. wizard = self
  77. if self.email_template_id:
  78. wizard = self.with_context(
  79. lang=self.lang.code or self.env.context['lang'])
  80. return super(MassMailing, wizard).action_test_mailing()
  81. @api.multi
  82. def send_mail(self):
  83. sendgrid = self.filtered('email_template_id')
  84. emails = self.env['mail.mail']
  85. for mailing in sendgrid:
  86. # use E-mail Template
  87. res_ids = mailing.get_recipients()
  88. if not res_ids:
  89. raise UserError(_('Please select recipients.'))
  90. lang = mailing.lang.code or self.env.context.get('lang', 'en_US')
  91. mailing = mailing.with_context(lang=lang)
  92. composer_values = mailing._send_mail_get_composer_values()
  93. if mailing.reply_to_mode == 'email':
  94. composer_values['reply_to'] = mailing.reply_to
  95. composer = self.env['mail.compose.message'].with_context(
  96. lang=lang, active_ids=res_ids)
  97. emails += composer.mass_mailing_sendgrid(res_ids, composer_values)
  98. mailing.write({
  99. 'state': 'done',
  100. 'sent_date': fields.Datetime.now(),
  101. })
  102. # Traditional sending
  103. super(MassMailing, self - sendgrid).send_mail()
  104. return emails
  105. def _send_mail_get_composer_values(self):
  106. """
  107. Get the values used for the mail.compose.message wizard that will
  108. generate the e-mails of a mass mailing campaign.
  109. :return: dictionary of mail.compose.message values
  110. """
  111. template = self.email_template_id
  112. author = self.mass_mailing_campaign_id.user_id.partner_id or \
  113. self.env.user.partner_id
  114. return {
  115. 'template_id': template.id,
  116. 'composition_mode': 'mass_mail',
  117. 'model': template.model,
  118. 'author_id': author.id,
  119. 'attachment_ids': [(4, attachment.id) for attachment in
  120. self.attachment_ids],
  121. 'email_from': self.email_from,
  122. 'body': self.body_html,
  123. 'subject': self.name,
  124. 'record_name': False,
  125. 'mass_mailing_id': self.id,
  126. 'mailing_list_ids': [(4, l.id) for l in
  127. self.contact_list_ids],
  128. 'no_auto_thread': self.reply_to_mode != 'thread',
  129. }