Skip to content

Commit

Permalink
Add UserDeleteView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Jan 3, 2024
1 parent 463470a commit 306f045
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
40 changes: 40 additions & 0 deletions django/thunderstore/api/cyberstorm/views/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.http import HttpRequest
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated
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


class CyberstormUserDeleteRequestSerialiazer(serializers.Serializer):
verification = serializers.CharField()


class CyberstormUserDeleteResponseSerialiazer(serializers.Serializer):
username = serializers.CharField()


class UserDeleteAPIView(APIView):
permission_classes = [IsAuthenticated]

@conditional_swagger_auto_schema(
request_body=CyberstormUserDeleteRequestSerialiazer,
responses={200: CyberstormUserDeleteResponseSerialiazer},
operation_id="cyberstorm.current-user.delete",
tags=["cyberstorm"],
)
def post(self, request: HttpRequest):
serializer = CyberstormUserDeleteRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)
form = DeleteAccountForm(
user=request.user,
data=serializer.validated_data,
)
if form.is_valid():
form.delete_user()
return Response()
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 @@ -16,6 +16,7 @@
TeamMemberListAPIView,
TeamServiceAccountListAPIView,
)
from thunderstore.api.cyberstorm.views.user import UserDeleteAPIView

cyberstorm_urls = [
path(
Expand Down Expand Up @@ -98,4 +99,9 @@
TeamServiceAccountListAPIView.as_view(),
name="cyberstorm.team.service-account",
),
path(
"current-user/delete/",
UserDeleteAPIView.as_view(),
name="cyberstorm.current-user.delete",
),
]
5 changes: 4 additions & 1 deletion django/thunderstore/social/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def clean_verification(self):
raise forms.ValidationError("Invalid verification")
return data

def delete_user(self):
self.user.delete()


class DeleteAccountView(SettingsViewMixin, RequireAuthenticationMixin, FormView):
template_name = "settings/delete_account.html"
Expand All @@ -66,5 +69,5 @@ def get_form_kwargs(self, *args, **kwargs):
return kwargs

def form_valid(self, form):
self.request.user.delete()
form.delete_user()
return super().form_valid(form)

0 comments on commit 306f045

Please sign in to comment.