Browse Source

Make sync method action more user friendly

The SQL constraint gives the user no idea how
to rectify the problem.

Instead, sync all the Contacts that can be, and
collect the ones that can't into a list to
display to the user in a ValidationError.
pull/305/head
Dave Burkholder 6 years ago
parent
commit
c32fcb00e0
  1. 20
      mass_mailing_list_dynamic/models/mail_mass_mailing_list.py

20
mass_mailing_list_dynamic/models/mail_mass_mailing_list.py

@ -1,9 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2017 Tecnativa - Jairo Llopis # Copyright 2017 Tecnativa - Jairo Llopis
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from psycopg2 import IntegrityError
from odoo import api, fields, models from odoo import api, fields, models
from odoo.tools import safe_eval
from odoo.exceptions import ValidationError
from ast import literal_eval
class MassMailingList(models.Model): class MassMailingList(models.Model):
@ -42,9 +44,10 @@ class MassMailingList(models.Model):
Partner = self.env["res.partner"] Partner = self.env["res.partner"]
# Skip non-dynamic lists # Skip non-dynamic lists
dynamic = self.filtered("dynamic") dynamic = self.filtered("dynamic")
errors = []
for one in dynamic: for one in dynamic:
sync_domain = safe_eval(one.sync_domain) + [("email", "!=", False)]
desired_partners = Partner.search(sync_domain)
domain = literal_eval(one.sync_domain) + [("email", "!=", False)]
desired_partners = Partner.search(domain)
# Remove undesired contacts when synchronization is full # Remove undesired contacts when synchronization is full
if one.sync_method == "full": if one.sync_method == "full":
Contact.search([ Contact.search([
@ -55,11 +58,22 @@ class MassMailingList(models.Model):
current_partners = current_contacts.mapped("partner_id") current_partners = current_contacts.mapped("partner_id")
# Add new contacts # Add new contacts
for partner in desired_partners - current_partners: for partner in desired_partners - current_partners:
try:
with self.env.cr.savepoint():
Contact.create({ Contact.create({
"list_id": one.id, "list_id": one.id,
"partner_id": partner.id, "partner_id": partner.id,
}) })
except IntegrityError:
errors.append(
'Cannot add Partner %s to "%s". '
'Email %s already added.' %
(partner.name, one.name, partner.email)
)
if errors:
raise ValidationError('\n'.join(errors))
one.is_synced = True one.is_synced = True
# Invalidate cached contact count # Invalidate cached contact count
self.invalidate_cache(["contact_nbr"], dynamic.ids) self.invalidate_cache(["contact_nbr"], dynamic.ids)

Loading…
Cancel
Save