Skip to content

Commit 682249d

Browse files
[#267] Created PartijIdentificatorAdminForm
1 parent 21ed08b commit 682249d

File tree

5 files changed

+287
-46
lines changed

5 files changed

+287
-46
lines changed

src/openklant/components/klantinteracties/admin/partijen.py

+38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
from django import forms
12
from django.contrib import admin
3+
from django.core.exceptions import ValidationError
24
from django.utils.translation import gettext_lazy as _
35

46
from openklant.components.klantinteracties.models.rekeningnummers import Rekeningnummer
7+
from openklant.components.klantinteracties.models.validators import (
8+
PartijIdentificatorValidator,
9+
SubIdentificatorValidator,
10+
)
511

612
from ..models.constants import SoortPartij
713
from ..models.digitaal_adres import DigitaalAdres
@@ -18,6 +24,37 @@
1824
)
1925

2026

27+
class PartijIdentificatorAdminForm(forms.ModelForm):
28+
class Meta:
29+
model = PartijIdentificator
30+
fields = "__all__"
31+
32+
def clean(self):
33+
cleaned_data = super().clean()
34+
sub_identificator_van = cleaned_data["sub_identificator_van"]
35+
partij_identificator = {
36+
"code_objecttype": cleaned_data["partij_identificator_code_objecttype"],
37+
"code_soort_object_id": cleaned_data[
38+
"partij_identificator_code_soort_object_id"
39+
],
40+
"object_id": cleaned_data["partij_identificator_object_id"],
41+
"code_register": cleaned_data["partij_identificator_code_register"],
42+
}
43+
44+
if sub_identificator_van == self.instance:
45+
# TODO MESSAGE
46+
raise ValidationError({"sub_identificator_van": "your_error_message 1"})
47+
48+
PartijIdentificatorValidator()(partij_identificator)
49+
SubIdentificatorValidator()(
50+
{
51+
"sub_identificator_van": sub_identificator_van,
52+
"partij_identificator": partij_identificator,
53+
}
54+
)
55+
return cleaned_data
56+
57+
2158
class CategorieRelatieInlineAdmin(admin.StackedInline):
2259
model = CategorieRelatie
2360
readonly_fields = ("uuid",)
@@ -34,6 +71,7 @@ class CategorieRelatieInlineAdmin(admin.StackedInline):
3471
class PartijIdentificatorInlineAdmin(admin.StackedInline):
3572
readonly_fields = ("uuid",)
3673
model = PartijIdentificator
74+
form = PartijIdentificatorAdminForm
3775
extra = 0
3876

3977

src/openklant/components/klantinteracties/api/serializers/partijen.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from openklant.components.klantinteracties.models.rekeningnummers import Rekeningnummer
4545
from openklant.components.klantinteracties.models.validators import (
4646
PartijIdentificatorValidator,
47-
UniquePartijIdentificatorValidator,
47+
SubIdentificatorValidator,
4848
)
4949

5050

@@ -410,15 +410,15 @@ class Meta:
410410
"help_text": "De unieke URL van deze partij indentificator binnen deze API.",
411411
},
412412
}
413-
validators = [UniquePartijIdentificatorValidator()]
413+
validators = [SubIdentificatorValidator()]
414414

415415
def validate(self, attrs):
416416
if sub_identificator_van := attrs.get("sub_identificator_van", None):
417417
attrs["sub_identificator_van"] = PartijIdentificator.objects.filter(
418418
uuid=sub_identificator_van["uuid"]
419419
)
420420

421-
UniquePartijIdentificatorValidator()(attrs)
421+
SubIdentificatorValidator()(attrs)
422422
return super().validate(attrs)
423423

424424
@transaction.atomic

src/openklant/components/klantinteracties/models/partijen.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
SoortPartij,
1818
)
1919
from .mixins import BezoekadresMixin, ContactnaamMixin, CorrespondentieadresMixin
20-
from .validators import PartijIdentificatorValidator, UniquePartijIdentificatorValidator
2120

2221

