Skip to content

Commit

Permalink
chore: automatically create groups when creating project
Browse files Browse the repository at this point in the history
  • Loading branch information
francisvaut committed Mar 13, 2024
1 parent bb9029c commit 892aeea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
3 changes: 1 addition & 2 deletions backend/api/serializers/project_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions backend/api/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]"
)
student2 = create_student(
id=2, first_name="Jane", last_name="Doe", email="[email protected]"
)
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.
Expand Down
13 changes: 10 additions & 3 deletions backend/api/views/course_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 892aeea

Please sign in to comment.