99from django .utils .translation import gettext
1010from rest_framework import serializers
1111from rest_framework .exceptions import PermissionDenied
12+ from rest_framework .validators import UniqueTogetherValidator
1213
1314from api .serializers import (
1415 Admin2Serializer ,
@@ -557,6 +558,7 @@ class SimplifiedEAPSerializer(
557558):
558559
559560 # FILES
561+ version = serializers .IntegerField (default = 1 , read_only = True )
560562 hazard_impact_images = EAPFileUpdateSerializer (required = False , many = True )
561563 selected_early_actions_images = EAPFileUpdateSerializer (required = False , many = True , allow_null = True )
562564 risk_selected_protocols_images = EAPFileUpdateSerializer (required = False , many = True , allow_null = True )
@@ -585,6 +587,13 @@ class Meta:
585587 "created_by" ,
586588 "modified_by" ,
587589 ]
590+ validators = [
591+ UniqueTogetherValidator (
592+ queryset = SimplifiedEAP .objects .all (),
593+ fields = ["eap_registration" , "version" ],
594+ message = "EAP for this registration has already been created." ,
595+ )
596+ ]
588597 exclude = ("cover_image" ,)
589598
590599 def _validate_timeframe (self , data : dict [str , typing .Any ]) -> None :
@@ -636,11 +645,6 @@ def _validate_timeframe(self, data: dict[str, typing.Any]) -> None:
636645 {"operational_timeframe" : gettext ("operational timeframe value is not valid for Months unit." )}
637646 )
638647
639- def validate_eap_registration (self , eap_registration : EAPRegistration ) -> EAPRegistration :
640- if not self .instance and eap_registration .has_eap_application :
641- raise serializers .ValidationError ("EAP for this registration has already been created." )
642- return eap_registration
643-
644648 def validate (self , data : dict [str , typing .Any ]) -> dict [str , typing .Any ]:
645649 original_eap_registration = getattr (self .instance , "eap_registration" , None ) if self .instance else None
646650 eap_registration : EAPRegistration | None = data .get ("eap_registration" , original_eap_registration )
@@ -649,6 +653,9 @@ def validate(self, data: dict[str, typing.Any]) -> dict[str, typing.Any]:
649653 if self .instance and original_eap_registration != eap_registration :
650654 raise serializers .ValidationError ("EAP Registration cannot be changed for existing EAP." )
651655
656+ if not self .instance and eap_registration .has_eap_application :
657+ raise serializers .ValidationError (gettext ("EAP for this registration has already been created." ))
658+
652659 if self .instance and eap_registration .get_status_enum not in [
653660 EAPRegistration .Status .UNDER_DEVELOPMENT ,
654661 EAPRegistration .Status .NS_ADDRESSING_COMMENTS ,
@@ -690,6 +697,7 @@ class FullEAPSerializer(
690697 CommonEAPFieldsSerializer ,
691698):
692699
700+ version = serializers .IntegerField (default = 1 , read_only = True )
693701 # admins
694702 key_actors = KeyActorSerializer (many = True , required = True )
695703
@@ -795,6 +803,13 @@ class Meta:
795803 "created_by" ,
796804 "modified_by" ,
797805 ]
806+ validators = [
807+ UniqueTogetherValidator (
808+ queryset = FullEAP .objects .all (),
809+ fields = ["eap_registration" , "version" ],
810+ message = "EAP for this registration has already been created." ,
811+ )
812+ ]
798813 exclude = ("cover_image" ,)
799814
800815 def _validate_timeframe (self , data : dict [str , typing .Any ]) -> None :
@@ -811,11 +826,6 @@ def _validate_timeframe(self, data: dict[str, typing.Any]) -> None:
811826 if lead_unit is not None and lead_time_value is not None and lead_unit != TimeFrame .DAYS :
812827 raise serializers .ValidationError ({"lead_timeframe_unit" : gettext ("lead timeframe unit must be Days for Full EAP." )})
813828
814- def validate_eap_registration (self , eap_registration : EAPRegistration ) -> EAPRegistration :
815- if not self .instance and eap_registration .has_eap_application :
816- raise serializers .ValidationError ("EAP for this registration has already been created." )
817- return eap_registration
818-
819829 def validate (self , data : dict [str , typing .Any ]) -> dict [str , typing .Any ]:
820830 original_eap_registration = getattr (self .instance , "eap_registration" , None ) if self .instance else None
821831 eap_registration : EAPRegistration | None = data .get ("eap_registration" , original_eap_registration )
@@ -824,6 +834,9 @@ def validate(self, data: dict[str, typing.Any]) -> dict[str, typing.Any]:
824834 if self .instance and original_eap_registration != eap_registration :
825835 raise serializers .ValidationError ("EAP Registration cannot be changed for existing EAP." )
826836
837+ if not self .instance and eap_registration .has_eap_application :
838+ raise serializers .ValidationError (gettext ("EAP for this registration has already been created." ))
839+
827840 if self .instance and eap_registration .get_status_enum not in [
828841 EAPRegistration .Status .UNDER_DEVELOPMENT ,
829842 EAPRegistration .Status .NS_ADDRESSING_COMMENTS ,
0 commit comments