From dcd486375996014fe5683623720c2ddf2e60116f Mon Sep 17 00:00:00 2001 From: Lander Maes Date: Sun, 10 Mar 2024 20:08:17 +0100 Subject: [PATCH] Admin create action #88 --- .../api/permissions/teacher_permissions.py | 0 backend/api/views/admin_view.py | 22 ++++++++++++++-- backend/api/views/teacher_view.py | 26 +++++++------------ backend/authentication/models.py | 5 +++- backend/authentication/serializers.py | 6 +++++ 5 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 backend/api/permissions/teacher_permissions.py diff --git a/backend/api/permissions/teacher_permissions.py b/backend/api/permissions/teacher_permissions.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/api/views/admin_view.py b/backend/api/views/admin_view.py index 63fdab43..34ee3b99 100644 --- a/backend/api/views/admin_view.py +++ b/backend/api/views/admin_view.py @@ -1,8 +1,26 @@ +from django.utils.translation import gettext from rest_framework import viewsets -from authentication.serializers import UserSerializer +from rest_framework.viewsets import ReadOnlyModelViewSet +from rest_framework.permissions import IsAdminUser +from rest_framework.response import Response + +from authentication.serializers import UserSerializer, UserIDSerializer from authentication.models import User -class AdminViewSet(viewsets.ModelViewSet): +class AdminViewSet(ReadOnlyModelViewSet): queryset = User.objects.filter(is_staff=True) serializer_class = UserSerializer + permission_classes = [IsAdminUser] + + def create(self, request): + # Add an Admin + serializer = UserIDSerializer(data=request.data) + + if serializer.is_valid(raise_exception=True): + user = User.objects.get(serializer.validated_data["user_id"]) + user.make_admin() + + return Response({ + "message": gettext("admins.success.add") + }) \ No newline at end of file diff --git a/backend/api/views/teacher_view.py b/backend/api/views/teacher_view.py index 49038133..89cf3a3a 100644 --- a/backend/api/views/teacher_view.py +++ b/backend/api/views/teacher_view.py @@ -1,31 +1,25 @@ from rest_framework import viewsets, status from rest_framework.decorators import action from rest_framework.response import Response +from rest_framework.viewsets import ReadOnlyModelViewSet from ..models.teacher import Teacher from ..serializers.teacher_serializer import TeacherSerializer from ..serializers.course_serializer import CourseSerializer -class TeacherViewSet(viewsets.ModelViewSet): +class TeacherViewSet(ReadOnlyModelViewSet): queryset = Teacher.objects.all() serializer_class = TeacherSerializer + permission_classes = [] @action(detail=True, methods=["get"]) def courses(self, request, pk=None): """Returns a list of courses for the given teacher""" + teacher = self.get_object() + courses = teacher.courses.all() - try: - queryset = Teacher.objects.get(id=pk) - courses = queryset.courses.all() - - # Serialize the course objects - serializer = CourseSerializer( - courses, many=True, context={"request": request} - ) - return Response(serializer.data) - - except Teacher.DoesNotExist: - # Invalid teacher ID - return Response( - status=status.HTTP_404_NOT_FOUND, data={"message": "Teacher not found"} - ) + # Serialize the course objects + serializer = CourseSerializer( + courses, many=True, context={"request": request} + ) + return Response(serializer.data) \ No newline at end of file diff --git a/backend/authentication/models.py b/backend/authentication/models.py index 066d6fbb..058eb38c 100644 --- a/backend/authentication/models.py +++ b/backend/authentication/models.py @@ -33,6 +33,10 @@ class User(AbstractBaseUser): """Model settings""" USERNAME_FIELD = "username" EMAIL_FIELD = "email" + + def make_admin(self): + self.is_staff = True + self.save() @staticmethod def get_dummy_admin(): @@ -45,7 +49,6 @@ def get_dummy_admin(): is_staff=True ) - class Faculty(models.Model): """This model represents a faculty.""" diff --git a/backend/authentication/serializers.py b/backend/authentication/serializers.py index 7c4f1d92..0db21efb 100644 --- a/backend/authentication/serializers.py +++ b/backend/authentication/serializers.py @@ -12,6 +12,7 @@ ModelSerializer, Serializer, ValidationError, + PrimaryKeyRelatedField ) from rest_framework_simplejwt.settings import api_settings from rest_framework_simplejwt.tokens import AccessToken, RefreshToken @@ -107,3 +108,8 @@ class Meta: def get_or_create(self, validated_data: dict) -> Tuple[User, bool]: """Create or fetch the user based on the validated data.""" return User.objects.get_or_create(**validated_data) + +class UserIDSerializer(Serializer): + user_id = PrimaryKeyRelatedField( + queryset=User.objects.all() + )