Skip to content

Commit

Permalink
Mooved coordinator check before student check, made delete section pr…
Browse files Browse the repository at this point in the history
…ocess atomic
  • Loading branch information
KartavyaSharma committed Oct 10, 2023
1 parent 410e740 commit c8b1998
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions csm_web/scheduler/views/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,8 @@ 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)
section = get_object_or_error(Course.objects.all(), pk=pk)
course = section.mentor.course
# If the course has students, we cannot delete the section
if section.students.count() > 0:
logger.error(
(
"<Section Deletion:Failure> Could not delete section %s, it has"
" students. Remove all students manually first."
),
log_str(section),
)
is_coordinator = course.coordinator_set.filter(user=request.user).exists()
if not is_coordinator:
logger.error(
Expand All @@ -147,13 +138,24 @@ def destroy(self, request, pk=None):
raise PermissionDenied(
"You must be a coordinator to delete this spacetime!"
)
# If the course has students, we cannot delete the section
if section.students.count() > 0:
logger.error(
(
"<Section Deletion:Failure> Could not delete section %s, it has"
" students. Remove all students manually first."
),
log_str(section),
)
return PermissionDenied("Cannot delete section with students")
# Delete all spacetimes in the section
for spacetime in section.spacetimes.all():
spacetime.delete()
# Delete the mentor
section.mentor.delete()
with transaction.atomic():
for spacetime in section.spacetimes.all():
spacetime.delete()
# Delete the mentor
section.mentor.delete()

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

def partial_update(self, request, pk=None):
Expand Down

0 comments on commit c8b1998

Please sign in to comment.