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.

53 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. class MailTrackingEmail(models.Model):
  6. _inherit = "mail.tracking.email"
  7. mass_mailing_id = fields.Many2one(
  8. string="Mass mailing", comodel_name='mail.mass_mailing', readonly=True)
  9. mail_stats_id = fields.Many2one(
  10. string="Mail statistics", comodel_name='mail.mail.statistics',
  11. readonly=True)
  12. mail_id_int = fields.Integer(string="Mail ID", readonly=True)
  13. @api.model
  14. def _statistics_link_prepare(self, tracking):
  15. """Inherit this method to link other object to mail.mail.statistics"""
  16. return {
  17. 'mail_tracking_id': tracking.id,
  18. }
  19. @api.model
  20. def create(self, vals):
  21. tracking = super(MailTrackingEmail, self).create(vals)
  22. # Link mail statistics with this tracking
  23. if tracking.mail_stats_id:
  24. tracking.mail_stats_id.write(
  25. self._statistics_link_prepare(tracking))
  26. return tracking
  27. @api.multi
  28. def _contacts_email_bounced_set(self, reason):
  29. for tracking_email in self:
  30. self.env['mail.mass_mailing.contact'].search([
  31. ('email', '=ilike', tracking_email.recipient_address)
  32. ]).email_bounced_set(tracking_email, reason)
  33. @api.multi
  34. def smtp_error(self, mail_server, smtp_server, exception):
  35. res = super(MailTrackingEmail, self).smtp_error(
  36. mail_server, smtp_server, exception)
  37. self._contacts_email_bounced_set('error')
  38. return res
  39. @api.multi
  40. def event_create(self, event_type, metadata):
  41. res = super(MailTrackingEmail, self).event_create(event_type, metadata)
  42. if event_type in {'hard_bounce', 'spam', 'reject'}:
  43. self._contacts_email_bounced_set(event_type)
  44. return res