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.2 KiB

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