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.

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