Skip to content

Commit

Permalink
Speed installation time and discard concurrent events (OCA#82)
Browse files Browse the repository at this point in the history
[IMP] mail_tracking: Speed installation time, discard concurrent events and other fixes
  • Loading branch information
antespi authored and cubells committed Jan 3, 2017
1 parent df0aeb6 commit 22cf06b
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 45 deletions.
2 changes: 1 addition & 1 deletion mail_tracking_mass_mailing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import models
from .hooks import post_init_hook
from .hooks import pre_init_hook
4 changes: 2 additions & 2 deletions mail_tracking_mass_mailing/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Mail tracking for mass mailing",
"summary": "Improve mass mailing email tracking",
"version": "8.0.1.0.0",
"version": "8.0.1.0.1",
"category": "Social Network",
"website": "http://www.tecnativa.com",
"author": "Tecnativa, "
Expand All @@ -24,5 +24,5 @@
"views/mail_mass_mailing_view.xml",
"views/mail_mass_mailing_contact_view.xml",
],
"post_init_hook": "post_init_hook",
"pre_init_hook": "pre_init_hook",
}
30 changes: 11 additions & 19 deletions mail_tracking_mass_mailing/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
from openerp import api, SUPERUSER_ID
try:
from openerp.addons.mail_tracking.hooks import column_add_with_value
except ImportError:
column_add_with_value = False

_logger = logging.getLogger(__name__)


def post_init_hook(cr, registry):
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
# Recalculate all mass_mailing contacts tracking_email_ids
contacts = env['mail.mass_mailing.contact'].search([
('email', '!=', False),
])
emails = contacts.mapped('email')
_logger.info(
"Recalculating 'tracking_email_ids' in "
"'mail.mass_mailing.contact' model for %d email addresses",
len(emails))
for n, email in enumerate(emails):
env['mail.tracking.email'].tracking_ids_recalculate(
'mail.mass_mailing.contact', 'email', 'tracking_email_ids',
email)
if n % 500 == 0: # pragma: no cover
_logger.info(" Recalculated %d of %d", n, len(emails))
def pre_init_hook(cr):
if column_add_with_value:
_logger.info("Creating mail.mass_mailing.contact.email_score column "
"with value 50.0")
column_add_with_value(
cr, 'mail_mass_mailing_contact', 'email_score', 'double precision',
50.0)
2 changes: 0 additions & 2 deletions mail_tracking_mass_mailing/models/mail_mail_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ class MailMailStatistics(models.Model):
tracking_event_ids = fields.One2many(
string="Tracking events", comodel_name='mail.tracking.event',
related='mail_tracking_id.tracking_event_ids', readonly=True)
tracking_state = fields.Selection(
string="State", related='mail_tracking_id.state', store=True)
11 changes: 5 additions & 6 deletions mail_tracking_mass_mailing/models/mail_mass_mailing_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ class MailMassMailingContact(models.Model):
string="Tracking emails", comodel_name="mail.tracking.email",
readonly=True)
email_score = fields.Float(
string="Email score",
compute="_compute_email_score", store=True, readonly=True)
string="Email score", readonly=True, default=50.0)

@api.one
@api.depends('tracking_email_ids', 'tracking_email_ids.state')
def _compute_email_score(self):
self.email_score = self.tracking_email_ids.email_score()
@api.multi
def email_score_calculate(self):
for contact in self:
contact.email_score = contact.tracking_email_ids.email_score()

@api.multi
def write(self, vals):
Expand Down
11 changes: 11 additions & 0 deletions mail_tracking_mass_mailing/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ def create(self, vals):
'mail.mass_mailing.contact', 'email', 'tracking_email_ids',
tracking.recipient_address, new_tracking=tracking)
return tracking

@api.multi
def event_create(self, event_type, metadata):
res = super(MailTrackingEmail, self).event_create(event_type, metadata)
for tracking_email in self:
contacts = self.tracking_ids_recalculate(
'mail.mass_mailing.contact', 'email', 'tracking_email_ids',
tracking_email.recipient_address)
if contacts:
contacts.email_score_calculate()
return res
32 changes: 32 additions & 0 deletions mail_tracking_mass_mailing/models/mail_tracking_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,35 @@ def process_open(self, tracking_email, metadata):
mail_mail_stats = self.sudo().env['mail.mail.statistics']
mail_mail_stats.set_opened(mail_mail_ids=[tracking_email.mail_id_int])
return res

def _tracking_set_bounce(self, tracking_email, metadata):
mail_mail_stats = self.sudo().env['mail.mail.statistics']
mail_mail_stats.set_bounced(mail_mail_ids=[tracking_email.mail_id_int])

@api.model
def process_hard_bounce(self, tracking_email, metadata):
res = super(MailTrackingEvent, self).process_hard_bounce(
tracking_email, metadata)
self._tracking_set_bounce(tracking_email, metadata)
return res

@api.model
def process_soft_bounce(self, tracking_email, metadata):
res = super(MailTrackingEvent, self).process_soft_bounce(
tracking_email, metadata)
self._tracking_set_bounce(tracking_email, metadata)
return res

@api.model
def process_reject(self, tracking_email, metadata):
res = super(MailTrackingEvent, self).process_reject(
tracking_email, metadata)
self._tracking_set_bounce(tracking_email, metadata)
return res

@api.model
def process_spam(self, tracking_email, metadata):
res = super(MailTrackingEvent, self).process_spam(
tracking_email, metadata)
self._tracking_set_bounce(tracking_email, metadata)
return res
29 changes: 28 additions & 1 deletion mail_tracking_mass_mailing/tests/test_mass_mailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,34 @@ def test_tracking_email_link(self):
}
tracking_email.event_create('open', metadata)
self.assertTrue(stat.opened)
self.assertEqual(stat.tracking_state, 'opened')

def _tracking_email_bounce(self, event_type, state):
self.mailing.send_mail()
for stat in self.mailing.statistics_ids:
if stat.mail_mail_id:
stat.mail_mail_id.send()
tracking_email = self.env['mail.tracking.email'].search([
('mail_id_int', '=', stat.mail_mail_id_int),
])
# And now mark the email as bounce
metadata = {
'bounce_type': '499',
'bounce_description': 'Unable to connect to MX servers',
}
tracking_email.event_create(event_type, metadata)
self.assertTrue(stat.bounced)

def test_tracking_email_hard_bounce(self):
self._tracking_email_bounce('hard_bounce', 'bounced')

def test_tracking_email_soft_bounce(self):
self._tracking_email_bounce('soft_bounce', 'soft-bounced')

def test_tracking_email_reject(self):
self._tracking_email_bounce('reject', 'rejected')

def test_tracking_email_spam(self):
self._tracking_email_bounce('spam', 'spam')

def test_contact_tracking_emails(self):
self.mailing.send_mail()
Expand Down
14 changes: 0 additions & 14 deletions mail_tracking_mass_mailing/views/mail_mail_statistics_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
<field name="model">mail.mail.statistics</field>
<field name="inherit_id" ref="mass_mailing.view_mail_mail_statistics_form"/>
<field name="arch" type="xml">
<field name="res_id" position="after">
<field name="tracking_state"/>
</field>
<xpath expr="//form" position="inside">
<group>
<field name="mail_tracking_id" />
Expand All @@ -34,16 +31,5 @@
</field>
</record>

<record model="ir.ui.view" id="view_mail_mail_statistics_tree">
<field name="name">Add partner and state columns</field>
<field name="model">mail.mail.statistics</field>
<field name="inherit_id" ref="mass_mailing.view_mail_mail_statistics_tree"/>
<field name="arch" type="xml">
<field name="sent" position="before">
<field name="tracking_state"/>
</field>
</field>
</record>

</data>
</openerp>

0 comments on commit 22cf06b

Please sign in to comment.