7
7
from glom import PathAccessError , glom
8
8
from rest_framework import serializers
9
9
from vng_api_common .serializers import GegevensGroepSerializer , NestedGegevensGroepMixin
10
+ from vng_api_common .utils import get_help_text
10
11
11
12
from openklant .components .klantinteracties .api .polymorphism import (
12
13
Discriminator ,
43
44
)
44
45
from openklant .components .klantinteracties .models .rekeningnummers import Rekeningnummer
45
46
from openklant .components .klantinteracties .models .validators import (
46
- PartijIdentificatorValidator ,
47
+ PartijIdentificatorTypesValidator ,
48
+ PartijIdentificatorUniquenessValidator ,
47
49
)
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
49
52
50
53
51
54
class PartijForeignkeyBaseSerializer (serializers .HyperlinkedModelSerializer ):
@@ -364,6 +367,13 @@ class PartijIdentificatorGroepTypeSerializer(GegevensGroepSerializer):
364
367
class Meta :
365
368
model = PartijIdentificator
366
369
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 = []
367
377
368
378
369
379
class PartijIdentificatorSerializer (
@@ -383,6 +393,13 @@ class PartijIdentificatorSerializer(
383
393
"of ander extern register uniek identificeren."
384
394
),
385
395
)
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
+ )
386
403
387
404
class Meta :
388
405
model = PartijIdentificator
@@ -392,8 +409,8 @@ class Meta:
392
409
"identificeerde_partij" ,
393
410
"andere_partij_identificator" ,
394
411
"partij_identificator" ,
412
+ "sub_identificator_van" ,
395
413
)
396
-
397
414
extra_kwargs = {
398
415
"uuid" : {"read_only" : True },
399
416
"url" : {
@@ -405,29 +422,35 @@ class Meta:
405
422
406
423
def validate (self , attrs ):
407
424
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 ()(
410
431
code_objecttype = partij_identificator ["code_objecttype" ],
411
432
code_soort_object_id = partij_identificator ["code_soort_object_id" ],
412
433
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
+
414
444
return super ().validate (attrs )
415
445
446
+ @handle_db_exceptions
416
447
@transaction .atomic
417
448
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
-
424
449
return super ().update (instance , validated_data )
425
450
451
+ @handle_db_exceptions
426
452
@transaction .atomic
427
453
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
-
431
454
return super ().create (validated_data )
432
455
433
456
0 commit comments