From 2d707d54059e5ac7874c2ccc2f3964ea74e3dd30 Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Tue, 8 Nov 2016 17:18:10 +0100 Subject: [PATCH] [FIX] Save and search recipient_address in lowercase (#125) * [FIX] Save and search recipient_address in lowercase * [FIX] Save partner email in lower and improve tests * Migration script to 8.0.4.0.0 --- mail_tracking/__openerp__.py | 2 +- .../migrations/8.0.4.0.0/pre-migrate.py | 8 +++++ mail_tracking/models/mail_tracking_email.py | 29 ++++++++++++------- mail_tracking/models/res_partner.py | 9 ++++-- mail_tracking/tests/test_mail_tracking.py | 25 ++++++++++++++++ 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 mail_tracking/migrations/8.0.4.0.0/pre-migrate.py diff --git a/mail_tracking/__openerp__.py b/mail_tracking/__openerp__.py index 8530a0d2..10a45f2d 100644 --- a/mail_tracking/__openerp__.py +++ b/mail_tracking/__openerp__.py @@ -5,7 +5,7 @@ { "name": "Email tracking", "summary": "Email tracking system for all mails sent", - "version": "8.0.3.0.1", + "version": "8.0.4.0.0", "category": "Social Network", "website": "http://www.tecnativa.com", "author": "Tecnativa, " diff --git a/mail_tracking/migrations/8.0.4.0.0/pre-migrate.py b/mail_tracking/migrations/8.0.4.0.0/pre-migrate.py new file mode 100644 index 00000000..76b2a0b3 --- /dev/null +++ b/mail_tracking/migrations/8.0.4.0.0/pre-migrate.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +def migrate(cr, version): + """Update database from previous versions, before updating module.""" + cr.execute("UPDATE res_partner SET email=lower(email)") diff --git a/mail_tracking/models/mail_tracking_email.py b/mail_tracking/models/mail_tracking_email.py index 4050aad6..c81bdd94 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): @@ -148,11 +152,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.multi @api.depends('name', 'recipient') diff --git a/mail_tracking/models/res_partner.py b/mail_tracking/models/res_partner.py index f91f3488..faa1db88 100644 --- a/mail_tracking/models/res_partner.py +++ b/mail_tracking/models/res_partner.py @@ -28,10 +28,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_email, reason): @@ -43,6 +45,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 f52e123b..8cea41c8 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({