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
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 44 additions & 3 deletions csm_web/scheduler/tests/models/test_attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Course,
Section,
Mentor,
Coordinator,
Student,
Attendance,
SectionOccurrence,
Expand All @@ -21,6 +22,7 @@
CourseFactory,
SectionFactory,
MentorFactory,
CoordinatorFactory,
StudentFactory,
AttendanceFactory,
SpacetimeFactory,
Expand All @@ -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"
)
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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': "[email protected]"}], '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="[email protected]")
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()
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)