|
|
@ -1,4 +1,3 @@ |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
# Copyright 2017 Tecnativa - Jairo Llopis |
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|
|
|
|
|
|
@ -47,18 +46,36 @@ class MassMailingList(models.Model): |
|
|
|
desired_partners = Partner.search(sync_domain) |
|
|
|
# Remove undesired contacts when synchronization is full |
|
|
|
if one.sync_method == "full": |
|
|
|
Contact.search([ |
|
|
|
("list_id", "=", one.id), |
|
|
|
("partner_id", "not in", desired_partners.ids), |
|
|
|
]).unlink() |
|
|
|
current_contacts = Contact.search([("list_id", "=", one.id)]) |
|
|
|
current_partners = current_contacts.mapped("partner_id") |
|
|
|
contacts = Contact.search([("list_ids", "in", [one.id]), |
|
|
|
("partner_id", "not in", |
|
|
|
desired_partners.ids)]) |
|
|
|
for contact in contacts: |
|
|
|
# No delete contacts with other lists |
|
|
|
if len(contact.list_ids) > 1: |
|
|
|
contact.write({'list_ids': [(3, one.id, False)]}) |
|
|
|
else: |
|
|
|
contact.unlink() |
|
|
|
current_contacts_in_list = \ |
|
|
|
Contact.search([("list_ids", "in", [one.id])]) |
|
|
|
current_partners_in_list = \ |
|
|
|
current_contacts_in_list.mapped("partner_id") |
|
|
|
current_contacts_not_in_list = \ |
|
|
|
Contact.search([("partner_id", "in", desired_partners.ids), |
|
|
|
("list_ids", "not in", [one.id])]) |
|
|
|
current_partners_not_in_list = \ |
|
|
|
current_contacts_not_in_list.mapped("partner_id") |
|
|
|
# Add new contacts |
|
|
|
for partner in desired_partners - current_partners: |
|
|
|
for partner in desired_partners - current_partners_in_list - \ |
|
|
|
current_partners_not_in_list: |
|
|
|
Contact.create({ |
|
|
|
"list_id": one.id, |
|
|
|
"list_ids": [(4, one.id, False)], |
|
|
|
"partner_id": partner.id, |
|
|
|
}) |
|
|
|
# Add list in existing contacts |
|
|
|
for contact in current_contacts_not_in_list: |
|
|
|
contact.write({ |
|
|
|
"list_ids": [(4, one.id, False)], |
|
|
|
}) |
|
|
|
one.is_synced = True |
|
|
|
# Invalidate cached contact count |
|
|
|
self.invalidate_cache(["contact_nbr"], dynamic.ids) |
|
|
|