Skip to content

Commit

Permalink
Added destroy method to section view and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KartavyaSharma committed Sep 13, 2023
1 parent ad5844a commit d223b4f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
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
Expand Up @@ -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)
Expand Down

0 comments on commit d223b4f

Please sign in to comment.