Skip to content

Commit

Permalink
Revert "Admin create action #88"
Browse files Browse the repository at this point in the history
This reverts commit dcd4863.
  • Loading branch information
DeLany123 committed Mar 10, 2024
1 parent dcd4863 commit b8d7710
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 40 deletions.
Empty file.
22 changes: 2 additions & 20 deletions backend/api/views/admin_view.py
Original file line number Diff line number Diff line change
@@ -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")
})
26 changes: 16 additions & 10 deletions backend/api/views/teacher_view.py
Original file line number Diff line number Diff line change
@@ -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)
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"}
)
5 changes: 1 addition & 4 deletions backend/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -49,6 +45,7 @@ def get_dummy_admin():
is_staff=True
)


class Faculty(models.Model):
"""This model represents a faculty."""

Expand Down
6 changes: 0 additions & 6 deletions backend/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
ModelSerializer,
Serializer,
ValidationError,
PrimaryKeyRelatedField
)
from rest_framework_simplejwt.settings import api_settings
from rest_framework_simplejwt.tokens import AccessToken, RefreshToken
Expand Down Expand Up @@ -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()
)

0 comments on commit b8d7710

Please sign in to comment.