[ADD] mass_mailing_custom_unsubscribe_event: Allow to unsubscribe discretely from an event

This addon extends the unsubscription process for allowing to unsubscribe
only for an event.

Standard process includes the mail in the general blacklist instead, which
can be very unconvenient.

This includes also the needed changes in the base module `mass_mailing_custom_unsubscribe`.
This commit is contained in:
Pedro M. Baeza 2020-06-19 12:47:06 +02:00 committed by Ernesto Tejeda
parent d3aacdfbc2
commit c2ac2eb4d4
8 changed files with 114 additions and 17 deletions

View File

@ -33,6 +33,9 @@ This addon extends the unsubscription form to let you:
mass mailing.
- Provide proof on why you are sending mass mailings to a given contact, as
required by the GDPR in Europe.
- Handle discrete unsubscriptions from other recipients that are not a mailing
list. On standard module, unsubscriptions from these recipients directly
include that mail on the general blacklist.
**Table of contents**
@ -50,6 +53,15 @@ they are going to unsubscribe. To do it:
#. If *Details required* is enabled, they will have to fill a text area to
continue.
For having discrete unsubscriptions from other recipients than the mailing
lists, you need to add a glue module that adds 2 fields in the associated
model:
- `opt_out`.
- Either `email` or `email_from`.
See `mass_mailing_custom_unsubscribe_event` for an example.
Usage
=====
@ -98,6 +110,7 @@ Contributors
* Jairo Llopis
* David Vidal
* Ernesto Tejeda
* Pedro M. Baeza
Maintainers
~~~~~~~~~~~

View File

@ -1,11 +1,12 @@
# Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
# Copyright 2018 David Vidal <david.vidal@tecnativa.com>
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Customizable unsubscription process on mass mailing emails',
'summary': 'Know and track (un)subscription reasons, GDPR compliant',
'category': 'Marketing',
'version': '12.0.1.0.0',
'version': '12.0.1.0.1',
'depends': [
'mass_mailing',
],

View File

@ -1,5 +1,6 @@
# Copyright 2015 Antiun Ingeniería S.L. (http://www.antiun.com)
# Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging
@ -43,6 +44,7 @@ class CustomUnsubscribe(MassMailController):
"Called `mailing()` with: %r",
(mailing_id, email, res_id, token, post))
reasons = request.env["mail.unsubscription.reason"].search([])
res_id = res_id and int(res_id)
try:
# Check if we already have a reason for unsubscription
reason_id = int(post["reason_id"])
@ -53,23 +55,38 @@ class CustomUnsubscribe(MassMailController):
# Unsubscribe, saving reason and details by context
details = post.get("details", False)
self._add_extra_context(mailing_id, res_id, reason_id, details)
# You could get a DetailsRequiredError here, but only if HTML5
# validation fails, which should not happen in modern browsers
result = super().mailing(
mailing_id, email, res_id, token=token, **post)
result.qcontext.update({"reasons": reasons})
# update list_ids taking into account not_cross_unsubscriptable
# field
mailing_obj = request.env['mail.mass_mailing']
mailing = mailing_obj.sudo().browse(mailing_id)
if mailing.mailing_model_real == 'mail.mass_mailing.contact':
result.qcontext.update({
"list_ids": result.qcontext["list_ids"].filtered(
lambda mailing_list:
not mailing_list.not_cross_unsubscriptable or
mailing_list in mailing.contact_list_ids
)
mass_mailing = mailing_obj.sudo().browse(mailing_id)
model = mass_mailing.mailing_model_real
if "opt_out" in request.env[model]._fields:
mass_mailing.update_opt_out_other(email, [res_id], True)
result = request.render("mass_mailing.page_unsubscribed", {
"email": email,
"mailing_id": mailing_id,
"res_id": res_id,
"show_blacklist_button": request.env[
"ir.config_parameter"
].sudo().get_param(
"mass_mailing.show_blacklist_buttons"
),
})
result.qcontext.update({"reasons": reasons})
else:
# You could get a DetailsRequiredError here, but only if HTML5
# validation fails, which should not happen in modern browsers
result = super().mailing(
mailing_id, email, res_id, token=token, **post)
if model == "mail.mass_mailing.contact":
# update list_ids taking into account
# not_cross_unsubscriptable field
result.qcontext.update({
"reasons": reasons,
"list_ids": result.qcontext["list_ids"].filtered(
lambda mailing_list:
not mailing_list.not_cross_unsubscriptable or
mailing_list in mass_mailing.contact_list_ids
)
})
return result
@route()

View File

@ -1,7 +1,9 @@
# Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models
from odoo import models, tools
from odoo.tools.safe_eval import safe_eval
from itertools import groupby
@ -31,3 +33,42 @@ class MailMassMailing(models.Model):
"action": action,
})
return super().update_opt_out(email, list_ids, value)
def update_opt_out_other(self, email, res_ids, value):
"""Method for changing unsubscription for models with opt_out field."""
model = self.env[self.mailing_model_real].with_context(
active_test=False)
action = "unsubscription" if value else "subscription"
if 'opt_out' in model._fields:
email_fname = 'email_from'
if 'email' in model._fields:
email_fname = 'email'
records = model.search([
('id', 'in', res_ids), (email_fname, 'ilike', email)])
records.write({'opt_out': value})
for res_id in res_ids:
self.env["mail.unsubscription"].create({
"email": email,
"mass_mailing_id": self.id,
"unsubscriber_id": "%s,%d" % (
self.mailing_model_real, res_id),
"action": action,
})
def _get_opt_out_list(self):
"""Handle models with opt_out field for excluding them."""
self.ensure_one()
model = self.env[self.mailing_model_real].with_context(
active_test=False)
if (self.mailing_model_real != "mail.mass_mailing.contact" and
'opt_out' in model._fields):
email_fname = 'email_from'
if 'email' in model._fields:
email_fname = 'email'
domain = safe_eval(self.mailing_domain)
domain = [('opt_out', '=', True)] + domain
recs = self.env[self.mailing_model_real].search(domain)
normalized_email = (
tools.email_split(c[email_fname]) for c in recs)
return set(e[0].lower() for e in normalized_email if e)
return super()._get_opt_out_list()

