Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#798 closed task (fixed)

Specifying username, email, and password on as arguments to "django-admin.py createsuperuser"

Reported by: bjorn@… Owned by: adrian
Component: Translations Version: 0.91
Severity: trivial Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

We're frequently rebuilding the entire Django database environment, and it's a pain to specify the username, email, and password to the createsuperuser command. This patch enable you to specify these on the command line so the process can be completely automated/ done in a batch script.

Index: django/bin/django-admin.py
===================================================================
--- django/bin/django-admin.py (revision 1235)
+++ django/bin/django-admin.py (working copy)
@@ -81,7 +81,7 @@

translation.activate('en-us')

if action in ('createsuperuser', 'init', 'validate'):

  • ACTION_MAPPING[action]()

+ ACTION_MAPPING[action](*args[1:])

elif action == 'inspectdb':

try:

param = args[1]

Index: django/core/management.py
===================================================================
--- django/core/management.py (revision 1235)
+++ django/core/management.py (working copy)
@@ -480,39 +480,43 @@

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

+ sys.stderr.write("Error: That username is invalid\n")
+ username = None

try:

users.get_object(usernameexact=username)

except users.UserDoesNotExist:

break

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:

  • 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")

+ 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")
+ continue

if password.strip() == :

sys.stderr.write("Error: Blank passwords aren't allowed.\n")

+ password = None

continue

break

except KeyboardInterrupt:

@@ -524,7 +528,7 @@

u.is_superuser = True
u.save()
print "User created successfully."

-createsuperuser.args =
+createsuperuser.args = '[optional username, email, password]'

def inspectdb(db_name):

"Generator that introspects the tables in the given database name and returns a Django model, one line at a time."

Attachments (1)

createsuperuser.patch (3.4 KB) - added by bjorn@… 8 years ago.
Patch to createsuperuser

Download all attachments as: .zip

Change History (5)

Changed 8 years ago by bjorn@…

Patch to createsuperuser

comment:1 Changed 8 years ago by garthk

It's a little rusty, but miniflush.py demonstrates a slightly different approach: automate the whole thing so you don't have to even run django-admin multiple times.

comment:2 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(In [1474]) Fixed #798 and #715 -- Added optional arguments to createsuperuser, for each use in shell scripts. Thanks for the patch, bjorn@…

comment:3 Changed 8 years ago by anonymous

  • Component changed from django-admin.py to Translations
  • milestone set to Version 0.92
  • priority changed from normal to lowest
  • Severity changed from normal to trivial
  • Summary changed from Specifying username, email, and password on as arguments to "django-admin.py createsuperuser" to Specifying username, email, and password on as arguments to "django-admin.py createsuperuser"
  • Type changed from enhancement to task
  • Version set to 0.91

comment:4 Changed 7 years ago by adrian

  • milestone Version 0.92 deleted

Milestone Version 0.92 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.