Skip to content

Commit 98bb794

Browse files
committed
Protect against buggy participants reports from Aalto SISU API
Fixes #1180
1 parent 8ab057c commit 98bb794

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

course/models.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from django.utils.text import format_lazy
2525
from django.utils.translation import gettext_lazy as _
2626
from django_colortag.models import ColorTag
27+
from requests.exceptions import HTTPError
2728

2829
from apps.models import BaseTab, BasePlugin
2930
from authorization.models import JWTAccessible
@@ -935,6 +936,11 @@ def enroll_from_sis(self) -> Tuple[int, int]:
935936
delcount = 0
936937
try:
937938
participants = sis.get_participants(self.sis_id)
939+
except HTTPError as exc:
940+
code = exc.response.status_code
941+
# pylint: disable-next=logging-fstring-interpolation
942+
logger.exception(f"{self}: Error {code} when getting participants from SIS.")
943+
return -1, -1
938944
except Exception:
939945
# pylint: disable-next=logging-fstring-interpolation
940946
logger.exception(f"{self}: Error in getting participants from SIS.")
@@ -954,16 +960,18 @@ def enroll_from_sis(self) -> Tuple[int, int]:
954960
# yet logged in to A+, then the user profile does not exist yet.
955961
pass
956962

957-
# Remove SIS-enrolled students who are not anymore in SIS participants,
958-
# for example, because they have first enrolled in SIS, but then
959-
# unenrolled themselves.
960-
students = self.all_students.filter(enrollment__from_sis=True)
961-
to_remove = students.exclude(student_id__in=participants)
962-
qs = Enrollment.objects.filter(user_profile__in=to_remove, course_instance=self)
963-
qs.update(status=Enrollment.ENROLLMENT_STATUS.REMOVED)
964-
for e in qs:
965-
invalidate_content(Enrollment, e)
966-
delcount += 1
963+
# Ignore empty participants list caused by a rare SIS API gateway malfunction
964+
if participants:
965+
# Remove SIS-enrolled students who are not anymore in SIS participants,
966+
# for example, because they have first enrolled in SIS, but then
967+
# unenrolled themselves.
968+
students = self.all_students.filter(enrollment__from_sis=True)
969+
to_remove = students.exclude(student_id__in=participants)
970+
qs = Enrollment.objects.filter(user_profile__in=to_remove, course_instance=self)
971+
qs.update(status=Enrollment.ENROLLMENT_STATUS.REMOVED)
972+
for e in qs:
973+
invalidate_content(Enrollment, e)
974+
delcount += 1
967975

968976
# pylint: disable-next=logging-fstring-interpolation
969977
logger.info(f"{self}: enrolled {addcount}, removed {delcount} students based on SIS")

0 commit comments

Comments
 (0)