From aa5faae41c6e881f6410349d2f11f40819fcc7bb Mon Sep 17 00:00:00 2001 From: David Beguin Date: Fri, 9 Nov 2018 08:45:59 +0000 Subject: [PATCH] [IMP] mass_mailing : improve heuristics to handle seen mails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the assumption is that if email field doesn't exist on the target model the mail field has to be email_from. So if one model is targeted and does not have a field email nor email_from the seen list will raise as it is searching at least on email_from on the targeted model table. Better heuristics implemented here is to apply the same rule as 'get email field' from MailThread.message_get_default_recipients§). It searches for email_normalized (address mixin), email_from, partner_email and email fields in addition to partner_id which covers all use cases in odoo. Related to task ID 1907292 Linked to PR #28535 Signed-off-by: Thibault Delavallee (tde) --- addons/mass_mailing/models/mass_mailing.py | 31 ++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/addons/mass_mailing/models/mass_mailing.py b/addons/mass_mailing/models/mass_mailing.py index 2c48732e933..98faafd5da8 100644 --- a/addons/mass_mailing/models/mass_mailing.py +++ b/addons/mass_mailing/models/mass_mailing.py @@ -870,17 +870,18 @@ class MassMailing(models.Model): """Returns a set of emails already targeted by current mailing/campaign (no duplicates)""" self.ensure_one() target = self.env[self.mailing_model_real] - if set(['email', 'email_from']) & set(target._fields): - mail_field = 'email' if 'email' in target._fields else 'email_from' - # avoid loading a large number of records in memory - # + use a basic heuristic for extracting emails - query = """ - SELECT lower(substring(t.%(mail_field)s, '([^ ,;<@]+@[^> ,;]+)')) - FROM mail_mail_statistics s - JOIN %(target)s t ON (s.res_id = t.id) - WHERE substring(t.%(mail_field)s, '([^ ,;<@]+@[^> ,;]+)') IS NOT NULL - """ - elif 'partner_id' in target._fields: + + # avoid loading a large number of records in memory + # + use a basic heuristic for extracting emails + query = """ + SELECT lower(substring(t.%(mail_field)s, '([^ ,;<@]+@[^> ,;]+)')) + FROM mail_mail_statistics s + JOIN %(target)s t ON (s.res_id = t.id) + WHERE substring(t.%(mail_field)s, '([^ ,;<@]+@[^> ,;]+)') IS NOT NULL + """ + + # Apply same 'get email field' rule from mail_thread.message_get_default_recipients + if 'partner_id' in target._fields: mail_field = 'email' query = """ SELECT lower(substring(p.%(mail_field)s, '([^ ,;<@]+@[^> ,;]+)')) @@ -889,6 +890,14 @@ class MassMailing(models.Model): JOIN res_partner p ON (t.partner_id = p.id) WHERE substring(p.%(mail_field)s, '([^ ,;<@]+@[^> ,;]+)') IS NOT NULL """ + elif issubclass(type(target), self.pool['mail.address.mixin']): + mail_field = 'email_normalized' + elif 'email_from' in target._fields: + mail_field = 'email_from' + elif 'partner_email' in target._fields: + mail_field = 'partner_email' + elif 'email' in target._fields: + mail_field = 'email' else: raise UserError(_("Unsupported mass mailing model %s") % self.mailing_model_id.name)