Add delete user endpoint

This commit is contained in:
Keannu Bernasol 2025-01-16 19:54:49 +08:00
parent d227535383
commit 008a2a2271
3 changed files with 27 additions and 1 deletions

View file

@ -29,3 +29,12 @@ class IsHead(BasePermission):
def has_permission(self, request, view):
return bool(request.user and request.user.role == "head")
class IsAdmin(BasePermission):
"""
Allows access only to users with admin role
"""
def has_permission(self, request, view):
return bool(request.user and request.user.role == "admin")

View file

@ -1,7 +1,8 @@
from django.urls import include, path
from .views import CustomUserDeleteView
urlpatterns = [
path("", include("djoser.urls")),
path("", include("djoser.urls.jwt")),
path("users/delete/<int:pk>/", CustomUserDeleteView.as_view()),
]

View file

@ -0,0 +1,16 @@
from rest_framework import generics
from .serializers import CustomUserSerializer
from rest_framework.permissions import IsAuthenticated
from accounts.permissions import IsAdmin
from .models import CustomUser
class CustomUserDeleteView(generics.DestroyAPIView):
"""
Used by admin to delete users. Accepts the user id as a URL parameter
"""
http_method_names = ["delete"]
serializer_class = CustomUserSerializer
queryset = CustomUser.objects.all()
permission_classes = [IsAuthenticated, IsAdmin]