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.

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