Skip to content

Commit

Permalink
feat(exams): auto match old student ID's
Browse files Browse the repository at this point in the history
We'll automatically grant the spades to the active student instead now
  • Loading branch information
vEnhance committed Dec 3, 2024
1 parent fb3c1a0 commit b922700
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions exams/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Meta:

class ParticipationPointsForm(forms.Form):
exam = forms.ModelChoiceField(PracticeExam.objects.filter(is_test=True))
pks = forms.CharField(
sids = forms.CharField(
widget=forms.Textarea,
help_text="ID's to create stuff for, paste one per line",
help_text="Student ID's to create stuff for, paste one per line",
validators=[
RegexValidator(r"[0-9\n]+"),
],
Expand Down
31 changes: 26 additions & 5 deletions exams/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_mocks(self):
self.login("dead")
self.assertGetDenied("mocks", follow=True)

def test_participation_points(self):
def test_participation_points_with_one_semester(self):
test_waltz = PracticeExam.objects.get(family="Waltz", is_test=True)
quiz_waltz = PracticeExam.objects.get(family="Waltz", is_test=False)
# first check that plebians can't login
Expand All @@ -233,30 +233,51 @@ def test_participation_points(self):
self.assertGetOK("participation-points")
# invalid post
self.assertHas(
self.assertPostOK("participation-points", data={"pks": "9001"}),
self.assertPostOK("participation-points", data={"sids": "9001"}),
"This field is required",
)
self.assertEqual(MockCompleted.objects.all().count(), 0)
self.assertHas(
self.assertPostOK(
"participation-points", data={"exam": quiz_waltz.pk, "pks": "9001"}
"participation-points", data={"exam": quiz_waltz.pk, "sids": "9001"}
),
"Select a valid choice",
)
self.assertEqual(MockCompleted.objects.all().count(), 0)

resp1 = self.assertPostOK(
"participation-points",
data={"exam": test_waltz.pk, "pks": "\n".join(pks[:4])},
data={"exam": test_waltz.pk, "sids": "\n".join(pks[:4])},
)
self.assertHas(resp1, "Created 4 completion database entries")
self.assertNotHas(resp1, "with existing entries")
self.assertEqual(MockCompleted.objects.all().count(), 4)

resp2 = self.assertPostOK(
"participation-points",
data={"exam": test_waltz.pk, "pks": "\n".join(pks[2:7])},
data={"exam": test_waltz.pk, "sids": "\n".join(pks[2:7])},
)
self.assertHas(resp2, "Created 3 completion database entries")
self.assertHas(resp2, "There were 2 students with existing entries")
self.assertEqual(MockCompleted.objects.all().count(), 7)

def test_participation_points_multi_semester(self):
test_waltz = PracticeExam.objects.get(family="Waltz", is_test=True)
admin = UserFactory.create(is_staff=True, is_superuser=True)
self.login(admin)
cathy = UserFactory.create(username="cathy")
cathy_student_old = StudentFactory.create(
user=cathy, semester=ExamTest.semester_old
)
cathy_student_new = StudentFactory.create(
user=cathy, semester=ExamTest.semester
)

resp = self.assertPostOK(
"participation-points",
data={"exam": test_waltz.pk, "sids": str(cathy_student_old.pk)},
)
self.assertHas(resp, "Created 1 completion database entries")
self.assertEqual(MockCompleted.objects.all().count(), 1)
mc = MockCompleted.objects.get()
self.assertEqual(mc.student.pk, cathy_student_new.pk)
9 changes: 7 additions & 2 deletions exams/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from exams.calculator import expr_compute
from otisweb.decorators import admin_required
from otisweb.utils import AuthHttpRequest
from roster.models import Student
from roster.utils import get_student_by_pk, infer_student

from .forms import ExamAttemptForm, ParticipationPointsForm
Expand Down Expand Up @@ -151,11 +152,15 @@ def participation_points(request: AuthHttpRequest) -> HttpResponse:
if request.method == "POST":
form = ParticipationPointsForm(request.POST)
if form.is_valid():
pks = [
sids = [
int(line)
for line in form.cleaned_data["pks"].splitlines()
for line in form.cleaned_data["sids"].splitlines()
if line.strip().isdigit()
]
# Look for students whose ID's match those in SID's and active
pks = Student.objects.filter(
semester__active=True, user__student__pk__in=sids
).values_list("pk", flat=True)
existing_completes = MockCompleted.objects.filter(
exam=form.cleaned_data["exam"]
)
Expand Down

0 comments on commit b922700

Please sign in to comment.