From 3368aa9bac821e27dcb95ff96cfb3485df904380 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Mon, 6 Jan 2020 14:56:34 +0100 Subject: [PATCH] [FIX] partner_ref_unique: Trigger check on company update Before this commit, the partner_ref_unique could be changed on the company even if existing partners share the same internal ref. Therefore, errors will appear afterwards as DB already contains duplicates although the policy shouldn't allow it. With this change, the error will appear as soon as the policy is changed, so that the data must be fixed before activating the constraint. --- partner_ref_unique/__manifest__.py | 2 +- partner_ref_unique/models/res_partner.py | 12 +++++++++--- partner_ref_unique/tests/test_res_partner_ref.py | 7 ++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/partner_ref_unique/__manifest__.py b/partner_ref_unique/__manifest__.py index 87f9ae2fa..672969662 100644 --- a/partner_ref_unique/__manifest__.py +++ b/partner_ref_unique/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Partner unique reference", "summary": "Add an unique constraint to partner ref field", - "version": "12.0.1.0.0", + "version": "12.0.1.0.1", "category": "Customer Relationship Management", "website": "https://github.com/OCA/partner-contact/", "author": "Tecnativa, " diff --git a/partner_ref_unique/models/res_partner.py b/partner_ref_unique/models/res_partner.py index c7fc7af23..0b45a7ca0 100644 --- a/partner_ref_unique/models/res_partner.py +++ b/partner_ref_unique/models/res_partner.py @@ -1,18 +1,24 @@ # Copyright 2016 Antonio Espinosa # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import _, api, models +from odoo import _, api, fields, models from odoo.exceptions import ValidationError class ResPartner(models.Model): _inherit = "res.partner" + # This related is needed in order to trigger the check when changing the + # value on res.company + partner_ref_unique = fields.Selection( + related='company_id.partner_ref_unique', + ) + @api.multi - @api.constrains('ref', 'is_company', 'company_id') + @api.constrains('ref', 'is_company', 'company_id', 'partner_ref_unique') def _check_ref(self): for partner in self: - mode = partner.company_id.partner_ref_unique + mode = partner.partner_ref_unique if (partner.ref and ( mode == 'all' or (mode == 'companies' and partner.is_company))): diff --git a/partner_ref_unique/tests/test_res_partner_ref.py b/partner_ref_unique/tests/test_res_partner_ref.py index bd97bda3b..39509fa42 100644 --- a/partner_ref_unique/tests/test_res_partner_ref.py +++ b/partner_ref_unique/tests/test_res_partner_ref.py @@ -18,7 +18,7 @@ class TestResPartnerRefUnique(common.SavepointCase): 'name': 'Partner2', }) - def test_check_ref(self): + def test_check_ref_company(self): # Test can create/modify partners with same ref self.company.partner_ref_unique = 'none' self.partner1.ref = 'same_ref' @@ -30,6 +30,11 @@ class TestResPartnerRefUnique(common.SavepointCase): 'ref': 'same_ref', }) self.partner2.ref = False + with self.assertRaises(ValidationError): + self.company.partner_ref_unique = 'all' + + def test_check_ref(self): + self.partner1.ref = 'same_ref' # Test can't create/modify partner with same ref self.company.partner_ref_unique = 'all' with self.assertRaises(ValidationError):