Skip to content

Commit

Permalink
Add UserDeleteView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 12, 2023
1 parent 31405a1 commit 73f4b22
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 rest_framework import serializers
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.response import Response
from rest_framework.views import APIView

from thunderstore.api.utils import conditional_swagger_auto_schema
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):
@conditional_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)

Check warning on line 32 in django/thunderstore/api/cyberstorm/views/user.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/user.py#L31-L32

Added lines #L31 - L32 were not covered by tests
if request.user.username != username:
raise PermissionDenied("You can only delete your own account")
form = DeleteAccountForm(

Check warning on line 35 in django/thunderstore/api/cyberstorm/views/user.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/user.py#L34-L35

Added lines #L34 - L35 were not covered by tests
user=request.user,
data=request.data,
)
if form.is_valid():
request.user.delete()
return Response(

Check warning on line 41 in django/thunderstore/api/cyberstorm/views/user.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/user.py#L40-L41

Added lines #L40 - L41 were not covered by tests
CyberstormUserDeleteResponseSerialiazer(
{"username": request.user.username}
).data
)
else:
raise ValidationError(form.errors)

Check warning on line 47 in django/thunderstore/api/cyberstorm/views/user.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/user.py#L47

Added line #L47 was not covered by tests
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 73f4b22

Please sign in to comment.