diff --git a/api/dashboard/location/location_serializer.py b/api/dashboard/location/location_serializer.py index 8aec9782..47184b4a 100644 --- a/api/dashboard/location/location_serializer.py +++ b/api/dashboard/location/location_serializer.py @@ -23,19 +23,24 @@ class Meta: model = Country fields = ["label"] + def create(self, validated_data): + validated_data['id'] = str(uuid.uuid4()) + validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() + validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") + + return super().create(validated_data) + def update(self, instance, validated_data): - validated_data['id'] = uuid.uuid4() validated_data["updated_at"] = DateTimeUtils.get_current_utc_time() validated_data["updated_by_id"] = self.context.get("user_id") return super().update(instance, validated_data) - def create(self, validated_data): - validated_data['id'] = uuid.uuid4() - validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() - validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") - - return super().create(validated_data) + def validate_label(self, country): + country_obj = Country.objects.filter(name=country).first() + if country_obj: + raise serializers.ValidationError('Country with this name is already exists') + return country class StateRetrievalSerializer(serializers.ModelSerializer): @@ -55,19 +60,24 @@ class Meta: model = State fields = ["label", "country"] + def create(self, validated_data): + validated_data['id'] = str(uuid.uuid4()) + validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() + validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") + + return super().create(validated_data) + def update(self, instance, validated_data): - validated_data['id'] = uuid.uuid4() validated_data["updated_at"] = DateTimeUtils.get_current_utc_time() validated_data["updated_by_id"] = self.context.get("user_id") return super().update(instance, validated_data) - def create(self, validated_data): - validated_data['id'] = uuid.uuid4() - validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() - validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") - - return super().create(validated_data) + def validate_label(self, state): + state_obj = State.objects.filter(name=state).first() + if state_obj: + raise serializers.ValidationError("State with this name is already exists") + return state class ZoneRetrievalSerializer(serializers.ModelSerializer): @@ -87,19 +97,24 @@ class Meta: model = Zone fields = ["label", "state"] + def create(self, validated_data): + validated_data['id'] = str(uuid.uuid4()) + validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() + validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") + + return super().create(validated_data) + def update(self, instance, validated_data): - validated_data['id'] = uuid.uuid4() validated_data["updated_at"] = DateTimeUtils.get_current_utc_time() validated_data["updated_by_id"] = self.context.get("user_id") return super().update(instance, validated_data) - def create(self, validated_data): - validated_data['id'] = uuid.uuid4() - validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() - validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") - - return super().create(validated_data) + def validate_label(self, zone): + zone_obj = Zone.objects.filter(name=zone).first() + if zone_obj: + raise serializers.ValidationError("Zone with this name is already exists") + return zone class DistrictRetrievalSerializer(serializers.ModelSerializer): @@ -119,16 +134,21 @@ class Meta: model = District fields = ["label", "zone"] + def create(self, validated_data): + validated_data['id'] = str(uuid.uuid4()) + validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() + validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") + + return super().create(validated_data) + def update(self, instance, validated_data): - validated_data['id'] = uuid.uuid4() validated_data["updated_at"] = DateTimeUtils.get_current_utc_time() validated_data["updated_by_id"] = self.context.get("user_id") return super().update(instance, validated_data) - def create(self, validated_data): - validated_data['id'] = uuid.uuid4() - validated_data["updated_at"] = validated_data["created_at"] = DateTimeUtils.get_current_utc_time() - validated_data["updated_by_id"] = validated_data["created_by_id"] = self.context.get("user_id") - - return super().create(validated_data) + def validate_label(self, district): + district_obj = District.objects.filter(name=district).first() + if district_obj: + raise serializers.ValidationError("District with this name is already exists") + return district diff --git a/api/dashboard/location/location_views.py b/api/dashboard/location/location_views.py index c4ebd977..c7a4d99e 100644 --- a/api/dashboard/location/location_views.py +++ b/api/dashboard/location/location_views.py @@ -52,11 +52,10 @@ def post(self, request): "user_id": user_id } ) - if serializer.is_valid(): serializer.save() return CustomResponse( - general_message=serializer.data + general_message="Country created Successfully" ).get_success_response() return CustomResponse( @@ -80,7 +79,7 @@ def patch(self, request, country_id): serializer.save() return CustomResponse( - general_message=serializer.data + general_message="Country edited successfully" ).get_success_response() return CustomResponse( @@ -148,7 +147,7 @@ def post(self, request): serializer.save() return CustomResponse( - general_message=serializer.data + general_message="State created successfully" ).get_success_response() return CustomResponse( @@ -176,7 +175,7 @@ def patch(self, request, state_id): ).get_success_response() return CustomResponse( - general_message=serializer.errors + general_message="State edited successfully" ).get_failure_response() @role_required([RoleType.ADMIN.value]) @@ -241,7 +240,7 @@ def post(self, request): serializer.save() return CustomResponse( - general_message=serializer.data + general_message="Zone created successfully" ).get_success_response() return CustomResponse( @@ -265,7 +264,7 @@ def patch(self, request, zone_id): serializer.save() return CustomResponse( - general_message=serializer.data + general_message="zone edited successfully" ).get_success_response() return CustomResponse( @@ -338,7 +337,7 @@ def post(self, request): ).get_success_response() return CustomResponse( - general_message=serializer.errors + general_message="Zone created successfully" ).get_failure_response() @role_required([RoleType.ADMIN.value]) @@ -358,7 +357,7 @@ def patch(self, request, district_id): serializer.save() return CustomResponse( - general_message=serializer.data + general_message="Zone edited successfully" ).get_success_response() return CustomResponse(