diff --git a/csm_web/scheduler/tests/models/test_attendance.py b/csm_web/scheduler/tests/models/test_attendance.py index 2275c315..00b71418 100644 --- a/csm_web/scheduler/tests/models/test_attendance.py +++ b/csm_web/scheduler/tests/models/test_attendance.py @@ -12,6 +12,7 @@ Course, Section, Mentor, + Coordinator, Student, Attendance, SectionOccurrence, @@ -21,6 +22,7 @@ CourseFactory, SectionFactory, MentorFactory, + CoordinatorFactory, StudentFactory, AttendanceFactory, SpacetimeFactory, @@ -33,6 +35,9 @@ @pytest.fixture def setup_section(db): + coordinator_user = UserFactory( + username="coordinator_user", first_name="coordinator", last_name="user" + ) mentor_user = UserFactory( username="mentor_user", first_name="mentor", last_name="user" ) @@ -56,6 +61,7 @@ def setup_section(db): ), ) mentor = MentorFactory(user=mentor_user, course=course) + coord = CoordinatorFactory(user=coordinator_user, course=course) section = SectionFactory( mentor=mentor, capacity=6, @@ -74,7 +80,7 @@ def setup_section(db): ), ], ) - return mentor, student_user, course, section + return mentor, student_user, course, section, coord @pytest.mark.django_db @@ -115,7 +121,7 @@ def setup_section(db): def test_attendance_add_student_on_day( client, setup_section, day, num_attendances_added ): - mentor, student_user, course, section = setup_section + mentor, student_user, course, section, coord = setup_section with freeze_time(day): client.force_login(student_user) enroll_url = reverse("section-students", kwargs={"pk": section.pk}) @@ -181,7 +187,7 @@ def test_attendance_add_student_on_day( def test_attendance_drop_student_on_day( client, setup_section, day, num_attendances_left ): - mentor, student_user, course, section = setup_section + mentor, student_user, course, section, coord = setup_section # enroll student first with freeze_time(timezone.datetime(2020, 6, 1, 0, 0, 0, tzinfo=DEFAULT_TZ)): @@ -210,3 +216,38 @@ def test_attendance_drop_student_on_day( # make sure attendance objects have been deleted assert student.attendance_set.count() == num_attendances_left + + +@pytest.mark.django_db +def test_coord_add_attendance(client, setup_section): + mentor, student_user, course, section, coord = setup_section() + with freeze_time(timezone.datetime(2020, 6, 1, 0, 0, 0, tzinfo=DEFAULT_TZ)): + client.force_login(coord) + student = Student.objects.get(user=student_user) + attendance_url = reverse("student-attendances", kwargs={"pk": student.pk}) + + # Mark student as present + data = {'id': student.id, 'presence': 'PR'} + client.put(attendance_url, data) + student.refresh_from_db() + + +@pytest.mark.django_db +def test_coord_add_student_attendance(client, setup_section): + mentor, student_user, course, section, coord = setup_section() + with freeze_time(timezone.datetime(2020, 6, 1, 0, 0, 0, tzinfo=DEFAULT_TZ)): + client.force_login(coord) + data = {'emails': [{'email': "amogus@berkeley.edu"}], 'actions': {}} + add_student_url = reverse("section-students", kwargs={"pk": section.pk}) + client.put(add_student_url, data) + + # check of the student was added + assert section.students.count() == 1 + + student = Student.objects.filter(user__email="amogus@berkeley.edu") + attendance_url = reverse("student-attendances", kwargs={"pk": student.pk}) + + # Mark student as present + data = {'id': student.id, 'presence': 'PR'} + client.put(attendance_url, data) + student.refresh_from_db() diff --git a/csm_web/scheduler/views/student.py b/csm_web/scheduler/views/student.py index fc3d29c8..48eab516 100644 --- a/csm_web/scheduler/views/student.py +++ b/csm_web/scheduler/views/student.py @@ -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 @@ -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" 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 {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" Could not record attendance for User {log_str(request.user)}, used non-existent attendance id {request.data['id']}" + f" 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 {log_str(attendance)} recorded for User {log_str(request.user)}" - ) - return Response(status=status.HTTP_204_NO_CONTENT) - logger.error( - f" Could not record attendance for User {log_str(request.user)}, errors: {serializer.errors}" - ) - return Response(serializer.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY) -