Skip to content

Commit

Permalink
chore: course serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
francisvaut committed Mar 10, 2024
1 parent 3abdecb commit c14424e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
9 changes: 9 additions & 0 deletions backend/api/serializers/project_serializer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from rest_framework.exceptions import ValidationError
from django.utils.translation import gettext
from rest_framework import serializers
from ..models.project import Project

Expand Down Expand Up @@ -40,6 +42,13 @@ class Meta:
"groups"
]

def validate(self, data):
print("*** Project validation goes here ***")
return data


class TeacherCreateGroupSerializer(serializers.Serializer):
number_groups = serializers.IntegerField(min_value=1)

def validate(self, data):
return data
27 changes: 26 additions & 1 deletion backend/api/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ def test_toggle_archived(self):
past_project.toggle_archived()
self.assertIs(past_project.archived, True)

def test_deadline_of_Project_in_past_on_creation(self):
"""
unable to create a project as a teacher/admin if the deadline lies within the past.
"""
course = create_course(id=3, name="test course", academic_startyear=2024)
past_deadline = timezone.now() - timezone.timedelta(days=1)

project_data = {
"name": "Test Project",
"description": "Test project description",
"visible": True,
"archived": False,
"start_date": timezone.now(),
"deadline": past_deadline,
"course": course,
}

response = self.client.post(
reverse("course-projects", args=[course.id]),
data=project_data,
follow=True
)

print(f"Response: {response}")

def test_deadline_approaching_in_with_past_Project(self):
"""
deadline_approaching_in() returns False for Projects whose Deadline
Expand Down Expand Up @@ -351,7 +376,7 @@ def test_project_structure_checks(self):

def test_project_extra_checks(self):
"""
Able to retrieve a extra check of a project after creating it.
Able to retrieve an extra check of a project after creating it.
"""
course = create_course(id=3, name="test course", academic_startyear=2024)
project = create_project(
Expand Down
20 changes: 20 additions & 0 deletions backend/api/views/course_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ def projects(self, request, **_):

return Response(serializer.data)

@projects.mapping.post
@projects.mapping.put
def _add_project(self, request, **_):
"""Add a project to the course"""
course = self.get_object()

serializer = ProjectSerializer(
data=request.data, context={}
)

# Validate the serializer
if serializer.is_valid(raise_exception=True):
course.projects.add(
serializer.validated_data["project_id"]
)

return Response({
"message": gettext("course.success.project.add"),
})

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

0 comments on commit c14424e

Please sign in to comment.