From 220da198817a0281e33182c4e45957c74f3dee45 Mon Sep 17 00:00:00 2001 From: Emanuel Cino Date: Thu, 10 Jun 2021 12:19:05 +0200 Subject: [PATCH] Various Mailchimp fixes - Sync was missing when tags were updated - FIX for partners merging: merge also mailing contacts - Bug fixes in the code --- .../models/mailchimp_lists.py | 3 ++- .../models/mass_mailing_contact.py | 16 ++++++++++++++-- .../models/res_partner_category.py | 3 ++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/mass_mailing_switzerland/models/mailchimp_lists.py b/mass_mailing_switzerland/models/mailchimp_lists.py index 3f8087fab..c6fb0fe97 100644 --- a/mass_mailing_switzerland/models/mailchimp_lists.py +++ b/mass_mailing_switzerland/models/mailchimp_lists.py @@ -23,5 +23,6 @@ def process_member_from_stored_response(self, pending_record): @api.multi def get_mapped_merge_field(self): res = super().get_mapped_merge_field() - res.extend(["lastname", "firstname", "number_sponsorships", "opt_out"]) + res.extend([ + "lastname", "firstname", "number_sponsorships", "opt_out", "category_id"]) return res diff --git a/mass_mailing_switzerland/models/mass_mailing_contact.py b/mass_mailing_switzerland/models/mass_mailing_contact.py index a31cf685d..a737b764e 100644 --- a/mass_mailing_switzerland/models/mass_mailing_contact.py +++ b/mass_mailing_switzerland/models/mass_mailing_contact.py @@ -172,6 +172,17 @@ def create(self, vals_list): @api.multi def write(self, values): + if values.get("email"): + # Check for duplicates when changing the email. + changed = self.filtered(lambda c: c.email != values["email"]) + other_contacts = self.search([ + ("email", "=", values["email"]), + ("id", "not in", changed.ids) + ]) + if other_contacts: + values["partner_ids"] = [(4, c.id) for c in self.mapped("partner_ids")] + changed.delay_contact_unlink() + return super(MassMailingContact, other_contacts).write(values) out = super().write(values) # can't be simplified because we are looking for is_email_valid == False # AND is_email_valid is in values (return None otherwise) @@ -213,7 +224,8 @@ def process_mailchimp_update(self): ]) to_update = self if queue_job: - args = ",".join(queue_job.mapped("args")) + args = [ + item for sublist in queue_job.mapped("args") for item in sublist] for contact in self: if contact.id in args: to_update -= contact @@ -232,7 +244,7 @@ def action_export_to_mailchimp(self): @api.multi @job(default_channel="root.mass_mailing_switzerland.update_partner_mailchimp") def action_update_to_mailchimp(self): - """ + """Synchronize Contacts to Mailchimp. Always export in english so that all tags are set in English """ out = True diff --git a/mass_mailing_switzerland/models/res_partner_category.py b/mass_mailing_switzerland/models/res_partner_category.py index 86e1e56ce..e7cbe797a 100644 --- a/mass_mailing_switzerland/models/res_partner_category.py +++ b/mass_mailing_switzerland/models/res_partner_category.py @@ -27,5 +27,6 @@ def update_partner_tags(self): "tag_ids": [(4, tag.id)] }) to_update = old_partners ^ new_partners - to_update.mapped("mass_mailing_contact_ids").process_mailchimp_update() + self.env["res.partner"].browse(to_update).mapped( + "mass_mailing_contact_ids").process_mailchimp_update() return True