Skip to content

Commit

Permalink
[8.0][FIX][mail_tracking] Use event recipient address to find partner…
Browse files Browse the repository at this point in the history
…s and contacts to bounce (OCA#133)
  • Loading branch information
antespi authored and chienandalu committed Apr 5, 2018
1 parent 1db9567 commit bc64bee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
27 changes: 20 additions & 7 deletions mail_tracking/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,19 @@ def _get_mail_tracking_img(self):
'tracking_email_id': self.id,
})

def _partners_email_bounced_set(self, reason):
for tracking_email in self:
@api.multi
def _partners_email_bounced_set(self, reason, event=None):
recipients = []
if event and event.recipient_address:
recipients.append(event.recipient_address)
else:
recipients = list(filter(None, self.mapped('recipient_address')))
for recipient in recipients:
self.env['res.partner'].search([
('email', '=ilike', tracking_email.recipient_address)
]).email_bounced_set(tracking_email, reason)
('email', '=ilike', recipient)
]).email_bounced_set(self, reason, event=event)

@api.multi
def smtp_error(self, mail_server, smtp_server, exception):
self.sudo().write({
'error_smtp_server': tools.ustr(smtp_server),
Expand All @@ -206,6 +213,7 @@ def smtp_error(self, mail_server, smtp_server, exception):
self.sudo()._partners_email_bounced_set('error')
return True

@api.multi
def tracking_img_add(self, email):
self.ensure_one()
tracking_url = self._get_mail_tracking_img()
Expand Down Expand Up @@ -233,6 +241,7 @@ def _message_partners_check(self, message, message_id):
})
return True

@api.multi
def _tracking_sent_prepare(self, mail_server, smtp_server, message,
message_id):
self.ensure_one()
Expand Down Expand Up @@ -278,18 +287,22 @@ def _concurrent_events(self, event_type, metadata):
concurrent_event_ids = m_event.search(domain)
return concurrent_event_ids

@api.multi
def event_create(self, event_type, metadata):
event_ids = self.env['mail.tracking.event']
for tracking_email in self:
other_ids = tracking_email._concurrent_events(event_type, metadata)
if not other_ids:
vals = tracking_email._event_prepare(event_type, metadata)
if vals:
event_ids += event_ids.sudo().create(vals)
events = event_ids.sudo().create(vals)
if event_type in {'hard_bounce', 'spam', 'reject'}:
for event in events:
self.sudo()._partners_email_bounced_set(
event_type, event=event)
event_ids += events
else:
_logger.debug("Concurrent event '%s' discarded", event_type)
if event_type in {'hard_bounce', 'spam', 'reject'}:
self.sudo()._partners_email_bounced_set(event_type)
return event_ids

@api.model
Expand Down
18 changes: 18 additions & 0 deletions mail_tracking/models/mail_tracking_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import re
import time
from datetime import datetime

Expand All @@ -16,6 +17,9 @@ class MailTrackingEvent(models.Model):
_description = 'MailTracking event'

recipient = fields.Char(string="Recipient", readonly=True)
recipient_address = fields.Char(
string='Recipient email address', readonly=True, store=True,
compute='_compute_recipient_address', index=True)
timestamp = fields.Float(
string='UTC timestamp', readonly=True,
digits=dp.get_precision('MailTracking Timestamp'))
Expand Down Expand Up @@ -51,6 +55,20 @@ class MailTrackingEvent(models.Model):
error_description = fields.Char(string='Error description', readonly=True)
error_details = fields.Text(string='Error details', readonly=True)

@api.multi
@api.depends('recipient')
def _compute_recipient_address(self):
for email in self:
if email.recipient:
matches = re.search(r'<(.*@.*)>', email.recipient)
if matches:
email.recipient_address = matches.group(1).lower()
else:
email.recipient_address = email.recipient.lower()
else:
email.recipient_address = False

@api.multi
@api.depends('time')
def _compute_date(self):
for email in self:
Expand Down
2 changes: 1 addition & 1 deletion mail_tracking/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _compute_tracking_emails_count(self):
partner.tracking_emails_count = count

@api.multi
def email_bounced_set(self, tracking_email, reason):
def email_bounced_set(self, tracking_emails, reason, event=None):
"""Inherit this method to make any other actions to partners"""
partners = self.filtered(lambda r: not r.email_bounced)
return partners.write({'email_bounced': True})
Expand Down

0 comments on commit bc64bee

Please sign in to comment.