Skip to content

Commit

Permalink
Add UserDeleteView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 13, 2023
1 parent f2a2960 commit 7c4a8e5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
37 changes: 37 additions & 0 deletions django/thunderstore/api/cyberstorm/views/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.http import HttpRequest
from rest_framework import serializers
from rest_framework.exceptions import 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


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


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


class UserDeleteAPIView(APIView):
@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(

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

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/user.py#L27-L29

Added lines #L27 - L29 were not covered by tests
user=request.user,
data=serializer.validated_data,
)
if form.is_valid():
form.delete_user()
return Response()

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
else:
raise ValidationError(form.errors)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L37 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 @@ -16,6 +16,7 @@
TeamMembersAPIView,
TeamServiceAccountsAPIView,
)
from thunderstore.api.cyberstorm.views.user import UserDeleteAPIView

cyberstorm_urls = [
path(
Expand Down Expand Up @@ -101,4 +102,9 @@
PackageVersionsAPIView.as_view(),
name="cyberstorm.package.versions",
),
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()

Check warning on line 53 in django/thunderstore/social/views.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/social/views.py#L53

Added line #L53 was not covered by tests


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()

Check warning on line 72 in django/thunderstore/social/views.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/social/views.py#L72

Added line #L72 was not covered by tests
return super().form_valid(form)

0 comments on commit 7c4a8e5

Please sign in to comment.