-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.contrib.auth import get_user_model | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework import serializers | ||
from rest_framework.exceptions import AuthenticationFailed, ValidationError | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from thunderstore.social.views import DeleteAccountForm | ||
|
||
User = get_user_model() | ||
|
||
|
||
class CyberstormUserDeleteRequestSerialiazer(serializers.Serializer): | ||
verification = serializers.CharField( | ||
max_length=User._meta.get_field("username").max_length | ||
) | ||
|
||
|
||
class CyberstormUserDeleteResponseSerialiazer(serializers.Serializer): | ||
username = serializers.CharField() | ||
|
||
|
||
class UserDeleteAPIView(APIView): | ||
@swagger_auto_schema( | ||
request_body=CyberstormUserDeleteRequestSerialiazer, | ||
responses={200: CyberstormUserDeleteResponseSerialiazer}, | ||
operation_id="cyberstorm.user.delete", | ||
tags=["cyberstorm"], | ||
) | ||
def post(self, request, username, format=None): | ||
serializer = CyberstormUserDeleteRequestSerialiazer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
if request.user.username != username: | ||
raise AuthenticationFailed("You can only delete your own account") | ||
form = DeleteAccountForm( | ||
user=request.user, | ||
data=request.data, | ||
) | ||
if form.is_valid(): | ||
request.user.delete() | ||
return Response( | ||
CyberstormUserDeleteResponseSerialiazer( | ||
{"username": request.user.username} | ||
).data | ||
) | ||
else: | ||
raise ValidationError(form.errors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters