From 52df97e4f38e13216f182c30e858d4eb6fd822d3 Mon Sep 17 00:00:00 2001 From: nounoubensebia Date: Fri, 29 Jan 2021 09:44:24 +0000 Subject: [PATCH] [IMP] mass_mailing: improve list duplication and protect "opt-out-ness" of contacts Before this commit, on duplication, the contacts who opted-out from the original list have the opt-out field set to False on the duplicated list. After this commit, on duplication, for a contact who has opted-out from the original list,the opt-out field will be set to True on the duplicated list, Because when a recipient opt-out from a mailing list, chances are high that he does not want to be included in the new duplicated list. This commit also brings some fixes to the update and create methods on the mailing_contact_subscription model so that - The unsubscription_date is set to now only when no unsubscription_date is given, because when another value for unsubscription_date is given by the user it should be applied - The opt_out field is set to True when the user sets an unsubscription_date, because when a date is set for unsubscription_date, it means that the user has opted-out from the list We also update name of mailing list when duplicating it, adding a copy mark to it. Finally we also avoid copying mailing_ids through m2m relationship as it may impact existing mailings. Task ID-2431383 closes odoo/odoo#65277 Signed-off-by: Thibault Delavallee (tde) --- addons/mass_mailing/models/mailing_contact.py | 8 ++++++-- addons/mass_mailing/models/mailing_list.py | 13 ++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/addons/mass_mailing/models/mailing_contact.py b/addons/mass_mailing/models/mailing_contact.py index c1b2963ab7c..96976af0763 100644 --- a/addons/mass_mailing/models/mailing_contact.py +++ b/addons/mass_mailing/models/mailing_contact.py @@ -31,13 +31,17 @@ class MassMailingContactListRel(models.Model): def create(self, vals_list): now = fields.Datetime.now() for vals in vals_list: - if 'opt_out' in vals: + if 'opt_out' in vals and 'unsubscription_date' not in vals: vals['unsubscription_date'] = now if vals['opt_out'] else False + if vals.get('unsubscription_date'): + vals['opt_out'] = True return super().create(vals_list) def write(self, vals): - if 'opt_out' in vals: + if 'opt_out' in vals and 'unsubscription_date' not in vals: vals['unsubscription_date'] = fields.Datetime.now() if vals['opt_out'] else False + if vals.get('unsubscription_date'): + vals['opt_out'] = True return super(MassMailingContactListRel, self).write(vals) diff --git a/addons/mass_mailing/models/mailing_list.py b/addons/mass_mailing/models/mailing_list.py index 2c895df9c96..ef842f651f8 100644 --- a/addons/mass_mailing/models/mailing_list.py +++ b/addons/mass_mailing/models/mailing_list.py @@ -22,11 +22,11 @@ class MassMailingList(models.Model): contact_pct_bounce = fields.Float(compute="_compute_mailing_list_statistics", string="Percentage of Bouncing") contact_ids = fields.Many2many( 'mailing.contact', 'mailing_contact_list_rel', 'list_id', 'contact_id', - string='Mailing Lists') + string='Mailing Lists', copy=False) mailing_count = fields.Integer(compute="_compute_mailing_list_count", string="Number of Mailing") - mailing_ids = fields.Many2many('mailing.mailing', 'mail_mass_mailing_list_rel', string='Mass Mailings') + mailing_ids = fields.Many2many('mailing.mailing', 'mail_mass_mailing_list_rel', string='Mass Mailings', copy=False) subscription_ids = fields.One2many('mailing.contact.subscription', 'list_id', - string='Subscription Information') + string='Subscription Information', copy=True) is_public = fields.Boolean(default=True, help="The mailing list can be accessible by recipient in the unsubscription" " page to allows him to update his subscription preferences.") @@ -109,6 +109,13 @@ class MassMailingList(models.Model): def name_get(self): return [(list.id, "%s (%s)" % (list.name, list.contact_count)) for list in self] + def copy(self, default=None): + self.ensure_one() + + default = dict(default or {}, + name=_('%s (copy)', self.name),) + return super(MassMailingList, self).copy(default) + # ------------------------------------------------------ # ACTIONS # ------------------------------------------------------