Skip to content

Commit

Permalink
[IMP] mail_tracking: email score performance (OCA#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
chienandalu authored and ernestotejeda committed Dec 18, 2018
1 parent 58bf8ba commit 149fe1c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mail_tracking/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Email tracking",
"summary": "Email tracking system for all mails sent",
"version": "11.0.1.0.0",
"version": "11.0.1.1.0",
"category": "Social Network",
"website": "http://github.com/OCA/social",
"author": "Tecnativa, "
Expand Down
21 changes: 14 additions & 7 deletions mail_tracking/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ def email_is_bounced(self, email):

@api.model
def email_score_from_email(self, email):
if email:
return self.search([
('recipient_address', '=', email.lower())]).email_score()
return 0.
if not email:
return 0.
data = self.read_group([('recipient_address', '=', email.lower())],
['recipient_address', 'state'], ['state'])
mapped_data = {state['state']: state['state_count'] for state in data}
return self.with_context(mt_states=mapped_data).email_score()

@api.model
def _email_score_weights(self):
Expand All @@ -124,16 +126,21 @@ def _email_score_weights(self):

def email_score(self):
"""Default email score algorimth. Ready to be inherited
It can receive a recordset or mapped states dictionary via context.
Must return a value beetwen 0.0 and 100.0
- Bad reputation: Value between 0 and 50.0
- Unknown reputation: Value 50.0
- Good reputation: Value between 50.0 and 100.0
"""
weights = self._email_score_weights()
score = 50.0
for tracking in self:
score += weights.get(tracking.state, 0.0)
states = self.env.context.get('mt_states', False)
if states:
for state in states.keys():
score += weights.get(state, 0.0) * states[state]
else:
for tracking in self:
score += weights.get(tracking.state, 0.0)
if score > 100.0:
score = 100.0
elif score < 0.0:
Expand Down
9 changes: 9 additions & 0 deletions mail_tracking/tests/test_mail_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ def test_process_several_bounce(self):
self.assertEqual('bounced', tracking.state)
self.assertEqual(0.0, self.recipient.email_score)

def test_recordset_email_score(self):
"""For backwords compatibility sake"""
trackings = self.env['mail.tracking.email']
for i in range(11):
mail, tracking = self.mail_send(self.recipient.email)
tracking.event_create('click', {})
trackings |= tracking
self.assertEqual(100.0, trackings.email_score())

def test_db(self):
db = self.env.cr.dbname
controller = MailTrackingController()
Expand Down

0 comments on commit 149fe1c

Please sign in to comment.