1
1
import datetime
2
+ from collections import Counter
2
3
3
4
from django .db import transaction
4
5
from django .utils .translation import gettext_lazy as _
@@ -412,7 +413,7 @@ class Meta:
412
413
"sub_identificator_van" ,
413
414
)
414
415
extra_kwargs = {
415
- "uuid" : {"read_only " : True },
416
+ "uuid" : {"required " : False , "validators" : [ partij_identificator_exists ] },
416
417
"url" : {
417
418
"view_name" : "klantinteracties:partijidentificator-detail" ,
418
419
"lookup_field" : "uuid" ,
@@ -443,7 +444,7 @@ def validate(self, attrs):
443
444
444
445
return super ().validate (attrs )
445
446
446
- def check_partij (self , partij ):
447
+ def assert_partij (self , partij ):
447
448
if not partij :
448
449
raise serializers .ValidationError (
449
450
{"identificeerdePartij" : _ ("Dit veld is vereist." )},
@@ -453,13 +454,13 @@ def check_partij(self, partij):
453
454
@handle_db_exceptions
454
455
@transaction .atomic
455
456
def update (self , instance , validated_data ):
456
- self .check_partij (validated_data ["partij" ])
457
+ self .assert_partij (validated_data ["partij" ])
457
458
return super ().update (instance , validated_data )
458
459
459
460
@handle_db_exceptions
460
461
@transaction .atomic
461
462
def create (self , validated_data ):
462
- self .check_partij (validated_data ["partij" ])
463
+ self .assert_partij (validated_data ["partij" ])
463
464
return super ().create (validated_data )
464
465
465
466
@@ -600,11 +601,60 @@ def get_vertegenwoordigden(self, obj):
600
601
if vertegenwoordigende
601
602
]
602
603
604
+ def validate_partij_identificatoren (self , attrs ):
605
+ if attrs :
606
+ if (
607
+ len (
608
+ Counter (
609
+ item ["partij" ] for item in attrs if item ["partij" ] is not None
610
+ )
611
+ )
612
+ > 0
613
+ ):
614
+ raise serializers .ValidationError (
615
+ {
616
+ "identificeerdePartij" : _ (
617
+ "Het veld `identificeerde_partij` wordt automatisch ingesteld en"
618
+ " hoeft niet te worden opgegeven."
619
+ )
620
+ },
621
+ code = "invalid" ,
622
+ )
623
+
624
+ uuid_list = [item ["uuid" ] for item in attrs if "uuid" in attrs ]
625
+ if uuid_list and max (Counter (uuid_list ).values ()) > 1 :
626
+ raise serializers .ValidationError (
627
+ {
628
+ "identificeerdePartij" : _ (
629
+ "Duplicaat uuid kan niet worden ingevoerd voor `partij_identificatoren`."
630
+ )
631
+ },
632
+ code = "duplicated" ,
633
+ )
634
+ return attrs
635
+
636
+ def update_or_create_partij_identificator (self , partij_identificator ):
637
+ partij_identificator_serializer = PartijIdentificatorSerializer (
638
+ data = partij_identificator
639
+ )
640
+ partij_identificator_serializer .is_valid (raise_exception = True )
641
+ if "uuid" in partij_identificator :
642
+ instance = PartijIdentificator .objects .get (
643
+ uuid = partij_identificator ["uuid" ]
644
+ )
645
+ partij_identificator_serializer .update (
646
+ instance , partij_identificator_serializer .validated_data
647
+ )
648
+ else :
649
+ partij_identificator_serializer .create (
650
+ partij_identificator_serializer .validated_data
651
+ )
652
+
603
653
@transaction .atomic
604
654
def update (self , instance , validated_data ):
605
655
method = self .context .get ("request" ).method
606
656
partij_identificatie = validated_data .pop ("partij_identificatie" , None )
607
-
657
+ partij_identificatoren = validated_data . pop ( "partijidentificator_set" , None )
608
658
if "digitaaladres_set" in validated_data :
609
659
existing_digitale_adressen = instance .digitaaladres_set .all ()
610
660
digitaal_adres_uuids = [
@@ -763,6 +813,17 @@ def update(self, instance, validated_data):
763
813
partij_identificatie ["partij" ] = partij
764
814
serializer .create (partij_identificatie )
765
815
816
+ if partij_identificatoren is not None :
817
+ partij .partijidentificator_set .exclude (
818
+ uuid__in = [pi ["uuid" ] for pi in partij_identificatoren if "uuid" in pi ]
819
+ ).delete ()
820
+
821
+ for partij_identificator in partij_identificatoren :
822
+ partij_identificator ["identificeerde_partij" ] = {
823
+ "uuid" : str (partij .uuid )
824
+ }
825
+ self .update_or_create_partij_identificator (partij_identificator )
826
+
766
827
return partij
767
828
768
829
@transaction .atomic
@@ -850,25 +911,11 @@ def create(self, validated_data):
850
911
851
912
if partij_identificatoren :
852
913
for partij_identificator in partij_identificatoren :
853
- if partij_identificator .get ("partij" , "" ):
854
- raise serializers .ValidationError (
855
- {
856
- "partijIdentificatoren.identificeerdePartij" : _ (
857
- "Het veld `identificeerde_partij` wordt automatisch ingesteld en hoeft niet te worden opgegeven."
858
- )
859
- },
860
- code = "invalid" ,
861
- )
862
914
partij_identificator ["identificeerde_partij" ] = {
863
915
"uuid" : str (partij .uuid )
864
916
}
865
- partij_identificator_serializer = PartijIdentificatorSerializer (
866
- data = partij_identificator
867
- )
868
- partij_identificator_serializer .is_valid (raise_exception = True )
869
- partij_identificator_serializer .create (
870
- partij_identificator_serializer .validated_data
871
- )
917
+ self .update_or_create_partij_identificator (partij_identificator )
918
+
872
919
return partij
873
920
874
921
0 commit comments