-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
40 additions
and
19 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters