43
43
)
44
44
from openklant .components .klantinteracties .models .rekeningnummers import Rekeningnummer
45
45
from openklant .components .klantinteracties .models .validators import (
46
- PartijIdentificatorValidator ,
47
- SubIdentificatorValidator ,
46
+ PartijIdentificatorTypesValidator ,
47
+ PartijIdentificatorUniquenessValidator ,
48
48
)
49
+ from openklant .utils .serializers import get_field_value
49
50
50
51
51
52
class PartijForeignkeyBaseSerializer (serializers .HyperlinkedModelSerializer ):
@@ -364,7 +365,7 @@ class PartijIdentificatorGroepTypeSerializer(GegevensGroepSerializer):
364
365
class Meta :
365
366
model = PartijIdentificator
366
367
gegevensgroep = "partij_identificator"
367
- validators = [PartijIdentificatorValidator () ]
368
+ validators = []
368
369
369
370
370
371
class PartijIdentificatorSerializer (
@@ -387,9 +388,7 @@ class PartijIdentificatorSerializer(
387
388
sub_identificator_van = PartijIdentificatorForeignkeySerializer (
388
389
required = False ,
389
390
allow_null = True ,
390
- help_text = _ (
391
- "Partij-identificatoren TEST CONTROLLA die hoorde bij deze partij."
392
- ),
391
+ help_text = _ ("Relatie sub_identificator_van" ),
393
392
)
394
393
395
394
class Meta :
@@ -410,36 +409,46 @@ class Meta:
410
409
"help_text" : "De unieke URL van deze partij indentificator binnen deze API." ,
411
410
},
412
411
}
413
- validators = [SubIdentificatorValidator ()]
414
412
415
413
def validate (self , attrs ):
416
- if sub_identificator_van := attrs .get ("sub_identificator_van" , None ):
417
- attrs ["sub_identificator_van" ] = PartijIdentificator .objects .filter (
414
+ instance = getattr (self , "instance" , None )
415
+ partij_identificator = get_field_value (self , attrs , "partij_identificator" )
416
+ sub_identificator_van = get_field_value (self , attrs , "sub_identificator_van" )
417
+ if sub_identificator_van :
418
+ sub_identificator_van = PartijIdentificator .objects .get (
418
419
uuid = sub_identificator_van ["uuid" ]
419
420
)
420
-
421
- SubIdentificatorValidator ()(attrs )
421
+ PartijIdentificatorTypesValidator (
422
+ partij_identificator = partij_identificator
423
+ ).validate ()
424
+ PartijIdentificatorUniquenessValidator (
425
+ queryset = (
426
+ PartijIdentificator .objects .exclude (pk = instance .pk )
427
+ if instance
428
+ else PartijIdentificator .objects .all ()
429
+ ),
430
+ partij_identificator = partij_identificator ,
431
+ sub_identificator_van = sub_identificator_van ,
432
+ instance = instance ,
433
+ ).check ()
422
434
return super ().validate (attrs )
423
435
424
436
@transaction .atomic
425
437
def update (self , instance , validated_data ):
426
- if "partij" in validated_data :
427
- if partij := validated_data .pop ("partij" , None ):
428
- validated_data ["partij" ] = Partij .objects .get (
429
- uuid = str (partij .get ("uuid" ))
430
- )
438
+ if partij := validated_data .get ("partij" , None ):
439
+ validated_data ["partij" ] = Partij .objects .get (uuid = partij ["uuid" ])
431
440
432
441
return super ().update (instance , validated_data )
433
442
434
443
@transaction .atomic
435
444
def create (self , validated_data ):
436
445
if sub_identificator_van := validated_data .get ("sub_identificator_van" , None ):
437
- validated_data ["sub_identificator_van" ] = (
438
- PartijIdentificator . objects . filter ( uuid = sub_identificator_van ["uuid" ])
446
+ validated_data ["sub_identificator_van" ] = PartijIdentificator . objects . get (
447
+ uuid = sub_identificator_van ["uuid" ]
439
448
)
440
449
441
- partij_uuid = str ( validated_data .pop ("partij" ). get ( "uuid" ))
442
- validated_data ["partij" ] = Partij .objects .get (uuid = partij_uuid )
450
+ if partij := validated_data .get ("partij" , None ):
451
+ validated_data ["partij" ] = Partij .objects .get (uuid = partij [ "uuid" ] )
443
452
444
453
return super ().create (validated_data )
445
454
0 commit comments