Skip to content

Commit

Permalink
fix: Append a suffix integer to quser with length < 4 (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
qcoumes authored and plgitlogin committed Feb 29, 2024
1 parent 08a9295 commit 46576c2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: [3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand All @@ -44,7 +44,7 @@ jobs:
run: tox -e py

- name: Upload coverage to Codecov
if: matrix.python-version == 3.8
if: matrix.python-version == 3.9
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
4 changes: 4 additions & 0 deletions lti_app/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ def test_wims_username(self):
self.assertEqual(
"jaaaceeeiiiooouuuu", utils.wims_username("Jean", "àâäçéêëíìïóòôúùûü")
)

# Add suffix so that length > 4
self.assertEqual("ab01", utils.wims_username("a", "b"))
self.assertEqual("abb1", utils.wims_username("a", "bb"))


def test_get_or_create_user_create_invalid_character_adapt(self):
Expand Down
39 changes: 30 additions & 9 deletions lti_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,41 @@ def get_or_create_class(lms: LMS, wims_srv: WIMS, wapi: wimsapi.WimsAPI,
def wims_username(firstname: str, lastname: str) -> str:
"""Create a valid login identifier for WIMS, taking care of translating
accented characters to their ASCII counterpart.
Replace some other character with underscores."""
Replace some other character with underscores.
Append an integer suffix the length of the would be less than 4."""
src = "áàâäçéèêëíìîïóòôöúùûü"
dst = "aaaaceeeeiiiioooouuuu"
translation = str.maketrans(src, dst)
quser = (firstname[0] + lastname).lower()[:22].replace(" ", "")
if len(quser) < 4:
quser += "1".rjust(4 - len(quser), "0")
return quser.translate(translation)



def increment_wims_username(quser: str) -> str:
"""Increment the integer suffix at the end of the `quser`.
The length of the suffix is preserved.
>>> increment_wims_username("jdoe")
jdoe1
>>> increment_wims_username("jdoe12")
jdoe13
>>> increment_wims_username("jdoe0120")
jdoe0121
"""
suffix_length = len(quser) - len(quser.rstrip(string.digits))
# No existing suffix
if not suffix_length:
return quser + "1"
incremented = str(int(quser[-suffix_length:]) + 1).rjust(suffix_length, "0")
return quser[:-suffix_length] + incremented



def create_user(parameters: Dict[str, Any]) -> wimsapi.User:
"""Create an instance of wimsapi.User with the given LTI request's parameters."""
password = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(20))
Expand Down Expand Up @@ -398,16 +423,12 @@ def get_or_create_user(wclass_db: WimsClass, wclass: wimsapi.Class, parameters:
# in this case, keep trying by appending integer to quser (jdoe, jdoe1,
# jdoe2, ...), stopping after 100 tries.

# Can also be raised if an error occured while communicating with the
# Can also be raised if an error occurred while communicating with the
# WIMS server, hence the following test.
if "user already exists" not in str(e) or i >= 100: # pragma: no cover
raise

if i:
user.quser = user.quser[:-len(str(i))]
i += 1
user.quser += str(i)

user.quser = increment_wims_username(user.quser)

user_db = WimsUser.objects.create(
lms_guid=parameters["user_id"], wclass=wclass_db, quser=user.quser
)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ title = WIMS-LTI Coverage
[tox:tox]
skipsdist = True
distshare = {homedir}/.tox/distshare
envlist = py{36,37,38,39}
envlist = py{38,39}
skip_missing_interpreters = true
indexserver =
pypi = https://pypi.python.org/simple
Expand Down

0 comments on commit 46576c2

Please sign in to comment.