Fixed #798 and #715 -- Added optional arguments to createsuperuser, for each use in shell scripts. Thanks for the patch, bjorn@exoweb.net

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1474 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-28 02:57:38 +00:00
parent 5de6fe1b2d
commit e85b071e47
3 changed files with 31 additions and 11 deletions

View File

@ -80,7 +80,14 @@ def main():
from django.utils import translation from django.utils import translation
translation.activate('en-us') translation.activate('en-us')
if action in ('createsuperuser', 'init', 'validate'): if action == 'createsuperuser':
try:
username, email, password = args[1], args[2], args[3]
except IndexError:
sys.stderr.write("Error: %r requires arguments of 'username email password' or no argument at all.\n")
sys.exit(1)
ACTION_MAPPING[action](username, email, password)
elif action in ('init', 'validate'):
ACTION_MAPPING[action]() ACTION_MAPPING[action]()
elif action == 'inspectdb': elif action == 'inspectdb':
try: try:

View File

@ -481,39 +481,46 @@ def startapp(app_name, directory):
startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory." startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory."
startapp.args = "[appname]" startapp.args = "[appname]"
def createsuperuser(): def createsuperuser(username=None, email=None, password=None):
"Creates a superuser account." "Creates a superuser account."
from django.core import validators from django.core import validators
from django.models.auth import users from django.models.auth import users
import getpass import getpass
try: try:
while 1: while 1:
username = raw_input('Username (only letters, digits and underscores): ') if not username:
username = raw_input('Username (only letters, digits and underscores): ')
if not username.isalnum(): if not username.isalnum():
sys.stderr.write("Error: That username is invalid.\n") sys.stderr.write("Error: That username is invalid.\n")
continue username = None
try: try:
users.get_object(username__exact=username) users.get_object(username__exact=username)
except users.UserDoesNotExist: except users.UserDoesNotExist:
break break
else: else:
sys.stderr.write("Error: That username is already taken.\n") sys.stderr.write("Error: That username is already taken.\n")
username = None
while 1: while 1:
email = raw_input('E-mail address: ') if not email:
email = raw_input('E-mail address: ')
try: try:
validators.isValidEmail(email, None) validators.isValidEmail(email, None)
except validators.ValidationError: except validators.ValidationError:
sys.stderr.write("Error: That e-mail address is invalid.\n") sys.stderr.write("Error: That e-mail address is invalid.\n")
email = None
else: else:
break break
while 1: while 1:
password = getpass.getpass() if not password:
password2 = getpass.getpass('Password (again): ') password = getpass.getpass()
if password != password2: password2 = getpass.getpass('Password (again): ')
sys.stderr.write("Error: Your passwords didn't match.\n") if password != password2:
continue sys.stderr.write("Error: Your passwords didn't match.\n")
password = None
continue
if password.strip() == '': if password.strip() == '':
sys.stderr.write("Error: Blank passwords aren't allowed.\n") sys.stderr.write("Error: Blank passwords aren't allowed.\n")
password = None
continue continue
break break
except KeyboardInterrupt: except KeyboardInterrupt:
@ -525,7 +532,7 @@ def createsuperuser():
u.is_superuser = True u.is_superuser = True
u.save() u.save()
print "User created successfully." print "User created successfully."
createsuperuser.args = '' createsuperuser.args = '[username] [email] [password] (Either all or none)'
def inspectdb(db_name): def inspectdb(db_name):
"Generator that introspects the tables in the given database name and returns a Django model, one line at a time." "Generator that introspects the tables in the given database name and returns a Django model, one line at a time."

View File

@ -54,6 +54,12 @@ createsuperuser
Creates a superuser account interactively. It asks you for a username, e-mail Creates a superuser account interactively. It asks you for a username, e-mail
address and password. address and password.
**New in Django development version:** You can specify
``username email password`` on the command line, for convenient use in shell
scripts. Example::
django-admin.py createsuperuser john john@example.com mypassword
init init
---- ----