Browse Source

[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
pull/130/head
Antonio Espinosa 8 years ago
committed by Holger Brunn
parent
commit
2d707d5405
  1. 2
      mail_tracking/__openerp__.py
  2. 8
      mail_tracking/migrations/8.0.4.0.0/pre-migrate.py
  3. 29
      mail_tracking/models/mail_tracking_email.py
  4. 9
      mail_tracking/models/res_partner.py
  5. 25
      mail_tracking/tests/test_mail_tracking.py

2
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, "

8
mail_tracking/migrations/8.0.4.0.0/pre-migrate.py

@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
# 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)")

29
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')

9
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))

25
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({

Loading…
Cancel
Save