View File

@ -5,3 +5,12 @@ they are going to unsubscribe. To do it:
#. Create / edit / remove / sort as usual.
#. If *Details required* is enabled, they will have to fill a text area to
continue.
For having discrete unsubscriptions from other recipients than the mailing
lists, you need to add a glue module that adds 2 fields in the associated
model:
- `opt_out`.
- Either `email` or `email_from`.
See `mass_mailing_custom_unsubscribe_event` for an example.

View File

@ -5,3 +5,4 @@
* Jairo Llopis
* David Vidal
* Ernesto Tejeda
* Pedro M. Baeza

View File

@ -6,3 +6,6 @@ This addon extends the unsubscription form to let you:
mass mailing.
- Provide proof on why you are sending mass mailings to a given contact, as
required by the GDPR in Europe.
- Handle discrete unsubscriptions from other recipients that are not a mailing
list. On standard module, unsubscriptions from these recipients directly
include that mail on the general blacklist.

View File

@ -376,6 +376,9 @@ from a different one.</li>
mass mailing.</li>
<li>Provide proof on why you are sending mass mailings to a given contact, as
required by the GDPR in Europe.</li>
<li>Handle discrete unsubscriptions from other recipients that are not a mailing
list. On standard module, unsubscriptions from these recipients directly
include that mail on the general blacklist.</li>
</ul>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
@ -402,6 +405,14 @@ they are going to unsubscribe. To do it:</p>
<li>If <em>Details required</em> is enabled, they will have to fill a text area to
continue.</li>
</ol>
<p>For having discrete unsubscriptions from other recipients than the mailing
lists, you need to add a glue module that adds 2 fields in the associated
model:</p>
<ul class="simple">
<li><cite>opt_out</cite>.</li>
<li>Either <cite>email</cite> or <cite>email_from</cite>.</li>
</ul>
<p>See <cite>mass_mailing_custom_unsubscribe_event</cite> for an example.</p>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#id2">Usage</a></h1>
@ -449,6 +460,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
<li>Jairo Llopis</li>
<li>David Vidal</li>
<li>Ernesto Tejeda</li>
<li>Pedro M. Baeza</li>
</ul>
</li>
</ul>