Browse Source

[FIX] Save and search recipient_address in lowercase (#124)

pull/147/head
Antonio Espinosa 8 years ago
committed by Pedro M. Baeza
parent
commit
b0455e9448
  1. 29
      mail_tracking/models/mail_tracking_email.py
  2. 9
      mail_tracking/models/res_partner.py
  3. 25
      mail_tracking/tests/test_mail_tracking.py

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):
@ -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):

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

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