Skip to content

Commit

Permalink
Add UserDeleteView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 11, 2023
1 parent 31405a1 commit eac5259
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
47 changes: 47 additions & 0 deletions django/thunderstore/api/cyberstorm/views/user.py
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)
6 changes: 6 additions & 0 deletions django/thunderstore/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TeamMembersAPIView,
TeamServiceAccountsAPIView,
)
from thunderstore.api.cyberstorm.views.user import UserDeleteAPIView

cyberstorm_urls = [
path(
Expand Down Expand Up @@ -58,4 +59,9 @@
TeamServiceAccountsAPIView.as_view(),
name="cyberstorm.team.service-accounts",
),
path(
"user/<str:username>/delete/",
UserDeleteAPIView.as_view(),
name="cyberstorm.user.delete",
),
]

0 comments on commit eac5259

Please sign in to comment.