-
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.
This reverts commit dcd4863.
- Loading branch information
Showing
5 changed files
with
19 additions
and
40 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,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") | ||
}) |
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,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"} | ||
) |
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