Created
April 21, 2016 16:00
-
-
Save pbassut/bfa44aad2819162eb16ff7d178d1cbc5 to your computer and use it in GitHub Desktop.
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
def strip_accents(s): | |
return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn') | |
def create_username(first_name, last_name=None): | |
guessed_name = first_name[:10] | |
if last_name: | |
guessed_name += '.%s' % last_name[:10] | |
# i = randint(0, 1000) # begins with a random number in order to avoid database overhead | |
i = 1 | |
guessed_name = filter(lambda x: x.isalnum(), guessed_name) | |
temp_name = guessed_name | |
while User.objects.filter(username__iexact=temp_name).count() > 0: | |
temp_name = guessed_name + '.%s' % str(i) | |
i += 1 | |
return strip_accents(temp_name.lower()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment