[FIX] mass_mailing: correctly check mailing domain

Currently each change in mailing model resets the mailing domain. Idea behind
that behavior is that domain for a given model will probably not be valid for
another model because of fields being model specific.

However this breaks when creating mailing with default values for mailing
model. Indeed even valid domains are reset to a default void / opt-out-ed
value due to current onchange implementation.

This commit fixes that behavior by resetting only invalid domain i.e.
that crashes when performing a search on target model.

LINKS

Task 1978729
PR #35061
This commit is contained in:
Thibault Delavallée
2019-08-05 08:41:27 +00:00
parent 98bdb241de
commit fd9962cd6a
+19 -10
View File
@@ -13,6 +13,7 @@ from base64 import b64encode
from odoo import api, fields, models, tools, _, SUPERUSER_ID
from odoo.exceptions import UserError
from odoo.osv import expression
from odoo.tools.safe_eval import safe_eval
_logger = logging.getLogger(__name__)
@@ -193,19 +194,27 @@ class MassMailing(models.Model):
else:
mass_mailing.next_departure = cron_time
@api.onchange('mailing_model_id', 'contact_list_ids')
@api.onchange('mailing_model_name', 'contact_list_ids')
def _onchange_model_and_list(self):
mailing_domain = []
mailing_domain = literal_eval(self.mailing_domain) if self.mailing_domain else []
if self.mailing_model_name:
if self.mailing_model_name == 'mailing.list':
if self.contact_list_ids:
mailing_domain.append(('list_ids', 'in', self.contact_list_ids.ids))
else:
mailing_domain.append((0, '=', 1))
elif 'opt_out' in self.env[self.mailing_model_name]._fields and not self.mailing_domain:
mailing_domain.append(('opt_out', '=', False))
if mailing_domain:
try:
self.env[self.mailing_model_real].search(mailing_domain, limit=1)
except:
mailing_domain = []
if not mailing_domain:
if self.mailing_model_name == 'mailing.list':
if self.contact_list_ids:
mailing_domain = [('list_ids', 'in', self.contact_list_ids.ids)]
else:
mailing_domain = [(0, '=', 1)]
elif self.mailing_model_name == 'res.partner':
mailing_domain = [('customer', '=', True)]
elif 'opt_out' in self.env[self.mailing_model_name]._fields and not self.mailing_domain:
mailing_domain = [('opt_out', '=', False)]
else:
mailing_domain.append((0, '=', 1))
mailing_domain = [(0, '=', 1)]
self.mailing_domain = repr(mailing_domain)
@api.onchange('subject')