diff --git a/backend/api/serializers/project_serializer.py b/backend/api/serializers/project_serializer.py index 10811f0b..44c2468c 100644 --- a/backend/api/serializers/project_serializer.py +++ b/backend/api/serializers/project_serializer.py @@ -4,7 +4,6 @@ from api.models.group import Group from rest_framework.exceptions import ValidationError from django.utils import timezone -from api.models.course import Course from api.models.submission import Submission, SubmissionFile from api.serializers.submission_serializer import SubmissionSerializer @@ -57,7 +56,7 @@ def validate(self, data): raise ValidationError(gettext("project.errors.context")) # Check if start date of the project is not in the past - if data["start_date"] < timezone.now(): + if data["start_date"] < timezone.now().replace(hour=0, minute=0, second=0): raise ValidationError(gettext("project.errors.start_date_in_past")) # Check if deadline of the project is before the start date diff --git a/backend/api/tests/test_project.py b/backend/api/tests/test_project.py index c8a50c82..31c38110 100644 --- a/backend/api/tests/test_project.py +++ b/backend/api/tests/test_project.py @@ -150,6 +150,54 @@ def test_toggle_locked_groups(self): past_project.toggle_groups_locked() self.assertIs(past_project.locked_groups, False) + def test_automatically_create_groups_when_creating_project(self): + """ + creating a project as a teacher should open the same amount of groups as students enrolled in the project. + """ + course = create_course(id=3, name="test course", academic_startyear=2024) + + student1 = create_student( + id=1, first_name="John", last_name="Doe", email="john.doe@example.com" + ) + student2 = create_student( + id=2, first_name="Jane", last_name="Doe", email="jane.doe@example.com" + ) + student1.courses.add(course) + student2.courses.add(course) + + project_data = { + "name": "Test Project", + "description": "Test project description", + "visible": True, + "archived": False, + "start_date": timezone.now(), + "deadline": timezone.now() + timezone.timedelta(days=1), + } + + response = self.client.post( + reverse("course-projects", args=[course.id]), data=project_data, follow=True + ) + + # Creating a group as a teacher should work + self.assertEqual(response.status_code, 200) + + project = Project.objects.get( + name="Test Project", + description="Test project description", + visible=True, + archived=False, + start_date=project_data["start_date"], + deadline=project_data["deadline"], + course=course, + ) + + groups_count = project.groups.count() + # The amount of students participating in the corresponding course + expected_groups_count = 2 + + # We expect the amount of groups to be the same as the amount of students in the course + self.assertEqual(groups_count, expected_groups_count) + def test_start_date_Project_not_in_past(self): """ unable to create a project as a teacher/admin if the start date lies within the past. diff --git a/backend/api/views/course_view.py b/backend/api/views/course_view.py index 8e7b9ac4..6aac3096 100644 --- a/backend/api/views/course_view.py +++ b/backend/api/views/course_view.py @@ -5,6 +5,7 @@ from rest_framework.response import Response from rest_framework.request import Request from api.models.course import Course +from api.models.group import Group from api.permissions.course_permissions import ( CoursePermission, CourseAssistantPermission, @@ -169,11 +170,17 @@ def _add_project(self, request, **_): } ) + project = None + # Validate the serializer if serializer.is_valid(raise_exception=True): - course.projects.add( - serializer.save() - ) + project = serializer.save() + course.projects.add(project) + + # Create groups for the project + students_count = course.students.count() + for _ in range(students_count): + Group.objects.create(project=project) return Response({ "message": gettext("course.success.project.add"),