Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.0][FIX][mail_tracking] Use event recipient address to find partners and contacts to bounce #133

Merged
merged 2 commits into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions mail_tracking/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,16 @@ def _get_mail_tracking_img(self):
})

@api.multi
def _partners_email_bounced_set(self, reason):
for tracking_email in self:
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):
Expand Down Expand Up @@ -293,11 +298,14 @@ def event_create(self, 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
17 changes: 17 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,19 @@ 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):
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 @@ -36,7 +36,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
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def _compute_email_score(self):
email_score_from_email(contact.email)

@api.multi
def email_bounced_set(self, tracking_email, reason):
return self.write({'email_bounced': True})
def email_bounced_set(self, tracking_emails, reason, event=None):
contacts = self.filtered(lambda r: not r.email_bounced)
return contacts.write({'email_bounced': True})

@api.multi
def write(self, vals):
Expand Down
13 changes: 9 additions & 4 deletions mail_tracking_mass_mailing/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ def create(self, vals):
return tracking

@api.multi
def _contacts_email_bounced_set(self, reason):
for tracking_email in self:
def _contacts_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['mail.mass_mailing.contact'].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):
Expand Down