From bbe469006689c966582e095d3508d0dcecb41dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 1 Oct 2018 16:57:04 +0200 Subject: [PATCH] [FIX] mail: assert email field used in blacklist and correctly use it in computed fields This commits fixes the use of email field in blacklist mixin as it was based on 'email' field instead of the one coming from the _primary_email class attribute since commit 2ff9b379ef80ba6a8744fb9702a8088284372ddd. This commit is related to task ID 33224 (original blacklist implementation done for v12) and its PR #25966 as well as task ID 1889703 (tests and fixes) and its PR #27330. Done with collaboration of @dbeguin. --- addons/mail/models/mail_blacklist.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/addons/mail/models/mail_blacklist.py b/addons/mail/models/mail_blacklist.py index a54179d5732..f7793e98b9d 100644 --- a/addons/mail/models/mail_blacklist.py +++ b/addons/mail/models/mail_blacklist.py @@ -101,11 +101,19 @@ class MailBlackListMixin(models.AbstractModel): store=False, search="_search_is_blacklisted", groups="base.group_user", help="If the email address is on the blacklist, the contact won't receive mass mailing anymore, from any list") + def _assert_primary_email(self): + if not hasattr(self, "_primary_email") or \ + not isinstance(self._primary_email, (list, tuple)) or \ + not len(self._primary_email) == 1: + raise UserError(_('Invalid primary email field on model %s') % self._name) + field_name = self._primary_email[0] + if field_name not in self._fields or self._fields[field_name].type != 'char': + raise UserError(_('Invalid primary email field on model %s') % self._name) + @api.model def _search_is_blacklisted(self, operator, value): # Assumes operator is '=' or '!=' and value is True or False - if not hasattr(self.env[self._name], "_primary_email"): - raise UserError(_('Invalid primary email field on model %s') % self._name) + self._assert_primary_email() if operator != '=': if operator == '!=' and isinstance(value, bool): value = not value @@ -117,17 +125,17 @@ class MailBlackListMixin(models.AbstractModel): query = """ SELECT m.id FROM mail_blacklist bl - JOIN %s m on (LOWER(m.email) = LOWER(bl.email) AND bl.active) + JOIN %s m on (LOWER(m.%s) = LOWER(bl.email) AND bl.active) """ else: query = """ SELECT m.id FROM %s m LEFT JOIN mail_blacklist bl - ON (LOWER(m.email) = LOWER(bl.email) AND bl.active) + ON (LOWER(m.%s) = LOWER(bl.email) AND bl.active) WHERE bl.id IS NULL """ - self._cr.execute(query % self._table) + self._cr.execute(query % (self._table, email_field)) res = self._cr.fetchall() if not res: return [(0, '=', 1)] @@ -135,6 +143,7 @@ class MailBlackListMixin(models.AbstractModel): @api.depends(lambda self: self._primary_email) def _compute_is_blacklisted(self): + self._assert_primary_email() [email_field] = self._primary_email # TODO : Should remove the sudo as compute_sudo defined on methods. # But if user doesn't have access to mail.blacklist, doen't work without sudo().