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

  1. # Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  2. # Copyright 2017 Vicent Cubells - <vicent.cubells@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo 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, event=None):
  29. recipients = []
  30. if event and event.recipient_address:
  31. recipients.append(event.recipient_address)
  32. else:
  33. recipients = list(filter(None, self.mapped('recipient_address')))
  34. for recipient in recipients:
  35. self.env['mail.mass_mailing.contact'].search([
  36. ('email', '=ilike', recipient)
  37. ]).email_bounced_set(self, reason, event=event)
  38. @api.multi
  39. def smtp_error(self, mail_server, smtp_server, exception):
  40. res = super(MailTrackingEmail, self).smtp_error(
  41. mail_server, smtp_server, exception)
  42. self._contacts_email_bounced_set('error')
  43. return res
  44. @api.multi
  45. def event_create(self, event_type, metadata):
  46. res = super(MailTrackingEmail, self).event_create(event_type, metadata)
  47. if event_type in {'hard_bounce', 'spam', 'reject'}:
  48. self._contacts_email_bounced_set(event_type)
  49. return res