[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate.
- Imported last updates from v8.
- Adapted to v9.
- Added a saner default to `mass_mailing.salt` configuration parameter by
reusing `database.secret` if available, hoping that some day
https://github.com/odoo/odoo/pull/12040 gets merged.
- Updated README.
- Increase security, drop backwards compatibility.
Security got improved upstream, which would again break compatibility among current addon and future master upstream.
I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features.
- Includes tour tests.
- Removes outdated tests.
- Extends the mailing list management form when unsubscriber is a contact.
- Adds a reason form even if he is not.
- Avoids all methods that were not model-agnostic.
[FIX][mass_mailing_custom_unsubscribe] Reasons noupdate
After this fix, when you update the addon, you will not lose your customized reasons.
[FIX] Compatibilize with mass_mailing_partner
Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets.
Now records are created from an empty recordset, and thus tests work everywhere.
Update instructions
If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon.
[FIX] Use the right operator to preserve recordsets order
Using `|=` sorts records at will each time (treating them as Python's `set`).
Using `+=` always appends a record to the end of the set.
Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
2016-11-11 14:41:20 +01:00
|
|
|
# Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
|
2020-06-19 12:47:06 +02:00
|
|
|
# Copyright 2020 Tecnativa - Pedro M. Baeza
|
2016-07-25 12:50:32 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
|
2020-08-24 10:36:19 +02:00
|
|
|
from itertools import groupby
|
|
|
|
|
2020-06-19 12:47:06 +02:00
|
|
|
from odoo import models, tools
|
|
|
|
from odoo.tools.safe_eval import safe_eval
|
2016-07-25 12:50:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MailMassMailing(models.Model):
|
|
|
|
_inherit = "mail.mass_mailing"
|
|
|
|
|
2019-04-06 01:39:55 +02:00
|
|
|
def update_opt_out(self, email, list_ids, value):
|
[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate.
- Imported last updates from v8.
- Adapted to v9.
- Added a saner default to `mass_mailing.salt` configuration parameter by
reusing `database.secret` if available, hoping that some day
https://github.com/odoo/odoo/pull/12040 gets merged.
- Updated README.
- Increase security, drop backwards compatibility.
Security got improved upstream, which would again break compatibility among current addon and future master upstream.
I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features.
- Includes tour tests.
- Removes outdated tests.
- Extends the mailing list management form when unsubscriber is a contact.
- Adds a reason form even if he is not.
- Avoids all methods that were not model-agnostic.
[FIX][mass_mailing_custom_unsubscribe] Reasons noupdate
After this fix, when you update the addon, you will not lose your customized reasons.
[FIX] Compatibilize with mass_mailing_partner
Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets.
Now records are created from an empty recordset, and thus tests work everywhere.
Update instructions
If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon.
[FIX] Use the right operator to preserve recordsets order
Using `|=` sorts records at will each time (treating them as Python's `set`).
Using `+=` always appends a record to the end of the set.
Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
2016-11-11 14:41:20 +01:00
|
|
|
"""Save unsubscription reason when opting out from mailing."""
|
2017-07-05 11:07:28 +02:00
|
|
|
self.ensure_one()
|
2018-05-10 12:17:04 +02:00
|
|
|
action = "unsubscription" if value else "subscription"
|
2020-08-24 10:36:19 +02:00
|
|
|
subscription_model = self.env["mail.mass_mailing.list_contact_rel"]
|
|
|
|
opt_out_records = subscription_model.search(
|
|
|
|
[
|
|
|
|
("contact_id.email", "=ilike", email),
|
|
|
|
("list_id", "in", list_ids),
|
|
|
|
("opt_out", "!=", value),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
model_name = "mail.mass_mailing.contact"
|
|
|
|
for contact, subscriptions in groupby(opt_out_records, lambda r: r.contact_id):
|
2019-04-06 01:39:55 +02:00
|
|
|
mailing_list_ids = [r.list_id.id for r in subscriptions]
|
|
|
|
# reason_id and details are expected from the context
|
2020-08-24 10:36:19 +02:00
|
|
|
self.env["mail.unsubscription"].create(
|
|
|
|
{
|
|
|
|
"email": email,
|
|
|
|
"mass_mailing_id": self.id,
|
|
|
|
"unsubscriber_id": "%s,%d" % (model_name, contact.id),
|
|
|
|
"mailing_list_ids": [(6, False, mailing_list_ids)],
|
|
|
|
"action": action,
|
|
|
|
}
|
|
|
|
)
|
2019-04-06 01:39:55 +02:00
|
|
|
return super().update_opt_out(email, list_ids, value)
|
2020-06-19 12:47:06 +02:00
|
|
|
|
|
|
|
def update_opt_out_other(self, email, res_ids, value):
|
|
|
|
"""Method for changing unsubscription for models with opt_out field."""
|
2020-08-24 10:36:19 +02:00
|
|
|
model = self.env[self.mailing_model_real].with_context(active_test=False)
|
2020-06-19 12:47:06 +02:00
|
|
|
action = "unsubscription" if value else "subscription"
|
2020-08-24 10:36:19 +02:00
|
|
|
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})
|
2020-06-19 12:47:06 +02:00
|
|
|
for res_id in res_ids:
|
2020-08-24 10:36:19 +02:00
|
|
|
self.env["mail.unsubscription"].create(
|
|
|
|
{
|
|
|
|
"email": email,
|
|
|
|
"mass_mailing_id": self.id,
|
|
|
|
"unsubscriber_id": "%s,%d" % (self.mailing_model_real, res_id),
|
|
|
|
"action": action,
|
|
|
|
}
|
|
|
|
)
|
2020-06-19 12:47:06 +02:00
|
|
|
|
|
|
|
def _get_opt_out_list(self):
|
|
|
|
"""Handle models with opt_out field for excluding them."""
|
|
|
|
self.ensure_one()
|
2020-08-24 10:36:19 +02:00
|
|
|
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"
|
2020-06-19 12:47:06 +02:00
|
|
|
domain = safe_eval(self.mailing_domain)
|
2020-08-24 10:36:19 +02:00
|
|
|
domain = [("opt_out", "=", True)] + domain
|
2020-06-19 12:47:06 +02:00
|
|
|
recs = self.env[self.mailing_model_real].search(domain)
|
2020-08-24 10:36:19 +02:00
|
|
|
normalized_email = (tools.email_split(c[email_fname]) for c in recs)
|
|
|
|
return {e[0].lower() for e in normalized_email if e}
|
2020-06-19 12:47:06 +02:00
|
|
|
return super()._get_opt_out_list()
|