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.

59 lines
2.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 models
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. try:
  8. from sendgrid.helpers.mail.mail import TrackingSettings, \
  9. SubscriptionTracking
  10. except ImportError:
  11. _logger.error("ImportError raised while loading module.")
  12. _logger.debug("ImportError details:", exc_info=True)
  13. class MailMail(models.Model):
  14. _inherit = "mail.mail"
  15. def _prepare_sendgrid_tracking(self):
  16. track_vals = super(MailMail, self)._prepare_sendgrid_tracking()
  17. track_vals.update({
  18. 'mail_id_int': self.id,
  19. 'mass_mailing_id': self.mailing_id.id,
  20. 'mail_stats_id': self.statistics_ids[:1].id
  21. if self.statistics_ids else False
  22. })
  23. return track_vals
  24. def _track_sendgrid_emails(self):
  25. """ Push tracking_email in mass_mail_statistic """
  26. tracking_emails = super(MailMail, self)._track_sendgrid_emails()
  27. for tracking in tracking_emails.filtered('mail_stats_id'):
  28. tracking.mail_stats_id.mail_tracking_id = tracking.id
  29. return tracking_emails
  30. def _prepare_sendgrid_data(self):
  31. """
  32. Add unsubscribe options in mass mailings
  33. :return: Sendgrid Email
  34. """
  35. s_mail = super(MailMail, self)._prepare_sendgrid_data()
  36. tracking_settings = TrackingSettings()
  37. if self.mailing_id.enable_unsubscribe:
  38. sub_settings = SubscriptionTracking(
  39. enable=True,
  40. text=self.mailing_id.unsubscribe_text,
  41. html=self.mailing_id.unsubscribe_text,
  42. )
  43. if self.mailing_id.unsubscribe_tag:
  44. sub_settings.substitution_tag = \
  45. self.mailing_id.unsubscribe_tag
  46. tracking_settings.subscription_tracking = sub_settings
  47. else:
  48. tracking_settings.subscription_tracking = SubscriptionTracking(
  49. enable=False)
  50. s_mail.tracking_settings = tracking_settings
  51. return s_mail