From d63e72853b299483e845115c868efc3bd16a1ddf Mon Sep 17 00:00:00 2001 From: David Beguin Date: Wed, 10 Oct 2018 13:30:58 +0000 Subject: [PATCH] [FIX] mass_mailing : restore import mass_mailing.contact into mass_mailing.list Since the opt out per mailing list, the intermediate model was used to list all the recipient of a mailing list. The side effect was that importing new contacts into mailing list was not possible anymore. Also, creation of a new contact into a mailing list was made indirectly by creating a new many2many record between contaxct and list, which was not really user friendly. This commit restores the mailing contact model as the view of the mailing list recipients. In other terms, the contact import is restored. An opt-out computed field as been added to be able to filter on opted out contact inside the mailing list recipients view. This opt_out field SHOULD NOT be used in a form view as it need the active mailing list id in the context in order to be computable and searchable. This field has no sense without this active mailing list id context. Task ID 1891228 Linked with blacklist and opt out per mailing task (ID 33224) Closes PR #27632 --- addons/mass_mailing/models/mass_mailing.py | 31 +++++++++++++++++++ .../mass_mailing/views/mass_mailing_views.xml | 18 ++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/addons/mass_mailing/models/mass_mailing.py b/addons/mass_mailing/models/mass_mailing.py index bd4ef81f143..0e146197260 100644 --- a/addons/mass_mailing/models/mass_mailing.py +++ b/addons/mass_mailing/models/mass_mailing.py @@ -76,6 +76,7 @@ class MassMailingContactListRel(models.Model): return super(MassMailingContactListRel, self).write(vals) def action_open_mailing_list_contact(self): + """TODO DBE : To remove - Deprecated""" contact_id = self.contact_id action = { 'name': _(contact_id.name), @@ -220,12 +221,42 @@ class MassMailingContact(models.Model): message_bounce = fields.Integer(string='Bounced', help='Counter of the number of bounced emails for this contact.', default=0) country_id = fields.Many2one('res.country', string='Country') tag_ids = fields.Many2many('res.partner.category', string='Tags') + opt_out = fields.Boolean('Opt Out', compute='_compute_opt_out', search='_search_opt_out', + help='Opt out flag for a specific mailing list.' + 'This field should not be used in a view without a unique and active mailing list context.') @api.depends('email') def _compute_is_email_valid(self): for record in self: record.is_email_valid = re.match(EMAIL_PATTERN, record.email) + @api.model + def _search_opt_out(self, operator, value): + # Assumes operator is '=' or '!=' and value is True or False + if operator != '=': + if operator == '!=' and isinstance(value, bool): + value = not value + else: + raise NotImplementedError() + + if 'default_list_ids' in self._context and isinstance(self._context['default_list_ids'], (list, tuple)) and len(self._context['default_list_ids']) == 1: + [active_list_id] = self._context['default_list_ids'] + contacts = self.env['mail.mass_mailing.list_contact_rel'].search([('list_id', '=', active_list_id)]) + return [('id', 'in', [record.contact_id.id for record in contacts if record.opt_out == value])] + else: + raise UserError('Search opt out cannot be executed without a unique and valid active mailing list context.') + + @api.depends('subscription_list_ids') + def _compute_opt_out(self): + if 'default_list_ids' in self._context and isinstance(self._context['default_list_ids'], (list, tuple)) and len(self._context['default_list_ids']) == 1: + [active_list_id] = self._context['default_list_ids'] + for record in self: + active_subscription_list = record.subscription_list_ids.filtered(lambda l: l.list_id.id == active_list_id) + record.opt_out = active_subscription_list.opt_out + else: + for record in self: + record.opt_out = False + def get_name_email(self, name): name, email = self.env['res.partner']._parse_partner_name(name) if name and not email: diff --git a/addons/mass_mailing/views/mass_mailing_views.xml b/addons/mass_mailing/views/mass_mailing_views.xml index 84bf335ca59..ab86e32d87d 100644 --- a/addons/mass_mailing/views/mass_mailing_views.xml +++ b/addons/mass_mailing/views/mass_mailing_views.xml @@ -11,6 +11,7 @@ + mail.mass_mailing.list_contact_rel.view.form mail.mass_mailing.list_contact_rel 10 @@ -36,6 +37,7 @@ + mail.mass_mailing.list_contact_rel.list_contact.view.tree mail.mass_mailing.list_contact_rel @@ -51,6 +53,7 @@ + mail.mass_mailing.list_contact_rel.view.search mail.mass_mailing.list_contact_rel @@ -85,8 +88,14 @@ string="Name / Email"/> + + + + @@ -109,6 +118,7 @@ + @@ -238,7 +248,7 @@ mail.mass_mailing.contact form tree,kanban,form,graph,pivot - {'search_default_not_blacklisted': 1} + {'search_default_valid_recipient': 1}

Create a contact in your address book @@ -250,11 +260,11 @@ Recipients - mail.mass_mailing.list_contact_rel + mail.mass_mailing.contact form tree,form,graph - [('list_id', 'in', [active_id])] - {'search_default_valid_recipients': 1, 'default_list_id': active_id} + [('list_ids', 'in', [active_id])] + {'search_default_valid_recipients': 1, 'default_list_ids': [active_id]}

Create a new recipient