Created
May 15, 2010 00:03
-
-
Save banterability/401851 to your computer and use it in GitHub Desktop.
Derive next available username
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from simonw's code to djangopeople.net | |
import re | |
notalpha_re = re.compile('[^a-zA-Z0-9]') | |
def derive_username(nickname): | |
nickname = notalpha_re.sub('', nickname) | |
if not nickname: | |
return '' | |
base_nickname = nickname | |
to_add = 1 | |
while True: | |
try: | |
DjangoPerson.objects.get(user__username = nickname) | |
except DjangoPerson.DoesNotExist: | |
break | |
nickname = base_nickname + str(to_add) | |
to_add += 1 | |
return nickname |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment