diff --git a/mail_tracking/models/mail_tracking_email.py b/mail_tracking/models/mail_tracking_email.py index 6b2944be..d498ad66 100644 --- a/mail_tracking/models/mail_tracking_email.py +++ b/mail_tracking/models/mail_tracking_email.py @@ -126,6 +126,13 @@ class MailTrackingEmail(models.Model): # Consider only last 10 tracking emails return self.sorted(key=lambda r: r.time, reverse=True)[:10] + @api.model + def email_score_from_email(self, email): + trackings = self.env['mail.tracking.email'].search([ + ('recipient_address', '=ilike', email) + ]) + return trackings.email_score() + @api.multi def email_score(self): """Default email score algorimth""" diff --git a/mail_tracking/models/res_partner.py b/mail_tracking/models/res_partner.py index c4eb294a..908f1190 100644 --- a/mail_tracking/models/res_partner.py +++ b/mail_tracking/models/res_partner.py @@ -40,6 +40,7 @@ class ResPartner(models.Model): def write(self, vals): email = vals.get('email') if email is not None: - vals['tracking_email_ids'] = \ - self.env['mail.tracking.email']._tracking_ids_to_write(email) + m_track = self.env['mail.tracking.email'] + vals['tracking_email_ids'] = m_track._tracking_ids_to_write(email) + vals['email_score'] = m_track.email_score_from_email(email) return super(ResPartner, self).write(vals) diff --git a/mail_tracking/tests/test_mail_tracking.py b/mail_tracking/tests/test_mail_tracking.py index a6eacc60..5e855f3b 100644 --- a/mail_tracking/tests/test_mail_tracking.py +++ b/mail_tracking/tests/test_mail_tracking.py @@ -88,11 +88,11 @@ class TestMailTracking(TransactionCase): tracking_email.event_create('open', metadata) self.assertEqual(tracking_email.state, 'opened') - def mail_send(self): + def mail_send(self, recipient): mail = self.env['mail.mail'].create({ 'subject': 'Test subject', 'email_from': 'from@domain.com', - 'email_to': 'to@domain.com', + 'email_to': recipient, 'body_html': '

This is a test message

', }) mail.send() @@ -106,7 +106,7 @@ class TestMailTracking(TransactionCase): controller = MailTrackingController() db = self.env.cr.dbname image = base64.decodestring(BLANK) - mail, tracking = self.mail_send() + mail, tracking = self.mail_send(self.recipient.email) self.assertEqual(mail.email_to, tracking.recipient) self.assertEqual(mail.email_from, tracking.sender) with mock.patch(mock_request) as mock_func: @@ -115,7 +115,7 @@ class TestMailTracking(TransactionCase): self.assertEqual(image, res.response[0]) def test_concurrent_open(self): - mail, tracking = self.mail_send() + mail, tracking = self.mail_send(self.recipient.email) ts = time.time() metadata = { 'ip': '127.0.0.1', @@ -146,7 +146,7 @@ class TestMailTracking(TransactionCase): self.assertEqual(len(opens), 2) def test_concurrent_click(self): - mail, tracking = self.mail_send() + mail, tracking = self.mail_send(self.recipient.email) ts = time.time() metadata = { 'ip': '127.0.0.1', @@ -188,11 +188,66 @@ class TestMailTracking(TransactionCase): def test_smtp_error(self): with mock.patch(mock_send_email) as mock_func: mock_func.side_effect = Warning('Test error') - mail, tracking = self.mail_send() + mail, tracking = self.mail_send(self.recipient.email) self.assertEqual('error', tracking.state) self.assertEqual('Warning', tracking.error_type) self.assertEqual('Test error', tracking.error_description) + def test_partner_email_change(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('open', {}) + orig_score = self.recipient.email_score + orig_email = self.recipient.email + self.recipient.email = orig_email + '2' + self.assertEqual(50.0, self.recipient.email_score) + self.recipient.email = orig_email + self.assertEqual(orig_score, self.recipient.email_score) + + def test_process_hard_bounce(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('hard_bounce', {}) + self.assertEqual('bounced', tracking.state) + + def test_process_soft_bounce(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('soft_bounce', {}) + self.assertEqual('soft-bounced', tracking.state) + + def test_process_delivered(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('delivered', {}) + self.assertEqual('delivered', tracking.state) + + def test_process_deferral(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('deferral', {}) + self.assertEqual('deferred', tracking.state) + + def test_process_spam(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('spam', {}) + self.assertEqual('spam', tracking.state) + + def test_process_unsub(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('unsub', {}) + self.assertEqual('unsub', tracking.state) + + def test_process_reject(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('reject', {}) + self.assertEqual('rejected', tracking.state) + + def test_process_open(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('open', {}) + self.assertEqual('opened', tracking.state) + + def test_process_click(self): + mail, tracking = self.mail_send(self.recipient.email) + tracking.event_create('click', {}) + self.assertEqual('opened', tracking.state) + def test_db(self): db = self.env.cr.dbname controller = MailTrackingController()