[IMP] mass_mailing : improve heuristics to handle seen mails

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) <tde@openerp.com>
This commit is contained in:
David Beguin
2019-04-15 09:25:27 +00:00
committed by Thibault Delavallée
parent 732ec79767
commit aa5faae41c
+20 -11
View File
@@ -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)