Skip to content

Commit b075fee

Browse files
[#267] Improvements
1 parent 7f249a1 commit b075fee

File tree

6 files changed

+197
-271
lines changed

6 files changed

+197
-271
lines changed

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class Meta:
3030

3131
def clean(self):
3232
cleaned_data = super().clean()
33-
sub_identificator_van = cleaned_data["sub_identificator_van"]
3433
partij_identificator = {
3534
"code_objecttype": cleaned_data["partij_identificator_code_objecttype"],
3635
"code_soort_object_id": cleaned_data[
@@ -47,16 +46,13 @@ def clean(self):
4746
queryset = PartijIdentificator.objects.exclude()
4847
if self.instance:
4948
queryset = queryset.exclude(pk=self.instance.pk)
49+
5050
PartijIdentificatorUniquenessValidator(
51-
queryset=(
52-
PartijIdentificator.objects.exclude(pk=self.instance.pk)
53-
if self.instance
54-
else PartijIdentificator.objects.all()
55-
),
56-
partij_identificator=partij_identificator,
57-
sub_identificator_van=sub_identificator_van,
5851
instance=self.instance,
59-
).check()
52+
partij_identificator=partij_identificator,
53+
sub_identificator_van=cleaned_data["sub_identificator_van"],
54+
partij=cleaned_data["partij"],
55+
).validate()
6056

6157
return cleaned_data
6258

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

+14-38
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
PartijIdentificatorTypesValidator,
4747
PartijIdentificatorUniquenessValidator,
4848
)
49-
from openklant.utils.serializers import get_field_value
49+
from openklant.utils.serializers import get_field_instance_by_uuid, get_field_value
5050

5151

5252
class PartijForeignkeyBaseSerializer(serializers.HyperlinkedModelSerializer):
@@ -422,48 +422,24 @@ class Meta:
422422
def validate(self, attrs):
423423
instance = getattr(self, "instance", None)
424424
partij_identificator = get_field_value(self, attrs, "partij_identificator")
425-
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:
428-
sub_identificator_van = PartijIdentificator.objects.get(
429-
uuid=sub_identificator_van["uuid"]
430-
)
425+
426+
sub_identificator_van = get_field_instance_by_uuid(
427+
self, attrs, "sub_identificator_van", PartijIdentificator
428+
)
429+
partij = get_field_instance_by_uuid(self, attrs, "partij", Partij)
431430

432431
PartijIdentificatorUniquenessValidator(
433-
queryset=(
434-
PartijIdentificator.objects.exclude(pk=instance.pk)
435-
if instance
436-
else PartijIdentificator.objects.all()
437-
),
432+
instance=instance,
438433
partij_identificator=partij_identificator,
439434
sub_identificator_van=sub_identificator_van,
440-
instance=instance,
441-
).check()
442-
return super().validate(attrs)
435+
partij=partij,
436+
).validate()
443437

444-
@transaction.atomic
445-
def update(self, instance, validated_data):
446-
if partij := validated_data.get("partij", None):
447-
validated_data["partij"] = Partij.objects.get(uuid=partij["uuid"])
448-
# TODO FAI QUESTO test
449-
breakpoint()
450-
if sub_identificator_van := validated_data.get("sub_identificator_van", None):
451-
validated_data["sub_identificator_van"] = PartijIdentificator.objects.get(
452-
uuid=sub_identificator_van["uuid"]
453-
)
454-
return super().update(instance, validated_data)
455-
456-
@transaction.atomic
457-
def create(self, validated_data):
458-
if sub_identificator_van := validated_data.get("sub_identificator_van", None):
459-
validated_data["sub_identificator_van"] = PartijIdentificator.objects.get(
460-
uuid=sub_identificator_van["uuid"]
461-
)
462-
463-
if partij := validated_data.get("partij", None):
464-
validated_data["partij"] = Partij.objects.get(uuid=partij["uuid"])
465-
466-
return super().create(validated_data)
438+
attrs["sub_identificator_van"] = get_field_instance_by_uuid(
439+
self, attrs, "sub_identificator_van", PartijIdentificator
440+
)
441+
attrs["partij"] = get_field_instance_by_uuid(self, attrs, "partij", Partij)
442+
return super().validate(attrs)
467443

468444

469445
class PartijSerializer(NestedGegevensGroepMixin, PolymorphicSerializer):

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

+70-116
Original file line numberDiff line numberDiff line change
@@ -2144,31 +2144,28 @@ def test_invalid_validation_partij_identificator_object_id(self):
21442144
"Deze waarde is ongeldig, reden: Waarde moet 9 tekens lang zijn",
21452145
)
21462146

