From c8854461d5948e33462930b598f76d6a0e6c10f5 Mon Sep 17 00:00:00 2001 From: Uttkarsh-raj <106571927+Uttkarsh-raj@users.noreply.github.com> Date: Mon, 29 Jul 2024 21:17:49 +0530 Subject: [PATCH] added the login , logout and delete apis (#2493) --- blt/urls.py | 2 ++ website/api/views.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/blt/urls.py b/blt/urls.py index 1585888c8..03aa0207d 100644 --- a/blt/urls.py +++ b/blt/urls.py @@ -18,6 +18,7 @@ from blt import settings from company.views import ShowBughuntView from website.api.views import ( + AuthApiViewset, BugHuntApiViewset, BugHuntApiViewsetV2, CompanyViewSet, @@ -549,6 +550,7 @@ ProjectViewSet.as_view({"get": "list", "post": "create", "patch": "update"}), name="projects_api", ), + path("auth/delete", AuthApiViewset.as_view({"delete": "delete"}), name="auth-delete-api"), ] if settings.DEBUG: diff --git a/website/api/views.py b/website/api/views.py index 8b934639c..4006ad485 100644 --- a/website/api/views.py +++ b/website/api/views.py @@ -26,6 +26,7 @@ IssueScreenshot, Points, Project, + Token, User, UserProfile, ) @@ -718,3 +719,20 @@ def list(self, request, *args, **kwargs): {"count": len(project_data), "projects": project_data}, status=200, ) + + +class AuthApiViewset(viewsets.ModelViewSet): + http_method_names = ("delete",) + permission_classes = (IsAuthenticated,) + + def delete(self, request, *args, **kwargs): + try: + token = request.headers["Authorization"].split(" ") + user = Token.objects.get(key=token[1]).user + user_data = User.objects.get(username=user) + user_data.delete() + return Response({"success": True, "message": "User deleted successfully !!"}) + except Token.DoesNotExist: + return Response({"success": False, "message": "User does not exists."}) + except User.DoesNotExist: + return Response({"success": False, "message": "User does not exists."})