Skip to content

Commit

Permalink
modified attendence query so only untaken attendences are deleted whe…
Browse files Browse the repository at this point in the history
…n students are dropped

pre-commit fixes

fix merge conflict
  • Loading branch information
Veriny committed Sep 19, 2023
1 parent 0af4cb6 commit 7b14e71
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions csm_web/scheduler/views/student.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from django.utils import timezone
from scheduler.models import Attendance, SectionOccurrence
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response
import datetime

from .utils import log_str, logger, get_object_or_error
from ..models import Student
from ..serializers import AttendanceSerializer, StudentSerializer
from .utils import get_object_or_error, log_str, logger


class StudentViewSet(viewsets.GenericViewSet):
Expand All @@ -28,6 +26,10 @@ def get_queryset(self):

@action(detail=True, methods=["patch"])
def drop(self, request, pk=None):
"""
Drops a student form a course.
PATCH: Drop a given student. Check for student ban if coordinator made request
"""
student = get_object_or_error(self.get_queryset(), pk=pk)
is_coordinator = student.course.coordinator_set.filter(
user=request.user
Expand All @@ -43,23 +45,36 @@ def drop(self, request, pk=None):
student.course.whitelist.remove(student.user)
student.save()
logger.info(
f"<Drop> User {log_str(request.user)} dropped Section {log_str(student.section)} for Student user {log_str(student.user)}"
"<Drop> User %s dropped section %s for Student %s.",
log_str(request.user),
log_str(student.section),
log_str(student.user),
)
# filter attendances and delete future attendances
now = timezone.now().astimezone(timezone.get_default_timezone())
num_deleted, _ = student.attendance_set.filter(
Q(
sectionOccurrence__date__gte=now.date(),
sectionOccurrence__section=student.section,
presence="",
)
).delete()
logger.info(
f"<Drop> Deleted {num_deleted} attendances for user {log_str(student.user)} in Section {log_str(student.section)} after {now.date()}"
"<Drop> Deleted %s attendances for user %s in Section %s after %s",
num_deleted,
log_str(student.user),
log_str(student.section),
now.date(),
)
return Response(status=status.HTTP_204_NO_CONTENT)

@action(detail=True, methods=["get", "put"])
def attendances(self, request, pk=None):
"""
Method for updating attendances.
GET: Gets the attendances for a student
PUT: Updates the attendances for a student
"""
student = get_object_or_error(self.get_queryset(), pk=pk)
if request.method == "GET":
return Response(
Expand All @@ -81,18 +96,26 @@ def attendances(self, request, pk=None):
)
except ObjectDoesNotExist:
logger.error(
f"<Attendance:Failure> Could not record attendance for User {log_str(request.user)}, used non-existent attendance id {request.data['id']}"
(
"<Attendance:Failure> Could not record attendance for User %s, used"
" non-existent attendance ID %s"
),
log_str(request.user),
request.data["id"],
)
return Response(status=status.HTTP_400_BAD_REQUEST)

if serializer.is_valid():
attendance = serializer.save()
logger.info(
f"<Attendance:Success> Attendance {log_str(attendance)} recorded for User {log_str(request.user)}"
"<Attendance:Success> Attendance %s recorded for User %s",
log_str(attendance),
log_str(request.user),
)
return Response(status=status.HTTP_204_NO_CONTENT)
logger.error(
f"<Attendance:Failure> Could not record attendance for User {log_str(request.user)}, errors: {serializer.errors}"
"<Attendance:Failure> Could not record attendance for User %s, errors: %s",
log_str(request.user),
serializer.errors,
)
return Response(serializer.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY)

0 comments on commit 7b14e71

Please sign in to comment.