Skip to content

Commit ba7db7b

Browse files
[#267] Improvements
1 parent 2b96fb6 commit ba7db7b

File tree

5 files changed

+98
-195
lines changed

5 files changed

+98
-195
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def clean(self):
5656
partij_identificator=partij_identificator,
5757
sub_identificator_van=sub_identificator_van,
5858
instance=self.instance,
59-
).check()
59+
).validate()
6060

6161
return cleaned_data
6262

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

+8-15
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242
Vertegenwoordigden,
4343
)
4444
from openklant.components.klantinteracties.models.rekeningnummers import Rekeningnummer
45-
from openklant.components.klantinteracties.models.validators import (
46-
PartijIdentificatorTypesValidator,
47-
PartijIdentificatorUniquenessValidator,
48-
)
45+
from openklant.components.klantinteracties.models.validators import *
4946
from openklant.utils.serializers import get_field_value
5047

5148

@@ -422,31 +419,27 @@ class Meta:
422419
def validate(self, attrs):
423420
instance = getattr(self, "instance", None)
424421
partij_identificator = get_field_value(self, attrs, "partij_identificator")
422+
partij = attrs.get("partij", None)
423+
# todo fai con
425424
sub_identificator_van = get_field_value(self, attrs, "sub_identificator_van")
426-
partij = get_field_value(self, attrs, "partij")
427-
if "sub_identificator_van" in attrs and sub_identificator_van:
425+
if sub_identificator_van and "sub_identificator_van" in attrs:
428426
sub_identificator_van = PartijIdentificator.objects.get(
429427
uuid=sub_identificator_van["uuid"]
430428
)
431-
432429
PartijIdentificatorUniquenessValidator(
433-
queryset=(
434-
PartijIdentificator.objects.exclude(pk=instance.pk)
435-
if instance
436-
else PartijIdentificator.objects.all()
437-
),
430+
instance=instance,
438431
partij_identificator=partij_identificator,
439432
sub_identificator_van=sub_identificator_van,
440-
instance=instance,
441-
).check()
433+
partij=partij,
434+
).validate()
442435
return super().validate(attrs)
443436

444437
@transaction.atomic
445438
def update(self, instance, validated_data):
446439
if partij := validated_data.get("partij", None):
447440
validated_data["partij"] = Partij.objects.get(uuid=partij["uuid"])
448441
# TODO FAI QUESTO test
449-
breakpoint()
442+
450443
if sub_identificator_van := validated_data.get("sub_identificator_van", None):
451444
validated_data["sub_identificator_van"] = PartijIdentificator.objects.get(
452445
uuid=sub_identificator_van["uuid"]

src/openklant/components/klantinteracties/api/tests/test_partijen.py

+1-93
Original file line numberDiff line numberDiff line change
@@ -2245,50 +2245,6 @@ def test_bsn_valid_create(self):
22452245
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
22462246
self.assertEqual(PartijIdentificator.objects.all().count(), 1)
22472247

2248-
def test_bsn_invalid_create_for_same_partij_unique(self):
2249-
data = {
2250-
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2251-
"anderePartijIdentificator": "anderePartijIdentificator",
2252-
"partijIdentificator": {
2253-
"codeObjecttype": "natuurlijk_persoon",
2254-
"codeSoortObjectId": "bsn",
2255-
"objectId": "296648875",
2256-
"codeRegister": "brp",
2257-
},
2258-
}
2259-
2260-
response = self.client.post(self.list_url, data)
2261-
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2262-
self.assertEqual(PartijIdentificator.objects.all().count(), 1)
2263-
2264-
with self.subTest("failed_subtest_same_partij_1"):
2265-
# same partij, same data_values
2266-
response = self.client.post(self.list_url, data)
2267-
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
2268-
self.assertEqual(response.data["code"], "invalid")
2269-
self.assertEqual(response.data["title"], "Invalid input.")
2270-
self.assertEqual(
2271-
response.data["invalid_params"][0]["reason"],
2272-
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie.",
2273-
)
2274-
2275-
with self.subTest("failed_subtest_same_partij_2"):
2276-
# same partij, new data_values
2277-
data = {
2278-
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2279-
"anderePartijIdentificator": "anderePartijIdentificator",
2280-
"partijIdentificator": {
2281-
"codeObjecttype": "natuurlijk_persoon",
2282-
"codeSoortObjectId": "bsn",
2283-
"objectId": "123456782",
2284-
"codeRegister": "brp",
2285-
},
2286-
}
2287-
2288-
response = self.client.post(self.list_url, data)
2289-
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2290-
self.assertEqual(PartijIdentificator.objects.all().count(), 2)
2291-
22922248
def test_bsn_invalid_create_global_unique(self):
22932249
PartijIdentificatorFactory.create(
22942250
partij=self.partij,
@@ -2351,7 +2307,7 @@ def test_bsn_invalid_create_sub_identificator_van(self):
23512307
self.assertEqual(response.data["title"], "Invalid input.")
23522308
self.assertEqual(
23532309
response.data["invalid_params"][0]["reason"],
2354-
"Alleen een identifier met code_soort_object_id kan een `sub_identificator_van' hebben.",
2310+
"Deze PartijIdentificator kan geen `sub_identificator_van`",
23552311
)
23562312

23572313
def test_bsn_valid_partial_update_unique(self):
@@ -2571,54 +2527,6 @@ def test_vestigingsnummer_invalid_create_invalid_sub_identificator_van(self):
25712527
"Het is alleen mogelijk om sub_identifier_vans te selecteren die CodeSoortObjectId = `kvk_nummer` hebben.",
25722528
)
25732529

2574-
def test_vestigingsnummer_valid_create(self):
2575-
sub_identificator_van = PartijIdentificatorFactory.create(
2576-
partij=self.partij,
2577-
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
2578-
partij_identificator_code_soort_object_id="kvk_nummer",
2579-
partij_identificator_object_id="12345678",
2580-
partij_identificator_code_register="hr",
2581-
)
2582-
2583-
data = {
2584-
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2585-
"sub_identificator_van": {"uuid": str(sub_identificator_van.uuid)},
2586-
"partijIdentificator": {
2587-
"codeObjecttype": "vestiging",
2588-
"codeSoortObjectId": "vestigingsnummer",
2589-
"objectId": "296648875154",
2590-
"codeRegister": "hr",
2591-
},
2592-
}
2593-
2594-
response = self.client.post(self.list_url, data)
2595-
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2596-
partij_identificatoren = PartijIdentificator.objects.all()
2597-
self.assertEqual(partij_identificatoren.count(), 2)
2598-
2599-
with self.subTest("subtest_global_1"):
2600-
# create new with objectId changed
2601-
data["partijIdentificator"]["objectId"] = "123412341234"
2602-
response = self.client.post(self.list_url, data)
2603-
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2604-
partij_identificatoren = PartijIdentificator.objects.all()
2605-
self.assertEqual(partij_identificatoren.count(), 3)
2606-
2607-
with self.subTest("subtest_global_2"):
2608-
# create new with sub_identificator_van changed
2609-
sub_identificator_van = PartijIdentificatorFactory.create(
2610-
partij=self.partij,
2611-
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
2612-
partij_identificator_code_soort_object_id="kvk_nummer",
2613-
partij_identificator_object_id="87654321",
2614-
partij_identificator_code_register="hr",
2615-
)
2616-
data["sub_identificator_van"] = {"uuid": str(sub_identificator_van.uuid)}
2617-
response = self.client.post(self.list_url, data)
2618-
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2619-
partij_identificatoren = PartijIdentificator.objects.all()
2620-
self.assertEqual(partij_identificatoren.count(), 5)
2621-
26222530
def test_vestigingsnummer_invalid_create(self):
26232531
sub_identificator_van = PartijIdentificatorFactory.create(
26242532
partij=self.partij,

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

+81-73
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
validate_bsn,
77
validate_rsin,
88
)
9-
9+
from openklant.components.klantinteracties.models.partijen import PartijIdentificator
1010
from .constants import (
1111
PartijIdentificatorCodeObjectType,
1212
PartijIdentificatorCodeRegister,
@@ -18,22 +18,34 @@
1818

1919

2020
class PartijIdentificatorUniquenessValidator:
21-
def __init__(self, queryset, partij_identificator, sub_identificator_van, instance):
22-
self.partij_identificator = partij_identificator
23-
self.sub_identificator_van = sub_identificator_van
24-
self.instance = instance
25-
self.queryset = queryset
26-
27-
def check(self):
28-
self.check_self_relation()
29-
self.check_sub_identificator_van()
30-
self.check_identificator_partij_unique()
31-
self.check_identificator_globally_unique()
32-
33-
def check_self_relation(self):
34-
"""
35-
Checks that the Identificator, does not refer to itself
36-
"""
21+
def __init__(self, *args, **kwargs):
22+
self.partij_identificator = kwargs.get("partij_identificator")
23+
self.sub_identificator_van = kwargs.get("sub_identificator_van")
24+
self.instance = kwargs.get("instance")
25+
self.partij = kwargs.get("partij")
26+
self.queryset = PartijIdentificator.objects.all()
27+
28+
if self.instance:
29+
self.queryset = self.queryset.exclude(pk=self.instance.pk)
30+
31+
if not self.partij_identificator:
32+
raise ValueError("partij_identificator is required")
33+
34+
def validate(self):
35+
self.validate_not_self_assigned()
36+
37+
if (
38+
self.partij_identificator["code_soort_object_id"]
39+
== PartijIdentificatorCodeSoortObjectId.vestigingsnummer.value
40+
):
41+
self.validate_sub_identificator_van_exists()
42+
else:
43+
self.validate_sub_identificator_van_not_exists()
44+
45+
self.validate_unique_partij_identificator_locally()
46+
self.validate_unique_partij_identificator_globally()
47+
48+
def validate_not_self_assigned(self):
3749
if self.sub_identificator_van and self.sub_identificator_van == self.instance:
3850
raise ValidationError(
3951
{
@@ -43,71 +55,65 @@ def check_self_relation(self):
4355
}
4456
)
4557

46-
def check_sub_identificator_van(self):
47-
"""
48-
Check the reference to the sub_identificator_van
58+
def validate_sub_identificator_van_exists(self):
59+
if not self.sub_identificator_van:
60+
raise ValidationError(
61+
{
62+
"sub_identificator_van": _(
63+
"Voor de Identificator met CodeSoortObjectId = `vestigingsnummer` is het verplicht om"
64+
" een `sub_identifier_van` met CodeSoortObjectId = `kvk_nummer` te kiezen."
65+
)
66+
}
67+
)
4968

50-
Allowed cases:
51-
- if `sub_identificator_van` exist, it must have the `partij_identificator_code_soort_object_id`
52-
equal to `kvk_nummer` and the `partij_identificator_code_soort_object_id` must be `vestigingsnummer`.
69+
if (
70+
self.sub_identificator_van.partij_identificator_code_soort_object_id
71+
!= PartijIdentificatorCodeSoortObjectId.kvk_nummer.value
72+
):
73+
raise ValidationError(
74+
{
75+
"sub_identificator_van": _(
76+
"Het is alleen mogelijk om sub_identifier_vans te selecteren"
77+
" die CodeSoortObjectId = `kvk_nummer` hebben."
78+
)
79+
}
80+
)
5381

54-
- if not `sub_identificator_van` exist, `partij_identificator_code_soort_object_id`
55-
can't be `vestigingsnummer`.
56-
"""
82+
def validate_sub_identificator_van_not_exists(self):
5783
if self.sub_identificator_van:
58-
if (
59-
self.partij_identificator["code_soort_object_id"]
60-
!= PartijIdentificatorCodeSoortObjectId.vestigingsnummer.value
61-
):
62-
raise ValidationError(
63-
{
64-
"sub_identificator_van": _(
65-
"Alleen een identifier met code_soort_object_id kan een `sub_identificator_van' hebben."
66-
)
67-
}
68-
)
69-
if (
70-
self.sub_identificator_van.partij_identificator_code_soort_object_id
71-
!= PartijIdentificatorCodeSoortObjectId.kvk_nummer.value
72-
):
73-
raise ValidationError(
74-
{
75-
"sub_identificator_van": _(
76-
"Het is alleen mogelijk om sub_identifier_vans te selecteren"
77-
" die CodeSoortObjectId = `kvk_nummer` hebben."
78-
)
79-
}
80-
)
81-
else:
82-
if (
83-
self.partij_identificator
84-
and self.partij_identificator["code_soort_object_id"]
85-
== PartijIdentificatorCodeSoortObjectId.vestigingsnummer.value
86-
):
87-
raise ValidationError(
88-
{
89-
"sub_identificator_van": _(
90-
"Voor de Identificator met CodeSoortObjectId = `vestigingsnummer` is het verplicht om"
91-
" een `sub_identifier_van` met CodeSoortObjectId = `kvk_nummer` te kiezen."
92-
)
93-
}
94-
)
95-
96-
def check_identificator_partij_unique(self):
97-
return
98-
if self.queryset.filter(**filters).exists():
9984
raise ValidationError(
10085
{
101-
"__all__": _(
102-
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie."
86+
"sub_identificator_van": _(
87+
"Deze PartijIdentificator kan geen `sub_identificator_van`"
10388
)
10489
}
10590
)
10691

107-
def check_identificator_globally_unique(self):
108-
"""
109-
Checks models constraints
110-
"""
92+
def validate_unique_partij_identificator_locally(self):
93+
return # TODO: for the moment it is blocked
94+
if (
95+
self.partij
96+
and PartijIdentificator.objects.filter(
97+
partij__uuid=str(self.partij["uuid"])
98+
)
99+
.filter(
100+
partij_identificator_code_soort_object_id=self.partij_identificator[
101+
"code_soort_object_id"
102+
]
103+
)
104+
.exists()
105+
):
106+
# TODO fai la testo
107+
raise ValidationError(
108+
{
109+
"sub_identificator_van": _(
110+
"Er is al een PartyIdentificator met dit CodeSoortObjectId = '%s' voor deze Partij."
111+
% (self.partij_identificator["code_soort_object_id"])
112+
)
113+
}
114+
)
115+
116+
def validate_unique_partij_identificator_globally(self):
111117
filters = {
112118
"partij_identificator_code_objecttype": self.partij_identificator[
113119
"code_objecttype"
@@ -122,6 +128,7 @@ def check_identificator_globally_unique(self):
122128
}
123129
if self.sub_identificator_van:
124130
filters["sub_identificator_van"] = self.sub_identificator_van
131+
125132
if self.queryset.filter(**filters).exists():
126133
raise ValidationError(
127134
{
@@ -130,6 +137,7 @@ def check_identificator_globally_unique(self):
130137
)
131138
}
132139
)
140+
return
133141

134142

135143
class PartijIdentificatorTypesValidator:

0 commit comments

Comments
 (0)