2322
class Partij(APIMixin, BezoekadresMixin, CorrespondentieadresMixin):
@@ -416,10 +415,7 @@ class Meta:
416415
verbose_name = _("partij identificator")
417416
verbose_name_plural = _("partij identificatoren")
418417
constraints = [
419-
models.CheckConstraint(
420-
check=~models.Q(sub_identificator_van=models.F("id")),
421-
name="check_sub_identificator_van_not_self",
422-
),
418+
# TODO: cahnge msg
423419
models.UniqueConstraint(
424420
fields=[
425421
"sub_identificator_van",
@@ -431,6 +427,7 @@ class Meta:
431427
condition=models.Q(sub_identificator_van__isnull=False),
432428
name="scoped_identificator_globally_unique",
433429
),
430+
# TODO: cahnge msg
434431
models.UniqueConstraint(
435432
fields=[
436433
"partij_identificator_code_objecttype",
@@ -441,20 +438,15 @@ class Meta:
441438
name="non_scoped_identificator_globally_unique",
442439
condition=models.Q(sub_identificator_van__isnull=True),
443440
),
441+
# TODO: cahnge msg
442+
models.CheckConstraint(
443+
check=~models.Q(sub_identificator_van=models.F("id")),
444+
name="check_sub_identificator_van_not_self",
445+
),
444446
]
445447

446448
def __str__(self):
447449
soort_object = self.partij_identificator_code_soort_object_id
448450
object = self.partij_identificator_object_id
449451

450452
return f"{soort_object} - {object}"
451-
452-
def clean(self):
453-
super().clean()
454-
PartijIdentificatorValidator()(self.partij_identificator)
455-
UniquePartijIdentificatorValidator()(
456-
{
457-
"sub_identificator_van": self.sub_identificator_van,
458-
"partij_identificator": self.partij_identificator,
459-
}
460-
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
from django.core.exceptions import ValidationError
2+
from django.test import TestCase
3+
4+
from openklant.components.klantinteracties.models.constants import (
5+
PartijIdentificatorCodeObjectType,
6+
PartijIdentificatorCodeRegister,
7+
PartijIdentificatorCodeSoortObjectId,
8+
)
9+
from openklant.components.klantinteracties.models.partijen import (
10+
Partij,
11+
PartijIdentificator,
12+
)
13+
from openklant.components.klantinteracties.models.tests.factories.partijen import (
14+
PartijFactory,
15+
)
16+
17+
"""
18+
# ALLOWED only one time, because it does not exist in any partij
19+
- partij_identificator(partij=partij, soortObjectId='bsn', object_id=123)
20+
21+
# NOT ALLOWED because already exists one bsn for the same partij
22+
- partij_identificator(partij=partij, soortObjectId='bsn', object_id=456)
23+
24+
# NOT ALLOWED because already exists one bsn=123 for another partij
25+
- partij_identificator(partij=partij1, soortObjectId='bsn', object_id=123)
26+
27+
# ALLOWED only one time because vestigingsnummer=None
28+
- partij_identificator(partij=partij1, soortObjectId='kvk', object_id=123)
29+
30+
# NOT ALLOWED because already exist one kvk_nummer=123 without vestigingsnummer in any partij
31+
- partij_identificator(partij=partij2, soortObjectId='kvk', object_id=123)
32+
33+
# ALLOWED only one time, SAME kvk (123) NEW vestigingsnummer(123)
34+
- partij_identificator(partij=partij3, soortObjectId='kvk', object_id=123)
35+
- partij_identificator(partij=partij3, soortObjectId='vestigingsnummer', object_id=123)
36+
37+
# NOT ALLOWED beacuse arleady exists this combination
38+
- partij_identificator(partij=partij4, soortObjectId='kvk', object_id=123)
39+
- partij_identificator(partij=partij4, soortObjectId='vestigingsnummer', object_id=123)
40+
41+
# ALLOWED only one time, SAME kvk (123) NEW vestigingsnummer(456)
42+
- partij_identificator(partij=partij5, soortObjectId='kvk', object_id=123)
43+
- partij_identificator(partij=partij5, soortObjectId='vestigingsnummer', object_id=456)
44+
45+
# ALLOWED only one time, NEW kvk (456) SAME vestigingsnummer(456)
46+
- partij_identificator(partij=partij6, soortObjectId='kvk', object_id=456)
47+
- partij_identificator(partij=partij6, soortObjectId='vestigingsnummer', object_id=456)
48+
49+
Specificando Partij
50+
test_bsn
51+
test_1_bsn_in_partij
52+
test_n_bsn_in_partij
53+
test_1_bsn_in_multiple_partij
54+
test_n_bsn_in_multiple_partij
55+
test_diversin_bsn_in_diversi_partij
56+
test_same_bsn_in_diversi_partij
57+
test_bsn_non_deve_avere_il_parent
58+
59+
60+
test_kvk (uguali a bsn)
61+
test_kvk_in_partij
62+
test_kvk_in_partij_in_multiple_parti
63+
64+
65+
66+
67+
68+
Senza specificare il partij
69+
test_constraint_partij_identificator (prova a fare lo stesso object_id)
70+
test_constraint_partij_identificator (prova a fare con diversi object_id)
71+
72+
73+
74+
validator = PartijIdentificatorTypeValidator(
75+
code_objecttype="natuurlijk_persoon",
76+
code_soort_object_id="bsn",
77+
object_id="296648875",
78+
code_register=PartijIdentificatorCodeRegister.brp,
79+
)
80+
validator.validate()
81+
"""
82+
83+
84+
class PartijIdentificatorModelConstraints(TestCase):
85+
def setUp(self):
86+
super().setUp()
87+
self.partij = PartijFactory.create(voorkeurs_digitaal_adres=None)
88+
89+
def test_valid_scoped_identificator_globally_unique(self):
90+
PartijIdentificator.objects.create(
91+
sub_identificator_van=None,
92+
partij_identificator_code_objecttype="natuurlijk_persoon",
93+
partij_identificator_code_soort_object_id="bsn",
94+
partij_identificator_object_id="296648875",
95+
partij_identificator_code_register="brp",
96+
)
97+
PartijIdentificator.objects.create(
98+
sub_identificator_van=None,
99+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
100+
partij_identificator_code_soort_object_id="rsin",
101+
partij_identificator_object_id="296648875",
102+
partij_identificator_code_register="hr",
103+
)
104+
105+
def test_valid_non_scoped_identificator_globally_unique_different_sub_identificator_van(
106+
self,
107+
):
108+
partij_identificator_a = PartijIdentificator.objects.create(
109+
sub_identificator_van=None,
110+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
111+
partij_identificator_code_soort_object_id="kvk_nummer",
112+
partij_identificator_object_id="12345678",
113+
partij_identificator_code_register="hr",
114+
)
115+
partij_identificator_b = PartijIdentificator.objects.create(
116+
sub_identificator_van=None,
117+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
118+
partij_identificator_code_soort_object_id="kvk_nummer",
119+
partij_identificator_object_id="87654321",
120+
partij_identificator_code_register="hr",
121+
)
122+
123+
PartijIdentificator.objects.create(
124+
sub_identificator_van=partij_identificator_a,
125+
partij_identificator_code_objecttype="vestiging",
126+
partij_identificator_code_soort_object_id="vestigingsnummer",
127+
partij_identificator_object_id="123412341234",
128+
partij_identificator_code_register="hr",
129+
)
130+
131+
PartijIdentificator.objects.create(
132+
sub_identificator_van=partij_identificator_b,
133+
partij_identificator_code_objecttype="vestiging",
134+
partij_identificator_code_soort_object_id="vestigingsnummer",
135+
partij_identificator_object_id="123412341234",
136+
partij_identificator_code_register="hr",
137+
)
138+
139+
def test_valid_non_scoped_identificator_globally_unique_different_values(self):
140+
partij_identificator = PartijIdentificator.objects.create(
141+
sub_identificator_van=None,
142+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
143+
partij_identificator_code_soort_object_id="kvk_nummer",
144+
partij_identificator_object_id="12345678",
145+
partij_identificator_code_register="hr",
146+
)
147+
148+
PartijIdentificator.objects.create(
149+
sub_identificator_van=partij_identificator,
150+
partij_identificator_code_objecttype="vestiging",
151+
partij_identificator_code_soort_object_id="vestigingsnummer",
152+
partij_identificator_object_id="123412341234",
153+
partij_identificator_code_register="hr",
154+
)
155+
156+
PartijIdentificator.objects.create(
157+
sub_identificator_van=partij_identificator,
158+
partij_identificator_code_objecttype="vestiging",
159+
partij_identificator_code_soort_object_id="vestigingsnummer",
160+
partij_identificator_object_id="987698769876",
161+
partij_identificator_code_register="hr",
162+
)
163+
164+
def test_invalid_scoped_identificator_globally_unique(self):
165+
return
166+
PartijIdentificator.objects.create(
167+
sub_identificator_van=None,
168+
partij_identificator_code_objecttype="natuurlijk_persoon",
169+
partij_identificator_code_soort_object_id="bsn",
170+
partij_identificator_object_id="296648875",
171+
partij_identificator_code_register="brp",
172+
)
173+
PartijIdentificator.objects.create(
174+
sub_identificator_van=None,
175+
partij_identificator_code_objecttype="natuurlijk_persoon",
176+
partij_identificator_code_soort_object_id="bsn",
177+
partij_identificator_object_id="296648875",
178+
partij_identificator_code_register="brp",
179+
)

0 commit comments

Comments
 (0)