From b546757876ac18a1e1b1ab1e4223ac781cb00fdd Mon Sep 17 00:00:00 2001 From: jelanmathewjames Date: Wed, 3 Jul 2024 19:47:45 +0530 Subject: [PATCH] refactor: Update UserProfileUpdateSerializer to validate email uniqueness --- api/launchpad/serializers.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/launchpad/serializers.py b/api/launchpad/serializers.py index b000e217..3f55279f 100644 --- a/api/launchpad/serializers.py +++ b/api/launchpad/serializers.py @@ -180,12 +180,22 @@ def update(self, instance, validated_data): class UserProfileUpdateSerializer(serializers.ModelSerializer): id = serializers.CharField(max_length=36, read_only=True) + full_name = serializers.CharField(required=False, allow_blank=True, allow_null=True) + phone_number = serializers.CharField(required=False, allow_blank=True, allow_null=True) + district = serializers.CharField(required=False, allow_blank=True, allow_null=True) + zone = serializers.CharField(required=False, allow_blank=True, allow_null=True) + email = serializers.EmailField(required=False) colleges = serializers.SerializerMethodField() class Meta: model = LaunchPadUsers - fields = ("id", "full_name", "phone_number", "district", "zone", "email") + fields = ("id", "full_name", "phone_number", "district", "zone", "email", "colleges") + def validate(self, attrs): + if LaunchPadUsers.objects.filter(email=attrs.get("email")).exclude(id=self.instance.id).exists(): + raise serializers.ValidationError("Email already exists") + return super().validate(attrs) + def update(self, instance, validated_data): instance.full_name = validated_data.get("full_name", instance.full_name) instance.phone_number = validated_data.get("phone_number", instance.phone_number)