Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete button to allow coordinators to drop sections #443

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Next Next commit
Added destroy method to section view and added tests
KartavyaSharma committed Sep 13, 2023
commit d223b4fb1206b794ecaaf273b555476e40c9ebc1
68 changes: 68 additions & 0 deletions csm_web/scheduler/tests/models/test_section.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest
from django.urls import reverse
from scheduler.factories import (
CourseFactory,
MentorFactory,
SectionFactory,
StudentFactory,
UserFactory,
)
from scheduler.models import Section


@pytest.fixture
def setup_test_scheduler():
"""
Create a course, coordinator, mentor, and a student for testing.
"""
# Setup course
course = CourseFactory.create()
# Setup coordinator for course
coordinator_user = UserFactory.create()

return (
course,
coordinator_user,
)


@pytest.mark.django_db
def test_section_delete(client, setup_scheduler):
"""
Test that a section can be deleted.
"""
(
section_one,
coordinator_user,
) = setup_scheduler
# Login as coordinator
client.force_login(coordinator_user)
# Delete section
response = client.delete(reverse("section-detail", kwargs={"pk": section_one.id}))
# Check that section was deleted
assert response.status_code == 204
assert Section.objects.count() == 1


def create_students(course, section, quantity):
"""
Creates a given number of students for a given section.
"""
student_users = UserFactory.create_batch(quantity)
students = []
for student_user in student_users:
student = StudentFactory.create(
user=student_user, course=course, section=section
)
students.append(student)
return students


def create_section(course):
"""
Creates a section for a given course.
"""
mentor_user = UserFactory.create()
mentor = MentorFactory.create(user=mentor_user, course=course)
section = SectionFactory.create(mentor=mentor)
return section
22 changes: 22 additions & 0 deletions csm_web/scheduler/views/section.py
Original file line number Diff line number Diff line change
@@ -119,6 +119,28 @@ def create(self, request):
serializer = self.serializer_class(section)
return Response(serializer.data, status=status.HTTP_201_CREATED)

def destroy(self, request, pk=None):
"""
Handle request to delete section through the UI;
deletes mentor and spacetimes along with it
"""
section = get_object_or_error(self.get_queryset(), pk=pk)
if not section.mentor.course.coordinator_set.filter(
user=self.request.user
).count():
raise PermissionDenied("Only coordinators can delete section")
# Delete all students in the section
for student in section.students.all():
student.delete()
# Delete all spacetimes in the section
for spacetime in section.spacetimes.all():
spacetime.delete()
# Delete the mentor
section.mentor.delete()

section.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

def partial_update(self, request, pk=None):
"""Update section metadata (capacity and description)"""
section = get_object_or_error(self.get_queryset(), pk=pk)