Skip to content

Commit 447d4e6

Browse files
[#267] Enforce uniqueness of Partij (#334)
* [#267] Added UniquePartijIdentificatorValidator and fixed old tests * [#267] Updated migrations * [#267] Delete duplicated tests * [#267] Created PartijIdentificatorAdminForm * [#267] Fixed PartijIdentificatorModelConstraints * [#267] Created tests for bsn and fixed old * [#267] Created PartijIdentificatorUniquenessValidatorTests * [#267] Improvements * [#267] Test improvements * [#267] PR suggestions * [#267] Removed partij constraint and fixed tests * [#267] Fixed tests and general improvements
1 parent cad6915 commit 447d4e6

File tree

13 files changed

+1307
-333
lines changed

13 files changed

+1307
-333
lines changed

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

+32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from django import forms
12
from django.contrib import admin
23
from django.utils.translation import gettext_lazy as _
34

45
from openklant.components.klantinteracties.models.rekeningnummers import Rekeningnummer
6+
from openklant.components.klantinteracties.models.validators import (
7+
PartijIdentificatorTypesValidator,
8+
PartijIdentificatorUniquenessValidator,
9+
)
510

611
from ..models.constants import SoortPartij
712
from ..models.digitaal_adres import DigitaalAdres
@@ -18,6 +23,32 @@
1823
)
1924

2025

26+
class PartijIdentificatorAdminForm(forms.ModelForm):
27+
class Meta:
28+
model = PartijIdentificator
29+
fields = "__all__"
30+
31+
def clean(self):
32+
cleaned_data = super().clean()
33+
PartijIdentificatorTypesValidator()(
34+
code_register=cleaned_data["partij_identificator_code_register"],
35+
code_objecttype=cleaned_data["partij_identificator_code_objecttype"],
36+
code_soort_object_id=cleaned_data[
37+
"partij_identificator_code_soort_object_id"
38+
],
39+
object_id=cleaned_data["partij_identificator_object_id"],
40+
)
41+
42+
PartijIdentificatorUniquenessValidator(
43+
code_soort_object_id=cleaned_data[
44+
"partij_identificator_code_soort_object_id"
45+
],
46+
sub_identificator_van=cleaned_data["sub_identificator_van"],
47+
)()
48+
49+
return cleaned_data
50+
51+
2152
class CategorieRelatieInlineAdmin(admin.StackedInline):
2253
model = CategorieRelatie
2354
readonly_fields = ("uuid",)
@@ -34,6 +65,7 @@ class CategorieRelatieInlineAdmin(admin.StackedInline):
3465
class PartijIdentificatorInlineAdmin(admin.StackedInline):
3566
readonly_fields = ("uuid",)
3667
model = PartijIdentificator
68+
form = PartijIdentificatorAdminForm
3769
extra = 0
3870

3971

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

+38-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from glom import PathAccessError, glom
88
from rest_framework import serializers
99
from vng_api_common.serializers import GegevensGroepSerializer, NestedGegevensGroepMixin
10+
from vng_api_common.utils import get_help_text
1011

1112
from openklant.components.klantinteracties.api.polymorphism import (
1213
Discriminator,
@@ -43,9 +44,11 @@
4344
)
4445
from openklant.components.klantinteracties.models.rekeningnummers import Rekeningnummer
4546
from openklant.components.klantinteracties.models.validators import (
46-
PartijIdentificatorValidator,
47+
PartijIdentificatorTypesValidator,
48+
PartijIdentificatorUniquenessValidator,
4749
)
48-
from openklant.utils.serializers import get_field_value
50+
from openklant.utils.decorators import handle_db_exceptions
51+
from openklant.utils.serializers import get_field_instance_by_uuid, get_field_value
4952

5053

5154
class PartijForeignkeyBaseSerializer(serializers.HyperlinkedModelSerializer):
@@ -364,6 +367,13 @@ class PartijIdentificatorGroepTypeSerializer(GegevensGroepSerializer):
364367
class Meta:
365368
model = PartijIdentificator
366369
gegevensgroep = "partij_identificator"
370+
extra_kwargs = {
371+
"code_register": {"required": True},
372+
"code_objecttype": {"required": True},
373+
"code_soort_object_id": {"required": True},
374+
"object_id": {"required": True},
375+
}
376+
validators = []
367377

368378

369379
class PartijIdentificatorSerializer(
@@ -383,6 +393,13 @@ class PartijIdentificatorSerializer(
383393
"of ander extern register uniek identificeren."
384394
),
385395
)
396+
sub_identificator_van = PartijIdentificatorForeignkeySerializer(
397+
required=False,
398+
allow_null=True,
399+
help_text=get_help_text(
400+
"klantinteracties.PartijIdentificator", "sub_identificator_van"
401+
),
402+
)
386403

387404
class Meta:
388405
model = PartijIdentificator
@@ -392,8 +409,8 @@ class Meta:
392409
"identificeerde_partij",
393410
"andere_partij_identificator",
394411
"partij_identificator",
412+
"sub_identificator_van",
395413
)
396-
397414
extra_kwargs = {
398415
"uuid": {"read_only": True},
399416
"url": {
@@ -405,29 +422,35 @@ class Meta:
405422

406423
def validate(self, attrs):
407424
partij_identificator = get_field_value(self, attrs, "partij_identificator")
408-
PartijIdentificatorValidator(
409-
code_register=partij_identificator["code_register"],
425+
sub_identificator_van = get_field_instance_by_uuid(
426+
self, attrs, "sub_identificator_van", PartijIdentificator
427+
)
428+
partij = get_field_instance_by_uuid(self, attrs, "partij", Partij)
429+
430+
PartijIdentificatorTypesValidator()(
410431
code_objecttype=partij_identificator["code_objecttype"],
411432
code_soort_object_id=partij_identificator["code_soort_object_id"],
412433
object_id=partij_identificator["object_id"],
413-
).validate()
434+
code_register=partij_identificator["code_register"],
435+
)
436+
PartijIdentificatorUniquenessValidator(
437+
code_soort_object_id=partij_identificator["code_soort_object_id"],
438+
sub_identificator_van=sub_identificator_van,
439+
)()
440+
441+
attrs["sub_identificator_van"] = sub_identificator_van
442+
attrs["partij"] = partij
443+
414444
return super().validate(attrs)
415445

446+
@handle_db_exceptions
416447
@transaction.atomic
417448
def update(self, instance, validated_data):
418-
if "partij" in validated_data:
419-
if partij := validated_data.pop("partij", None):
420-
validated_data["partij"] = Partij.objects.get(
421-
uuid=str(partij.get("uuid"))
422-
)
423-
424449
return super().update(instance, validated_data)
425450

451+
@handle_db_exceptions
426452
@transaction.atomic
427453
def create(self, validated_data):
428-
partij_uuid = str(validated_data.pop("partij").get("uuid"))
429-
validated_data["partij"] = Partij.objects.get(uuid=partij_uuid)
430-
431454
return super().create(validated_data)
432455

433456

0 commit comments

Comments
 (0)