From b0455e94480bbbcd36ec8908203c708c479cb35f Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Tue, 8 Nov 2016 17:18:23 +0100 Subject: [PATCH] [FIX] Save and search recipient_address in lowercase (#124) --- mail_tracking/models/mail_tracking_email.py | 29 +++++++++++++-------- mail_tracking/models/res_partner.py | 9 ++++--- mail_tracking/tests/test_mail_tracking.py | 25 ++++++++++++++++++ 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/mail_tracking/models/mail_tracking_email.py b/mail_tracking/models/mail_tracking_email.py index 940cd9d0..8a51112c 100644 --- a/mail_tracking/models/mail_tracking_email.py +++ b/mail_tracking/models/mail_tracking_email.py @@ -100,16 +100,20 @@ class MailTrackingEmail(models.Model): @api.model def email_is_bounced(self, email): - return len(self._email_score_tracking_filter([ - ('recipient_address', '=ilike', email), - ('state', 'in', ('error', 'rejected', 'spam', 'bounced')), - ])) > 0 + if email: + return len(self._email_score_tracking_filter([ + ('recipient_address', '=', email.lower()), + ('state', 'in', ('error', 'rejected', 'spam', 'bounced')), + ])) > 0 + return False @api.model def email_score_from_email(self, email): - return self._email_score_tracking_filter([ - ('recipient_address', '=ilike', email) - ]).email_score() + if email: + return self._email_score_tracking_filter([ + ('recipient_address', '=', email.lower()) + ]).email_score() + return 0. @api.model def _email_score_weights(self): @@ -146,11 +150,14 @@ class MailTrackingEmail(models.Model): @api.depends('recipient') def _compute_recipient_address(self): for email in self: - matches = re.search(r'<(.*@.*)>', email.recipient) - if matches: - email.recipient_address = matches.group(1) + 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 = email.recipient + email.recipient_address = False @api.depends('name', 'recipient') def _compute_tracking_display_name(self): diff --git a/mail_tracking/models/res_partner.py b/mail_tracking/models/res_partner.py index 7360e648..5d10ac81 100644 --- a/mail_tracking/models/res_partner.py +++ b/mail_tracking/models/res_partner.py @@ -27,10 +27,12 @@ class ResPartner(models.Model): @api.depends('email') def _compute_tracking_emails_count(self): for partner in self: - partner.tracking_emails_count = self.env['mail.tracking.email'].\ - search_count([ - ('recipient_address', '=ilike', partner.email) + count = 0 + if partner.email: + count = self.env['mail.tracking.email'].search_count([ + ('recipient_address', '=', partner.email.lower()) ]) + partner.tracking_emails_count = count @api.multi def email_bounced_set(self, tracking_emails, reason, event=None): @@ -41,6 +43,7 @@ class ResPartner(models.Model): def write(self, vals): email = vals.get('email') if email is not None: + vals['email'] = email.lower() if email else False vals['email_bounced'] = ( bool(email) and self.env['mail.tracking.email'].email_is_bounced(email)) diff --git a/mail_tracking/tests/test_mail_tracking.py b/mail_tracking/tests/test_mail_tracking.py index 4e5dea88..7a6ea83b 100644 --- a/mail_tracking/tests/test_mail_tracking.py +++ b/mail_tracking/tests/test_mail_tracking.py @@ -52,6 +52,31 @@ class TestMailTracking(TransactionCase): http.request = self.last_request return super(TestMailTracking, self).tearDown(*args, **kwargs) + def test_email_lower(self): + self.recipient.write({'email': 'UPPER@example.com'}) + self.assertEqual('upper@example.com', self.recipient.email) + + def test_empty_email(self): + self.recipient.write({'email_bounced': True}) + self.recipient.write({'email': False}) + self.assertEqual(False, self.recipient.email) + self.assertEqual(False, self.recipient.email_bounced) + self.recipient.write({'email_bounced': True}) + self.recipient.write({'email': ''}) + self.assertEqual(False, self.recipient.email) + self.assertEqual(False, self.recipient.email_bounced) + self.assertEqual( + False, + self.env['mail.tracking.email'].email_is_bounced(False)) + self.assertEqual( + 0., + self.env['mail.tracking.email'].email_score_from_email(False)) + + def test_recipient_address_compute(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.write({'recipient': False}) + self.assertEqual(False, tracking.recipient_address) + def test_message_post(self): # This message will generate a notification for recipient message = self.env['mail.message'].create({