Skip to content

Instantly share code, notes, and snippets.

@pbassut
Created April 21, 2016 16:00
Show Gist options
  • Save pbassut/bfa44aad2819162eb16ff7d178d1cbc5 to your computer and use it in GitHub Desktop.
Save pbassut/bfa44aad2819162eb16ff7d178d1cbc5 to your computer and use it in GitHub Desktop.
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