2147-
2148-
class PartijIdentificatorUniquenessTests(APITestCase):
2149-
def setUp(self):
2150-
self.list_url = reverse("klantinteracties:partijidentificator-list")
2151-
self.partij = PartijFactory.create()
2152-
super().setUp()
2153-
21542147
def test_invalid_create_empty(self):
21552148
# all partij_identificator fields required
2149+
partij = PartijFactory.create()
2150+
list_url = reverse("klantinteracties:partijidentificator-list")
21562151
data = {
2157-
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2152+
"identificeerdePartij": {"uuid": str(partij.uuid)},
21582153
"anderePartijIdentificator": "anderePartijIdentificator",
21592154
"partijIdentificator": {},
21602155
}
21612156

2162-
response = self.client.post(self.list_url, data)
2157+
response = self.client.post(list_url, data)
21632158
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
21642159
self.assertEqual(response.data["code"], "invalid")
21652160
self.assertEqual(response.data["title"], "Invalid input.")
21662161
self.assertEqual(len(response.data["invalid_params"]), 4)
21672162

21682163
def test_invalid_create_partial(self):
21692164
# all partij_identificator fields required
2165+
partij = PartijFactory.create()
2166+
list_url = reverse("klantinteracties:partijidentificator-list")
21702167
data = {
2171-
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2168+
"identificeerdePartij": {"uuid": str(partij.uuid)},
21722169
"anderePartijIdentificator": "anderePartijIdentificator",
21732170
"partijIdentificator": {
21742171
"codeObjecttype": "natuurlijk_persoon",
@@ -2177,7 +2174,7 @@ def test_invalid_create_partial(self):
21772174
},
21782175
}
21792176

2180-
response = self.client.post(self.list_url, data)
2177+
response = self.client.post(list_url, data)
21812178
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
21822179
self.assertEqual(response.data["code"], "invalid")
21832180
self.assertEqual(response.data["title"], "Invalid input.")
@@ -2192,8 +2189,10 @@ def test_invalid_create_partial(self):
21922189

21932190
def test_invalid_update_partial(self):
21942191
# all partij_identificator fields required
2192+
partij = PartijFactory.create()
2193+
list_url = reverse("klantinteracties:partijidentificator-list")
21952194
partij_identificator = PartijIdentificatorFactory.create(
2196-
partij=self.partij,
2195+
partij=partij,
21972196
andere_partij_identificator="anderePartijIdentificator",
21982197
partij_identificator_code_objecttype="natuurlijk_persoon",
21992198
partij_identificator_code_soort_object_id="bsn",
@@ -2216,7 +2215,7 @@ def test_invalid_update_partial(self):
22162215

22172216
response = self.client.patch(detail_url, data)
22182217

2219-
response = self.client.post(self.list_url, data)
2218+
response = self.client.post(list_url, data)
22202219
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
22212220
self.assertEqual(response.data["code"], "invalid")
22222221
self.assertEqual(response.data["title"], "Invalid input.")
@@ -2229,6 +2228,13 @@ def test_invalid_update_partial(self):
22292228
)
22302229
self.assertEqual(PartijIdentificator.objects.all().count(), 1)
22312230

2231+
2232+
class PartijIdentificatorUniquenessTests(APITestCase):
2233+
def setUp(self):
2234+
self.list_url = reverse("klantinteracties:partijidentificator-list")
2235+
self.partij = PartijFactory.create()
2236+
super().setUp()
2237+
22322238
def test_bsn_valid_create(self):
22332239
data = {
22342240
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
@@ -2245,49 +2251,30 @@ def test_bsn_valid_create(self):
22452251
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
22462252
self.assertEqual(PartijIdentificator.objects.all().count(), 1)
22472253

2248-
def test_bsn_invalid_create_for_same_partij_unique(self):
2254+
def test_bsn_valid_create_sub_identificator_van(self):
2255+
# sub_identificator_van not allowed for partij_identificator_code_soort_object_id = 'bsn'
2256+
partij_identificator = PartijIdentificatorFactory.create(
2257+
partij=self.partij,
2258+
andere_partij_identificator="anderePartijIdentificator",
2259+
partij_identificator_code_objecttype="natuurlijk_persoon",
2260+
partij_identificator_code_soort_object_id="bsn",
2261+
partij_identificator_object_id="123456782",
2262+
partij_identificator_code_register="brp",
2263+
)
2264+
22492265
data = {
22502266
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2251-
"anderePartijIdentificator": "anderePartijIdentificator",
22522267
"partijIdentificator": {
22532268
"codeObjecttype": "natuurlijk_persoon",
22542269
"codeSoortObjectId": "bsn",
22552270
"objectId": "296648875",
22562271
"codeRegister": "brp",
22572272
},
22582273
}
2259-
2274+
data["sub_identificator_van"] = {"uuid": str(partij_identificator.uuid)}
22602275
response = self.client.post(self.list_url, data)
22612276
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)
2277+
self.assertEqual(PartijIdentificator.objects.all().count(), 2)
22912278

