
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
        )


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"]
                .sudo()
                .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
        )

Add sudo() to prevent user without mailing access try to merge contacts
[FIX] mass_mailing_partner: Add sudo() to prevent user without mailing access try to merge contacts

  
  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 = (
-             contacts = self.env["mailing.contact"].search(
+                 self.env["mailing.contact"]
+                 .sudo()
-                 [("partner_id", "in", partner_ids)]
+                 .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
          )
  
Add sudo() to prevent user without mailing access try to merge contacts
## Code Before:

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
        )

## Instruction:
Add sudo() to prevent user without mailing access try to merge contacts
## Code After:

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"]
                .sudo()
                .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
        )
