diff --git a/backend/api/permissions/teacher_permissions.py b/backend/api/permissions/teacher_permissions.py deleted file mode 100644 index e69de29b..00000000 diff --git a/backend/api/views/admin_view.py b/backend/api/views/admin_view.py index 34ee3b99..63fdab43 100644 --- a/backend/api/views/admin_view.py +++ b/backend/api/views/admin_view.py @@ -1,26 +1,8 @@ -from django.utils.translation import gettext from rest_framework import viewsets -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.serializers import UserSerializer from authentication.models import User -class AdminViewSet(ReadOnlyModelViewSet): +class AdminViewSet(viewsets.ModelViewSet): 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 89cf3a3a..49038133 100644 --- a/backend/api/views/teacher_view.py +++ b/backend/api/views/teacher_view.py @@ -1,25 +1,31 @@ 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(ReadOnlyModelViewSet): +class TeacherViewSet(viewsets.ModelViewSet): 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() - # Serialize the course objects - serializer = CourseSerializer( - courses, many=True, context={"request": request} - ) - return Response(serializer.data) \ No newline at end of file + 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"} + ) diff --git a/backend/authentication/models.py b/backend/authentication/models.py index 058eb38c..066d6fbb 100644 --- a/backend/authentication/models.py +++ b/backend/authentication/models.py @@ -33,10 +33,6 @@ 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(): @@ -49,6 +45,7 @@ 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 0db21efb..7c4f1d92 100644 --- a/backend/authentication/serializers.py +++ b/backend/authentication/serializers.py @@ -12,7 +12,6 @@ ModelSerializer, Serializer, ValidationError, - PrimaryKeyRelatedField ) from rest_framework_simplejwt.settings import api_settings from rest_framework_simplejwt.tokens import AccessToken, RefreshToken @@ -108,8 +107,3 @@ 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() - )