Skip to content

Commit

Permalink
password check updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Lvyshnevska committed Oct 12, 2023
1 parent 362ada3 commit 19c3e84
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 9 additions & 4 deletions profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 5 additions & 3 deletions profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ProfileDetailSerializer,
ProfileOwnerDetailViewSerializer,
ProfileOwnerDetailEditSerializer,
ProfileDeleteSerializer,
CategorySerializer,
ActivitySerializer,
RegionSerializer,
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 19c3e84

Please sign in to comment.