Skip to content

Instantly share code, notes, and snippets.

@hanleybrand
Created October 17, 2012 01:43
Show Gist options
  • Save hanleybrand/3903264 to your computer and use it in GitHub Desktop.
Save hanleybrand/3903264 to your computer and use it in GitHub Desktop.
Chooser (username entry on django command line)
from django.contrib.auth.models import User
# assumes an already logged in user,
# with their username captured in a variable
target_user = chooser(raw_input("enter the username that will own the slideshow being created. Press Enter for %s " % username), username)
def chooser(anotherUser, me, inception = 0):
""" Given raw input and an already authenticated user, returns a valid user name
or the already logged in user """
# inception is to keep track of recursive calls to chooser
if anotherUser != '':
try:
usr = User.objects.get(username=anotherUser)
except User.DoesNotExist:
print 'ALERT: User with username %s not found' % anotherUser
anotherTry = raw_input('Try another username? (Press Enter for %s) > ' % me)
usr = chooser(anotherTry, me, inception + 1)
else:
usr = User.objects.get(username=me)
if inception == 0:
print 'Returning %s for user %s' % (usr.id, usr.username)
return usr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment