[FIX] mail_tracking: Don't call write inside other write

Or infinite recursions will happen on other `write` overwrites, like the one that happens
on `mass_mailing_partner`.
This commit is contained in:
Pedro M. Baeza 2019-06-26 20:40:11 +02:00 committed by Jasmin Solanki
parent cd1d3be640
commit 71b67a5475
2 changed files with 16 additions and 10 deletions

View File

@ -1,12 +1,13 @@
# Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com> # Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
# Copyright 2018 David Vidal - <david.vidal@tecnativa.com> # Copyright 2018 David Vidal - <david.vidal@tecnativa.com>
# Copyright 2018 Tecnativa - Ernesto Tejeda # Copyright 2018 Tecnativa - Ernesto Tejeda
# Copyright 2019 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{ {
"name": "Email tracking", "name": "Email tracking",
"summary": "Email tracking system for all mails sent", "summary": "Email tracking system for all mails sent",
"version": "12.0.1.0.0", "version": "12.0.1.0.1",
"category": "Social Network", "category": "Social Network",
"website": "http://github.com/OCA/social", "website": "http://github.com/OCA/social",
"author": "Tecnativa, " "author": "Tecnativa, "

View File

@ -24,20 +24,25 @@ class MailBouncedMixin(models.AbstractModel):
def email_bounced_set(self, tracking_emails, reason, event=None): def email_bounced_set(self, tracking_emails, reason, event=None):
"""Inherit this method to make any other actions to the model that """Inherit this method to make any other actions to the model that
inherit the mixin""" inherit the mixin"""
if self.env.context.get('write_loop'):
# We avoid with the context an infinite recursion calling write
# method from other write method.
return True
partners = self.filtered(lambda r: not r.email_bounced) partners = self.filtered(lambda r: not r.email_bounced)
return partners.write({'email_bounced': True}) return partners.write({'email_bounced': True})
def write(self, vals): def write(self, vals):
[email_field] = self._primary_email [email_field] = self._primary_email
if email_field not in vals: if email_field not in vals:
return super(MailBouncedMixin, self).write(vals) return super().write(vals)
email = vals[email_field].lower() if vals[email_field] else False email = vals[email_field].lower() if vals[email_field] else False
mte_obj = self.env['mail.tracking.email'] mte_obj = self.env['mail.tracking.email']
if not mte_obj.email_is_bounced(email): vals['email_bounced'] = mte_obj.email_is_bounced(email)
vals['email_bounced'] = False if vals['email_bounced']:
return super(MailBouncedMixin, self).write(vals) res = mte_obj._email_last_tracking_state(email)
res = mte_obj._email_last_tracking_state(email) tracking = mte_obj.browse(res[0].get('id'))
tracking = mte_obj.browse(res[0].get('id')) event = tracking.tracking_event_ids[:1]
event = tracking.tracking_event_ids[:1] self.with_context(
self.email_bounced_set(tracking, event.error_details, event) write_loop=True,
return super(MailBouncedMixin, self).write(vals) ).email_bounced_set(tracking, event.error_details, event)
return super().write(vals)