Skip to content

Commit

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


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

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

0 comments on commit dcd4863

Please sign in to comment.