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

Improve logging and output on SIS interactions #1159

Merged
merged 1 commit into from
Feb 3, 2023
Merged
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
23 changes: 13 additions & 10 deletions course/models.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import json
import logging
import string
from typing import Any, Dict, List
from typing import Any, Dict, List, Tuple
import urllib.request, urllib.parse
from random import choice

@@ -904,28 +904,30 @@ def enroll_student(self, user, from_sis=False, use_pending=False):
return True
return False

def enroll_from_sis(self) -> int:
def enroll_from_sis(self) -> Tuple[int, int]:
"""
Enroll students based on the participants information in Student Info System.
If student has removed herself in SIS, she will also be marked as removed in A+.
Returns
-------
Number of students enrolled based on this call. -1 if there was problem accessing SIS.
Number of enrolled and removed students based on this call.
-1 if there was problem accessing SIS.
"""
from .sis import get_sis_configuration, StudentInfoSystem
from .cache.menu import invalidate_content

sis: StudentInfoSystem = get_sis_configuration()
if not sis:
return -1
return -1, -1

count = 0
addcount = 0
delcount = 0
try:
participants = sis.get_participants(self.sis_id)
except Exception as e:
logger.exception(f"Error in getting participants from SIS.")
return -1
logger.exception(f"{self}: Error in getting participants from SIS.")
return -1, -1

from exercise.models import LearningObject
use_pending = bool(LearningObject.objects.find_enrollment_exercise(self, False))
@@ -934,7 +936,7 @@ def enroll_from_sis(self) -> int:
try:
profile = UserProfile.get_by_student_id(i)
if self.enroll_student(profile.user, from_sis=True, use_pending=use_pending):
count = count + 1
addcount += 1

except UserProfile.DoesNotExist:
# This is a common scenario, if the user has enrolled in SIS, but not
@@ -950,9 +952,10 @@ def enroll_from_sis(self) -> int:
qs.update(status=Enrollment.ENROLLMENT_STATUS.REMOVED)
for e in qs:
invalidate_content(Enrollment, e)
delcount += 1

logger.info(f"{self}: enrolled {count} students from SIS")
return count
logger.info(f"{self}: enrolled {addcount}, removed {delcount} students based on SIS")
return addcount, delcount

def set_users_with_role(self, users, role, remove_others_with_role=False):
# This method is used for adding or replacing (depending on the last
20 changes: 14 additions & 6 deletions course/staff_views.py
Original file line number Diff line number Diff line change
@@ -164,17 +164,25 @@ def get_form_kwargs(self):

def form_valid(self, form):
if (self.instance.sis_id and form.cleaned_data["sis"]):
count = self.instance.enroll_from_sis()
if count >= 0:
addcount, delcount = self.instance.enroll_from_sis()
if addcount >= 0:
# ngettext works only with one parameter at a time, hence the complicated setup.
messages.info(
self.request,
format_lazy(
ngettext(
'STUDENT_ENROLLED_FROM_SIS -- {count}',
'STUDENTS_ENROLLED_FROM_SIS -- {count}',
count
'STUDENT_ENROLLED_FROM_SIS -- {addcount}',
'STUDENTS_ENROLLED_FROM_SIS -- {addcount}',
addcount
),
count=count,
addcount=addcount,
) + " " + format_lazy(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concatenating translated strings with a space could maybe cause trouble with non-western languages that use right-to-left or top-to-bottom script, but let's ignore those now.

An alternative could be to render the two sentences in different paragraphs in the HTML.

ngettext(
'STUDENT_REMOVED_FROM_SIS -- {delcount}',
'STUDENTS_REMOVED_FROM_SIS -- {delcount}',
delcount
),
delcount=delcount,
)
)
else:
17 changes: 12 additions & 5 deletions locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-12-13 12:10+0200\n"
"POT-Creation-Date: 2023-02-03 14:48+0200\n"
"PO-Revision-Date: 2021-05-27 14:47+0300\n"
"Last-Translator: Jimmy Ihalainen <jimmy.ihalainen@aalto.fi>\n"
"Language-Team: English<>\n"
@@ -862,10 +862,17 @@ msgstr "Failed to save changes."

#: course/staff_views.py
#, python-brace-format
msgid "STUDENT_ENROLLED_FROM_SIS -- {count}"
msgid_plural "STUDENTS_ENROLLED_FROM_SIS -- {count}"
msgstr[0] "{count} student has been enrolled from SIS."
msgstr[1] "{count} students have been enrolled from SIS."
msgid "STUDENT_ENROLLED_FROM_SIS -- {addcount}"
msgid_plural "STUDENTS_ENROLLED_FROM_SIS -- {addcount}"
msgstr[0] "{addcount} student has been enrolled based on SIS."
msgstr[1] "{addcount} students have been enrolled based on SIS."

#: course/staff_views.py
#, python-brace-format
msgid "STUDENT_REMOVED_FROM_SIS -- {delcount}"
msgid_plural "STUDENTS_REMOVED_FROM_SIS -- {delcount}"
msgstr[0] "{delcount} student has been removed based on SIS."
msgstr[1] "{delcount} students have been removed based on SIS."

#: course/staff_views.py
msgid "COULD_NOT_ACCESS_SIS"
17 changes: 12 additions & 5 deletions locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-12-13 12:10+0200\n"
"POT-Creation-Date: 2023-02-03 14:48+0200\n"
"PO-Revision-Date: 2019-08-14 12:16+0200\n"
"Last-Translator: Jimmy Ihalainen <jimmy.ihalainen@aalto.fi>\n"
"Language-Team: Finnish <>\n"
@@ -868,10 +868,17 @@ msgstr "Muutosten tallentaminen epäonnistui."

#: course/staff_views.py
#, python-brace-format
msgid "STUDENT_ENROLLED_FROM_SIS -- {count}"
msgid_plural "STUDENTS_ENROLLED_FROM_SIS -- {count}"
msgstr[0] "{count} opiskelija on lisätty opintotietojärjestelmästä."
msgstr[1] "{count} opiskelijaa on lisätty opintotietojärjestelmästä."
msgid "STUDENT_ENROLLED_FROM_SIS -- {addcount}"
msgid_plural "STUDENTS_ENROLLED_FROM_SIS -- {addcount}"
msgstr[0] "{addcount} opiskelija on lisätty opintotietojärjestelmästä."
msgstr[1] "{addcount} opiskelijaa on lisätty opintotietojärjestelmästä."

#: course/staff_views.py
#, python-brace-format
msgid "STUDENT_REMOVED_FROM_SIS -- {delcount}"
msgid_plural "STUDENTS_REMOVED_FROM_SIS -- {delcount}"
msgstr[0] "{delcount} opiskelija on poistettu opintotietojärjestelmästä."
msgstr[1] "{delcount} opiskelijaa on poistettu opintotietojärjestelmästä."

#: course/staff_views.py
msgid "COULD_NOT_ACCESS_SIS"