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

Update attendances without using Serializers #393

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions csm_web/scheduler/views/student.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models import Q
from django.utils import timezone
from scheduler.models import Attendance, SectionOccurrence
Expand Down Expand Up @@ -70,29 +70,21 @@ def attendances(self, request, pk=None):
raise PermissionDenied("You cannot record your own attendance (nice try)")
try: # update
attendance = student.attendance_set.get(pk=request.data["id"])
serializer = AttendanceSerializer(
attendance,
data={
"student_id": student.id,
"student_name": student.name,
"student_email": student.user.email,
"presence": request.data["presence"],
},
)
presence = request.data["presence"]
if presence not in ["PR", "UN", "EX"]:
logger.error(
f"<Attendance:Failure> Could not record attendance for User {log_str(student)}, submitted invalid presence.")
return Response(status=status.HTTP_400_BAD_REQUEST)
attendance.presence = presence
try:
attendance.save()
logger.info(
f"<Attendance:Success> Attendance {log_str(attendance)} recorded for User {log_str(request.user)}")
except ValidationError:
return Response(status=status.HTTP_422_UNPROCESSABLE_ENTITY)
return Response(status=status.HTTP_204_NO_CONTENT)
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']}"
f"<Attendance:Failure> Could not record attendance for User {log_str(student)}, used non-existent attendance id {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)}"
)
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}"
)
return Response(serializer.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY)