[IMP] mail, mass_mailing, mass_mailing_crm : Use normalized email in unsubscribe link

Use the email_normalized from mail.address.mixin if available to get always
a normalized email address in the unsubscribe link. That allows to simplify
the search on email address in the controllers -> Check blacklist,
send feedback, etc..

Task ID 1896677
This commit is contained in:
David Beguin
2018-11-20 11:37:05 +00:00
parent 0fe50a01d2
commit fc2a8d63ee
7 changed files with 40 additions and 19 deletions
+2
View File
@@ -1695,6 +1695,8 @@ class MailThread(models.AbstractModel):
recipient_ids, email_to, email_cc = set(), False, False
if 'partner_id' in self._fields and record.partner_id:
recipient_ids.add(record.partner_id.id)
elif 'email_normalized' in self._fields and record.email_normalized:
email_to = record.email_normalized
elif 'email_from' in self._fields and record.email_from:
email_to = record.email_from
elif 'partner_email' in self._fields and record.partner_email:
+4 -4
View File
@@ -39,7 +39,7 @@ class MassMailController(http.Controller):
# Unsubscribe directly + Let the user choose his subscriptions
mailing.update_opt_out(email, mailing.contact_list_ids.ids, True)
contacts = request.env['mail.mass_mailing.contact'].sudo().search([('email_normalized', '=ilike', email)])
contacts = request.env['mail.mass_mailing.contact'].sudo().search([('email_normalized', '=', email)])
subscription_list_ids = contacts.mapped('subscription_list_ids')
# In many user are found : if user is opt_out on the list with contact_id 1 but not with contact_id 2,
# assume that the user is not opt_out on both
@@ -63,7 +63,7 @@ class MassMailController(http.Controller):
})
else:
opt_in_lists = request.env['mail.mass_mailing.list_contact_rel'].sudo().search([
('contact_id.email', '=', email),
('contact_id.email_normalized', '=', email),
('opt_out', '=', False)
]).mapped('list_id')
blacklist_rec = request.env['mail.blacklist'].sudo()._add(email)
@@ -115,7 +115,7 @@ class MassMailController(http.Controller):
if not self._valid_unsubscribe_token(mailing_id, res_id, email, token):
return 'unauthorized'
if email:
record = request.env['mail.blacklist'].sudo().with_context(active_test=False).search([('email', '=ilike', email)])
record = request.env['mail.blacklist'].sudo().with_context(active_test=False).search([('email', '=', email)])
if record['active']:
return True
return False
@@ -152,7 +152,7 @@ class MassMailController(http.Controller):
if not self._valid_unsubscribe_token(mailing_id, res_id, email, token):
return 'unauthorized'
model = request.env[mailing.mailing_model_real]
records = model.sudo().search([('email_normalized', '=ilike', email)])
records = model.sudo().search([('email_normalized', '=', email)])
for record in records:
record.sudo().message_post(body=_("Feedback from %s: %s" % (email, feedback)))
return bool(records)
+2 -2
View File
@@ -282,7 +282,7 @@ class MassMailingContact(models.Model):
@api.multi
def message_get_default_recipients(self):
return dict((record.id, {'partner_ids': [], 'email_to': record.email, 'email_cc': False}) for record in self)
return dict((record.id, {'partner_ids': [], 'email_to': record.email_normalized, 'email_cc': False}) for record in self)
class MassMailingStage(models.Model):
@@ -646,7 +646,7 @@ class MassMailing(models.Model):
def update_opt_out(self, email, list_ids, value):
if len(list_ids) > 0:
model = self.env['mail.mass_mailing.contact'].with_context(active_test=False)
records = model.search([('email', '=ilike', email)])
records = model.search([('email_normalized', '=', email)])
opt_out_records = self.env['mail.mass_mailing.list_contact_rel'].search([
('contact_id', 'in', records.ids),
('list_id', 'in', list_ids),
@@ -71,18 +71,18 @@
<p>You have been successfully <strong>unsubscribed</strong>!</p>
</div>
<div t-if="list_ids" class="alert alert-warning">
<p class="text-center">You are currently subscribed to those newsletters. Unsubscribing means you will not receive any news from them anymore:</p>
<ul class="list-group mb-4">
<t t-foreach="list_ids" t-as="list_id">
<t t-if="list_id.is_public == True">
<li class="list-group-item bg-transparent">
<strong><t t-esc="list_id.name"/></strong>
</li>
</t>
</t>
</ul>
</div>
<div t-if="list_ids" class="alert alert-warning">
<p class="text-center">You were still subscribed to those newsletters. You will not receive any news from them anymore:</p>
<ul class="list-group mb-4">
<t t-foreach="list_ids" t-as="list_id">
<t t-if="list_id.is_public == True">
<li class="list-group-item bg-transparent">
<strong><t t-esc="list_id.name"/></strong>
</li>
</t>
</t>
</ul>
</div>
<div t-if="show_blacklist_button" class="mb64">
<div class="btn btn-secondary pull-right" id="button_add_blacklist" style="display:none">Blacklist Me</div>
@@ -2,3 +2,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import mass_mailing
from . import crm_lead
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models
class Lead(models.Model):
_name = "crm.lead"
_inherit = ['crm.lead']
@api.multi
def message_get_default_recipients(self):
return {
r.id : {'partner_ids': [],
'email_to': r.email_normalized,
'email_cc': False}
for r in self.sudo()
}
@@ -49,7 +49,7 @@ class TestMassMailPerformance(TestMassMailPerformanceBase):
'mailing_domain': [('id', 'in', self.mm_recs.ids)],
})
with self.assertQueryCount(__system__=2433, marketing=3089):
with self.assertQueryCount(__system__=2435, marketing=3091):
mailing.send_mail()
self.assertEqual(mailing.sent, 50)