Merge branch 'kerl/username_generation' into 'master'
Génération de username plus intelligente Closes #6 and #5 See merge request klub-dev-ens/authens!8
This commit is contained in:
commit
f43036d6c0
1 changed files with 17 additions and 12 deletions
|
@ -74,21 +74,26 @@ class ENSCASBackend:
|
||||||
worry about data races).
|
worry about data races).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Ideally we can have username = cas_login.
|
||||||
|
if not UserModel.objects.filter(username=cas_login).exists():
|
||||||
|
return cas_login
|
||||||
|
|
||||||
|
# Otherwise, add a numerical suffix.
|
||||||
|
|
||||||
|
# If there was a numerical suffix already, remove it:
|
||||||
|
# https://git.eleves.ens.fr/klub-dev-ens/authens/issues/5
|
||||||
|
i = len(cas_login) - 1
|
||||||
|
while i >= 0 and cas_login[i] in "0123456789":
|
||||||
|
i -= 1
|
||||||
|
radical = cas_login[:i + 1]
|
||||||
|
|
||||||
|
# Find an integer i such that radical + str(i) is not taken.
|
||||||
taken = UserModel.objects.values_list("username", flat=True)
|
taken = UserModel.objects.values_list("username", flat=True)
|
||||||
|
taken = taken.filter(username__startswith=radical)
|
||||||
# This should handle most cases and produce a nice username.
|
|
||||||
prefered = [cas_login, "cas_" + cas_login]
|
|
||||||
pref_taken = taken.filter(username__in=prefered)
|
|
||||||
for name in prefered:
|
|
||||||
if name not in pref_taken:
|
|
||||||
return name
|
|
||||||
|
|
||||||
# Worst case: generate a username of the form cas_login + int
|
|
||||||
taken = taken.filter(username__startswith=cas_login)
|
|
||||||
i = 2
|
i = 2
|
||||||
while cas_login + str(i) in taken:
|
while radical + str(i) in taken:
|
||||||
i += 1
|
i += 1
|
||||||
return cas_login + str(i)
|
return radical + str(i)
|
||||||
|
|
||||||
def _get_or_create(self, cas_login, entrance_year):
|
def _get_or_create(self, cas_login, entrance_year):
|
||||||
"""Handles account retrieval, creation and invalidation as described above.
|
"""Handles account retrieval, creation and invalidation as described above.
|
||||||
|
|
Loading…
Reference in a new issue