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
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]()
elif action == 'inspectdb':
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.args = "[appname]"
def createsuperuser():
def createsuperuser(username=None, email=None, password=None):
"Creates a superuser account."
from django.core import validators
from django.models.auth import users
import getpass
try:
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():
sys.stderr.write("Error: That username is invalid.\n")
continue
username = None
try:
users.get_object(username__exact=username)
except users.UserDoesNotExist:
break
else:
sys.stderr.write("Error: That username is already taken.\n")
username = None
while 1:
email = raw_input('E-mail address: ')
if not email:
email = raw_input('E-mail address: ')
try:
validators.isValidEmail(email, None)
except validators.ValidationError:
sys.stderr.write("Error: That e-mail address is invalid.\n")
email = None
else:
break
while 1:
password = getpass.getpass()
password2 = getpass.getpass('Password (again): ')
if password != password2:
sys.stderr.write("Error: Your passwords didn't match.\n")
continue
if not password:
password = getpass.getpass()
password2 = getpass.getpass('Password (again): ')
if password != password2:
sys.stderr.write("Error: Your passwords didn't match.\n")
password = None
continue
if password.strip() == '':
sys.stderr.write("Error: Blank passwords aren't allowed.\n")
password = None
continue
break
except KeyboardInterrupt:
@ -525,7 +532,7 @@ def createsuperuser():
u.is_superuser = True
u.save()
print "User created successfully."
createsuperuser.args = ''
createsuperuser.args = '[username] [email] [password] (Either all or none)'
def inspectdb(db_name):
"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
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
----