[FIX] mass_mailing_partner: Fix partner merge error (dupplicate mass_mailing contacts)

This commit is contained in:
Víctor Martínez 2020-11-09 08:37:23 +01:00 committed by emagdalenaC2i
parent 7b8c29f73f
commit 2a8d15882a
4 changed files with 59 additions and 0 deletions

View File

@ -9,5 +9,6 @@
* Ernesto Tejeda
* Victor M.M. Torres
* Manuel Calero
* Víctor Martínez
* `Hibou Corp. <https://hibou.io>`_

View File

@ -108,3 +108,35 @@ class MailMassMailingContactCase(base.BaseCase):
contact.partner_id = partner
contact._onchange_partner_mass_mailing_partner()
self.check_mailing_contact_partner(contact)
def test_partners_merge(self):
partner_1 = self.create_partner({"name": "Demo 1", "email": "demo1@demo.com"})
partner_2 = self.create_partner({"name": "Demo 2", "email": "demo2@demo.com"})
list_1 = self.create_mailing_list({"name": "List test 1"})
list_2 = self.create_mailing_list({"name": "List test 2"})
contact_1 = self.create_mailing_contact(
{
"email": partner_1.email,
"name": partner_1.name,
"partner_id": partner_1.id,
"list_ids": [(6, 0, [list_1.id])],
}
)
contact_2 = self.create_mailing_contact(
{
"email": partner_2.email,
"name": partner_2.name,
"partner_id": partner_2.id,
"list_ids": [(6, 0, [list_1.id, list_2.id])],
}
)
# Wizard partner merge (partner_1 + partner_2) in partner_i1
wizard = self.env["base.partner.merge.automatic.wizard"].create(
{"state": "option"}
)
wizard._merge((partner_1 + partner_2).ids, partner_1)
contact = self.env["mailing.contact"].search(
[("id", "in", (contact_1 + contact_2).ids)]
)
self.assertEqual(len(contact), 1)
self.assertEqual(contact.list_ids.ids, (list_1 + list_2).ids)

View File

@ -1,3 +1,4 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import partner_mail_list_wizard
from . import partner_merge

View File

@ -0,0 +1,25 @@
# Copyright 2020 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models
class BasePartnerMergeAutomaticWizard(models.TransientModel):
_inherit = "base.partner.merge.automatic.wizard"
def _merge(self, partner_ids, dst_partner=None, extra_checks=True):
if dst_partner:
contacts = self.env["mailing.contact"].search(
[("partner_id", "in", partner_ids)]
)
if contacts:
contacts = contacts.sorted(
lambda x: 1 if x.partner_id == dst_partner else 0
)
list_ids = contacts.mapped("list_ids").ids
contacts[1:].unlink()
contacts[0].partner_id = dst_partner
contacts[0].list_ids = [(4, x) for x in list_ids]
return super()._merge(
partner_ids, dst_partner=dst_partner, extra_checks=extra_checks
)