Skip to content

Commit

Permalink
Fixed #798 and #715 -- Added optional arguments to createsuperuser, f…
Browse files Browse the repository at this point in the history
…or 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
  • Loading branch information
adrianholovaty committed Nov 28, 2005
1 parent 5de6fe1 commit e85b071
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
9 changes: 8 additions & 1 deletion django/bin/django-admin.py
Expand Up @@ -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:
Expand Down
27 changes: 17 additions & 10 deletions django/core/management.py
Expand Up @@ -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:
Expand All @@ -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."
Expand Down
6 changes: 6 additions & 0 deletions docs/django-admin.txt
Expand Up @@ -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
----

Expand Down

0 comments on commit e85b071

Please sign in to comment.