Skip to content

Commit

Permalink
Merge pull request #144 from SELab-2/api_cleanup
Browse files Browse the repository at this point in the history
Api cleanup
  • Loading branch information
EwoutV authored Mar 21, 2024
2 parents a4f52d0 + 5efd926 commit fe1328f
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 92 deletions.
11 changes: 1 addition & 10 deletions backend/api/serializers/assistant_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ class AssistantSerializer(serializers.ModelSerializer):

class Meta:
model = Assistant
fields = [
"id",
"first_name",
"last_name",
"email",
"faculties",
"last_enrolled",
"create_time",
"courses",
]
fields = "__all__"


class AssistantIDSerializer(serializers.Serializer):
Expand Down
12 changes: 3 additions & 9 deletions backend/api/serializers/checks_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ class StructureCheckSerializer(serializers.ModelSerializer):
read_only=True
)

obligated_extensions = FileExtensionSerializer(many=True, required=False, default=[], read_only=True)
obligated_extensions = FileExtensionSerializer(many=True, required=False, default=[])

blocked_extensions = FileExtensionSerializer(many=True, required=False, default=[], read_only=True)
blocked_extensions = FileExtensionSerializer(many=True, required=False, default=[])

class Meta:
model = StructureCheck
fields = [
"id",
"name",
"project",
"obligated_extensions",
"blocked_extensions"
]
fields = "__all__"


class ExtraCheckSerializer(serializers.ModelSerializer):
Expand Down
16 changes: 5 additions & 11 deletions backend/api/serializers/course_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,7 @@ class CourseSerializer(serializers.ModelSerializer):

class Meta:
model = Course
fields = [
"id",
"name",
"academic_startyear",
"description",
"parent_course",
"teachers",
"assistants",
"students",
"projects",
]
fields = "__all__"


class CourseIDSerializer(serializers.Serializer):
Expand All @@ -51,6 +41,10 @@ class CourseIDSerializer(serializers.Serializer):
)


class CourseCloneSerializer(serializers.Serializer):
clone_assistants = serializers.BooleanField()


class StudentJoinSerializer(StudentIDSerializer):
def validate(self, data):
# The validator needs the course context.
Expand Down
2 changes: 1 addition & 1 deletion backend/api/serializers/faculty_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class facultySerializer(serializers.ModelSerializer):
class Meta:
model = Faculty
fields = ["name"]
fields = "__all__"
2 changes: 1 addition & 1 deletion backend/api/serializers/group_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GroupSerializer(serializers.ModelSerializer):

class Meta:
model = Group
fields = ["id", "project", "students", "score", "submissions"]
fields = "__all__"

def to_representation(self, instance):
data = super().to_representation(instance)
Expand Down
21 changes: 1 addition & 20 deletions backend/api/serializers/project_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,7 @@ class ProjectSerializer(serializers.ModelSerializer):

class Meta:
model = Project
fields = [
"id",
"name",
"description",
"visible",
"archived",
"start_date",
"deadline",
"max_score",
"score_visible",
"group_size",
"structure_checks",
"extra_checks",
"course",
"groups",
"submissions"
]
fields = "__all__"

def validate(self, data):
if "course" in self.context:
Expand All @@ -76,9 +60,6 @@ def validate(self, data):
class TeacherCreateGroupSerializer(serializers.Serializer):
number_groups = serializers.IntegerField(min_value=1)

def validate(self, data):
return data


class SubmissionStatusSerializer(serializers.Serializer):
non_empty_groups = serializers.IntegerField(read_only=True)
Expand Down
10 changes: 1 addition & 9 deletions backend/api/serializers/submission_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ class SubmissionSerializer(serializers.ModelSerializer):

class Meta:
model = Submission
fields = [
"id",
"group",
"submission_number",
"submission_time",
"files",
"structure_checks_passed",
"extra_checks_results"
]
fields = "__all__"
extra_kwargs = {
"submission_number": {
"required": False,
Expand Down
11 changes: 1 addition & 10 deletions backend/api/serializers/teacher_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,4 @@ class TeacherSerializer(serializers.ModelSerializer):

class Meta:
model = Teacher
fields = [
"id",
"first_name",
"last_name",
"email",
"faculties",
"last_enrolled",
"create_time",
"courses",
]
fields = "__all__"
1 change: 1 addition & 0 deletions backend/api/tests/test_course.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ def test_clone_course(self):

response = self.client.post(
reverse("course-clone", args=[str(course.id)]),
data={"clone_assistants": False},
follow=True,
)

Expand Down
2 changes: 2 additions & 0 deletions backend/api/views/admin_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.permissions import IsAdminUser
from drf_yasg.utils import swagger_auto_schema
from authentication.serializers import UserSerializer, UserIDSerializer
from authentication.models import User

Expand All @@ -12,6 +13,7 @@ class AdminViewSet(ReadOnlyModelViewSet):
serializer_class = UserSerializer
permission_classes = [IsAdminUser]

@swagger_auto_schema(request_body=UserIDSerializer)
def create(self, request: Request) -> Response:
"""
Make the provided user admin by setting `is_staff` = true.
Expand Down
34 changes: 24 additions & 10 deletions backend/api/views/course_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.request import Request
from drf_yasg.utils import swagger_auto_schema
from api.models.course import Course
from api.models.group import Group
from api.permissions.course_permissions import (
Expand All @@ -12,7 +13,9 @@
CourseStudentPermission
)
from api.permissions.role_permissions import IsTeacher
from api.serializers.course_serializer import CourseSerializer, StudentJoinSerializer, StudentLeaveSerializer
from api.serializers.course_serializer import (
CourseSerializer, StudentJoinSerializer, StudentLeaveSerializer, CourseCloneSerializer
)
from api.serializers.teacher_serializer import TeacherSerializer
from api.serializers.assistant_serializer import AssistantSerializer, AssistantIDSerializer
from api.serializers.student_serializer import StudentSerializer
Expand Down Expand Up @@ -40,6 +43,7 @@ def assistants(self, request: Request, **_):

@assistants.mapping.post
@assistants.mapping.put
@swagger_auto_schema(request_body=AssistantIDSerializer)
def _add_assistant(self, request: Request, **_):
"""Add an assistant to the course"""
course = self.get_object()
Expand All @@ -59,6 +63,7 @@ def _add_assistant(self, request: Request, **_):
})

@assistants.mapping.delete
@swagger_auto_schema(request_body=AssistantIDSerializer)
def _remove_assistant(self, request: Request, **_):
"""Remove an assistant from the course"""
course = self.get_object()
Expand Down Expand Up @@ -92,6 +97,7 @@ def students(self, request, **_):

@students.mapping.post
@students.mapping.put
@swagger_auto_schema(request_body=StudentJoinSerializer)
def _add_student(self, request: Request, **_):
"""Add a student to the course"""
# Get the course
Expand All @@ -112,6 +118,7 @@ def _add_student(self, request: Request, **_):
})

@students.mapping.delete
@swagger_auto_schema(request_body=StudentLeaveSerializer)
def _remove_student(self, request: Request, **_):
"""Remove a student from the course"""
# Get the course
Expand Down Expand Up @@ -159,6 +166,7 @@ def projects(self, request, **_):

@projects.mapping.post
@projects.mapping.put
@swagger_auto_schema(request_body=ProjectSerializer)
def _add_project(self, request, **_):
"""Add a project to the course"""
course = self.get_object()
Expand Down Expand Up @@ -187,21 +195,27 @@ def _add_project(self, request, **_):
})

@action(detail=True, methods=["post"], permission_classes=[IsAdminUser | IsTeacher])
@swagger_auto_schema(request_body=CourseCloneSerializer)
def clone(self, request: Request, **__):
"""Copy the course to a new course with the same fields"""
course: Course = self.get_object()

try:
# We should return the already cloned course, if present
course = course.child_course
serializer = CourseCloneSerializer(
data=request.data
)

if serializer.is_valid(raise_exception=True):
try:
# We should return the already cloned course, if present
course = course.child_course

except Course.DoesNotExist:
# Else, we clone the course
course = course.clone(
clone_assistants=request.data.get("clone_assistants")
)
except Course.DoesNotExist:
# Else, we clone the course
course = course.clone(
clone_assistants=serializer.validated_data["clone_assistants"]
)

course.save()
course.save()

# Return serialized cloned course
course_serializer = CourseSerializer(course, context={"request": request})
Expand Down
10 changes: 5 additions & 5 deletions backend/api/views/group_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.permissions import IsAdminUser
from rest_framework.decorators import action
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema
from api.models.group import Group
from api.permissions.group_permissions import GroupPermission
from api.permissions.group_permissions import GroupStudentPermission
Expand All @@ -28,8 +29,6 @@ class GroupViewSet(CreateModelMixin,
@action(detail=True, methods=["get"], permission_classes=[IsAdminUser | GroupStudentPermission])
def students(self, request, **_):
"""Returns a list of students for the given group"""
# This automatically fetches the group from the URL.
# It automatically gives back a 404 HTTP response in case of not found.
group = self.get_object()
students = group.students.all()

Expand All @@ -41,9 +40,7 @@ def students(self, request, **_):

@action(detail=True, permission_classes=[IsAdminUser])
def submissions(self, request, **_):
"""Returns a list of students for the given group"""
# This automatically fetches the group from the URL.
# It automatically gives back a 404 HTTP response in case of not found.
"""Returns a list of submissions for the given group"""
group = self.get_object()
submissions = group.submissions.all()

Expand All @@ -55,6 +52,7 @@ def submissions(self, request, **_):

@students.mapping.post
@students.mapping.put
@swagger_auto_schema(request_body=StudentJoinGroupSerializer)
def _add_student(self, request, **_):
"""Add a student to the group"""
group = self.get_object()
Expand All @@ -74,6 +72,7 @@ def _add_student(self, request, **_):
})

@students.mapping.delete
@swagger_auto_schema(request_body=StudentLeaveGroupSerializer)
def _remove_student(self, request, **_):
"""Removes a student from the group"""
group = self.get_object()
Expand All @@ -94,6 +93,7 @@ def _remove_student(self, request, **_):

@submissions.mapping.post
@submissions.mapping.put
@swagger_auto_schema(request_body=SubmissionAddSerializer)
def _add_submission(self, request: Request, **_):
"""Add a submission to the group"""
group: Group = self.get_object()
Expand Down
7 changes: 5 additions & 2 deletions backend/api/views/project_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema
from api.permissions.project_permissions import ProjectGroupPermission, ProjectPermission
from api.models.group import Group
from api.models.submission import Submission
from api.models.project import Project
from api.models.checks import StructureCheck
from api.serializers.checks_serializer import StructureCheckSerializer, ExtraCheckSerializer
from api.serializers.project_serializer import (
StructureCheckAddSerializer, SubmissionStatusSerializer,
Expand Down Expand Up @@ -59,6 +59,8 @@ def submissions(self, request, **_):
return Response(serializer.data)

@groups.mapping.post
@groups.mapping.put
@swagger_auto_schema(request_body=TeacherCreateGroupSerializer)
def _create_groups(self, request, **_):
"""Create a number of groups for the project"""
project = self.get_object()
Expand Down Expand Up @@ -96,8 +98,9 @@ def structure_checks(self, request, **_):

@structure_checks.mapping.post
@structure_checks.mapping.put
@swagger_auto_schema(request_body=StructureCheckAddSerializer)
def _add_structure_check(self, request: Request, **_):
"""Add an structure_check to the project"""
"""Add a structure check to the project"""

project: Project = self.get_object()

Expand Down
3 changes: 0 additions & 3 deletions backend/api/views/teacher_view.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.permissions import IsAdminUser

from api.models.course import Course
from api.models.teacher import Teacher
from api.serializers.teacher_serializer import TeacherSerializer
from api.serializers.course_serializer import CourseSerializer
Expand Down
2 changes: 2 additions & 0 deletions backend/api/views/user_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class UserViewSet(ReadOnlyModelViewSet):

@action(detail=True, methods=["get"], permission_classes=[NotificationPermission])
def notifications(self, request: Request, pk: str):
"""Returns a list of notifications for the given user"""
notifications = Notification.objects.filter(user=pk)
serializer = NotificationSerializer(
notifications, many=True, context={"request": request}
Expand All @@ -33,6 +34,7 @@ def notifications(self, request: Request, pk: str):
url_path="notifications/read",
)
def read(self, request: Request, pk: str):
"""Marks all notifications as read for the given user"""
notifications = Notification.objects.filter(user=pk)
notifications.update(is_read=True)

Expand Down
Loading

0 comments on commit fe1328f

Please sign in to comment.