diff --git a/profiles/serializers.py b/profiles/serializers.py index 74f5f1b1f..25978cae8 100644 --- a/profiles/serializers.py +++ b/profiles/serializers.py @@ -204,13 +204,18 @@ class Meta: ) read_only_fields = ("person",) - def validate_for_delete(self, data): + +class ProfileDeleteSerializer(serializers.Serializer): + password = serializers.CharField(write_only=True, required=True) + + # class Meta: + # fields = ("password",) + + def validate(self, data): password = data.get("password") - if not password: - raise serializers.ValidationError("Password is required") user = self.context["request"].user if not user.check_password(password): - raise serializers.ValidationError("Invalid password") + raise serializers.ValidationError({"password": "Invalid password"}) return data diff --git a/profiles/views.py b/profiles/views.py index 2ed3cf8d2..27bbb8487 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -33,6 +33,7 @@ ProfileDetailSerializer, ProfileOwnerDetailViewSerializer, ProfileOwnerDetailEditSerializer, + ProfileDeleteSerializer, CategorySerializer, ActivitySerializer, RegionSerializer, @@ -171,13 +172,14 @@ def get_serializer_class(self): if get_contacts else ProfileDetailSerializer ) + elif self.request.method == "DELETE": + return ProfileDeleteSerializer else: return ProfileOwnerDetailEditSerializer def perform_destroy(self, instance): - request_data = {"password": self.request.data.get("password")} - serializer = self.get_serializer(instance) - if serializer.validate_for_delete(request_data): + serializer = self.get_serializer(data=self.request.data) + if serializer.is_valid(raise_exception=True): instance.is_deleted = True instance.save()