22922279
def test_bsn_invalid_create_global_unique(self):
22932280
PartijIdentificatorFactory.create(
@@ -2332,28 +2319,6 @@ def test_bsn_invalid_create_global_unique(self):
23322319
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie.",
23332320
)
23342321

2335-
def test_bsn_invalid_create_sub_identificator_van(self):
2336-
# sub_identificator_van not allowed for partij_identificator_code_soort_object_id = 'bsn'
2337-
partij_identificator = PartijIdentificatorFactory.create()
2338-
data = {
2339-
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2340-
"partijIdentificator": {
2341-
"codeObjecttype": "natuurlijk_persoon",
2342-
"codeSoortObjectId": "bsn",
2343-
"objectId": "296648875",
2344-
"codeRegister": "brp",
2345-
},
2346-
}
2347-
data["sub_identificator_van"] = {"uuid": str(partij_identificator.uuid)}
2348-
response = self.client.post(self.list_url, data)
2349-
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
2350-
self.assertEqual(response.data["code"], "invalid")
2351-
self.assertEqual(response.data["title"], "Invalid input.")
2352-
self.assertEqual(
2353-
response.data["invalid_params"][0]["reason"],
2354-
"Alleen een identifier met code_soort_object_id kan een `sub_identificator_van' hebben.",
2355-
)
2356-
23572322
def test_bsn_valid_partial_update_unique(self):
23582323
partij_identificator = PartijIdentificatorFactory.create(
23592324
partij=self.partij,
@@ -2571,54 +2536,6 @@ def test_vestigingsnummer_invalid_create_invalid_sub_identificator_van(self):
25712536
"Het is alleen mogelijk om sub_identifier_vans te selecteren die CodeSoortObjectId = `kvk_nummer` hebben.",
25722537
)
25732538

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-
26222539
def test_vestigingsnummer_invalid_create(self):
26232540
sub_identificator_van = PartijIdentificatorFactory.create(
26242541
partij=self.partij,
@@ -2733,6 +2650,43 @@ def test_vestigingsnummer_invalid_update_set_sub_identificator_van_null(self):
27332650
in response.data["invalid_params"][0]["reason"]
27342651
)
27352652

2653+
def test_invalid_vestigingsnummer_and_kvk_nummer_combination_unique(self):
2654+
sub_identificator_van = PartijIdentificatorFactory.create(
2655+
partij=self.partij,
2656+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
2657+
partij_identificator_code_soort_object_id="kvk_nummer",
2658+
partij_identificator_object_id="12345678",
2659+
partij_identificator_code_register="hr",
2660+
)
2661+
PartijIdentificatorFactory.create(
2662+
partij=self.partij,
2663+
sub_identificator_van=sub_identificator_van,
2664+
partij_identificator_code_objecttype="vestiging",
2665+
partij_identificator_code_soort_object_id="vestigingsnummer",
2666+
partij_identificator_object_id="296648875154",
2667+
partij_identificator_code_register="hr",
2668+
)
2669+
# Same sub_identificator_van and same data_values
2670+
data = {
2671+
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2672+
"sub_identificator_van": {"uuid": str(sub_identificator_van.uuid)},
2673+
"partijIdentificator": {
2674+
"codeObjecttype": "vestiging",
2675+
"codeSoortObjectId": "vestigingsnummer",
2676+
"objectId": "296648875154",
2677+
"codeRegister": "hr",
2678+
},
2679+
}
2680+
2681+
response = self.client.post(self.list_url, data)
2682+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
2683+
self.assertEqual(response.data["code"], "invalid")
2684+
self.assertEqual(response.data["title"], "Invalid input.")
2685+
self.assertEqual(
2686+
response.data["invalid_params"][0]["reason"],
2687+
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie.",
2688+
)
2689+
27362690
def test_vestigingsnummer_invalid_update_unique(self):
27372691
sub_identificator_van_a = PartijIdentificatorFactory.create(
27382692
partij=self.partij,
@@ -2756,7 +2710,7 @@ def test_vestigingsnummer_invalid_update_unique(self):
27562710
partij_identificator_object_id="12345678",
27572711
partij_identificator_code_register="hr",
27582712
)
2759-
partij_identificator_b = PartijIdentificatorFactory.create(
2713+
PartijIdentificatorFactory.create(
27602714
partij=self.partij,
27612715
sub_identificator_van=sub_identificator_van_b,
27622716
partij_identificator_code_objecttype="vestiging",

0 commit comments

Comments
 (0)