-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#260 add check password when delete profile #261
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import stat | ||
import django_filters | ||
from django.shortcuts import get_object_or_404 | ||
from django.contrib.auth.hashers import check_password | ||
from rest_framework import status | ||
from rest_framework.generics import ( | ||
CreateAPIView, | ||
|
@@ -174,6 +176,16 @@ def get_serializer_class(self): | |
else: | ||
return ProfileOwnerDetailEditSerializer | ||
|
||
def destroy(self, request, *args, **kwargs): | ||
instance = self.get_object() | ||
user = self.request.user | ||
password = self.request.data.get("password") | ||
if not password or not check_password(password, user.password): | ||
return Response(status=status.HTTP_400_BAD_REQUEST) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to do it in serializer, but there is no delete method in serializers, so I don't know how to implement it. I remember that it's not okay to return Response. But I just override original destroy method and it contains Response. |
||
else: | ||
self.perform_destroy(instance) | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
def perform_destroy(self, instance): | ||
instance.is_deleted = True | ||
instance.save() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to use
get_object_or_404
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No.
get_object_or_404()
may be used inget_object()
(if needed), but not here.