Skip to content

Commit

Permalink
Merge pull request #2128 from jelanmathewjames/dev
Browse files Browse the repository at this point in the history
refactor: Update UserProfileUpdateSerializer to validate email unique…
  • Loading branch information
jelanmathewjames authored Jul 3, 2024
2 parents 002b2e8 + b546757 commit 3b609e4
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion api/launchpad/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3b609e4

Please sign in to comment.