Skip to content

Instantly share code, notes, and snippets.

@emperorcezar
Last active December 12, 2015 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emperorcezar/4725313 to your computer and use it in GitHub Desktop.
Save emperorcezar/4725313 to your computer and use it in GitHub Desktop.
# Try and generate a unique username. Hopefully they never see this.
for count in itertools.count(1):
# Get the first part of the email and use 30 characters
username = request.POST['email'].split('@')[0][:30]
if not User.objects.filter(username = username).exists():
break
else:
# Try and append a number in the last postion of the username
username = username[:30-len(str(count))] + str(count)
continue
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment