Browse Source

[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.
12.0
Akim Juillerat 5 years ago
parent
commit
3368aa9bac
  1. 2
      partner_ref_unique/__manifest__.py
  2. 12
      partner_ref_unique/models/res_partner.py
  3. 7
      partner_ref_unique/tests/test_res_partner_ref.py

2
partner_ref_unique/__manifest__.py

@ -5,7 +5,7 @@
{ {
"name": "Partner unique reference", "name": "Partner unique reference",
"summary": "Add an unique constraint to partner ref field", "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", "category": "Customer Relationship Management",
"website": "https://github.com/OCA/partner-contact/", "website": "https://github.com/OCA/partner-contact/",
"author": "Tecnativa, " "author": "Tecnativa, "

12
partner_ref_unique/models/res_partner.py

@ -1,18 +1,24 @@
# Copyright 2016 Antonio Espinosa # Copyright 2016 Antonio Espinosa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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 from odoo.exceptions import ValidationError
class ResPartner(models.Model): class ResPartner(models.Model):
_inherit = "res.partner" _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.multi
@api.constrains('ref', 'is_company', 'company_id')
@api.constrains('ref', 'is_company', 'company_id', 'partner_ref_unique')
def _check_ref(self): def _check_ref(self):
for partner in self: for partner in self:
mode = partner.company_id.partner_ref_unique
mode = partner.partner_ref_unique
if (partner.ref and ( if (partner.ref and (
mode == 'all' or mode == 'all' or
(mode == 'companies' and partner.is_company))): (mode == 'companies' and partner.is_company))):

7
partner_ref_unique/tests/test_res_partner_ref.py

@ -18,7 +18,7 @@ class TestResPartnerRefUnique(common.SavepointCase):
'name': 'Partner2', 'name': 'Partner2',
}) })
def test_check_ref(self):
def test_check_ref_company(self):
# Test can create/modify partners with same ref # Test can create/modify partners with same ref
self.company.partner_ref_unique = 'none' self.company.partner_ref_unique = 'none'
self.partner1.ref = 'same_ref' self.partner1.ref = 'same_ref'
@ -30,6 +30,11 @@ class TestResPartnerRefUnique(common.SavepointCase):
'ref': 'same_ref', 'ref': 'same_ref',
}) })
self.partner2.ref = False 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 # Test can't create/modify partner with same ref
self.company.partner_ref_unique = 'all' self.company.partner_ref_unique = 'all'
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):

Loading…
Cancel
Save