Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5614: added 'manage.py createsuperuser'. Thanks, programmerq.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7590 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5675ae4443ae07325c97c70e4f951e1d6b06c2d3 1 parent 8d4f79a
Jacob Kaplan-Moss authored June 08, 2008
0  auth/management/commands/__init__.py b/django/contrib/auth/management/commands/__init__.py
No changes.
94  django/contrib/auth/create_superuser.py
... ...
@@ -1,94 +1,8 @@
1 1
 """
2  
-Helper function for creating superusers in the authentication system.
3  
-
4  
-If run from the command line, this module lets you create a superuser
5  
-interactively.
  2
+Create a superuser from the command line. Deprecated; use manage.py
  3
+createsuperuser instead.
6 4
 """
7 5
 
8  
-from django.core import validators
9  
-from django.contrib.auth.models import User
10  
-import getpass
11  
-import os
12  
-import sys
13  
-import re
14  
-
15  
-RE_VALID_USERNAME = re.compile('\w+$')
16  
-
17  
-def createsuperuser(username=None, email=None, password=None):
18  
-    """
19  
-    Helper function for creating a superuser from the command line. All
20  
-    arguments are optional and will be prompted-for if invalid or not given.
21  
-    """
22  
-    try:
23  
-        import pwd
24  
-    except ImportError:
25  
-        default_username = ''
26  
-    else:
27  
-        # Determine the current system user's username, to use as a default.
28  
-        default_username = pwd.getpwuid(os.getuid())[0].replace(' ', '').lower()
29  
-
30  
-    # Determine whether the default username is taken, so we don't display
31  
-    # it as an option.
32  
-    if default_username:
33  
-        try:
34  
-            User.objects.get(username=default_username)
35  
-        except User.DoesNotExist:
36  
-            pass
37  
-        else:
38  
-            default_username = ''
39  
-
40  
-    try:
41  
-        while 1:
42  
-            if not username:
43  
-                input_msg = 'Username'
44  
-                if default_username:
45  
-                    input_msg += ' (Leave blank to use %r)' % default_username
46  
-                username = raw_input(input_msg + ': ')
47  
-            if default_username and username == '':
48  
-                username = default_username
49  
-            if not RE_VALID_USERNAME.match(username):
50  
-                sys.stderr.write("Error: That username is invalid. Use only letters, digits and underscores.\n")
51  
-                username = None
52  
-                continue
53  
-            try:
54  
-                User.objects.get(username=username)
55  
-            except User.DoesNotExist:
56  
-                break
57  
-            else:
58  
-                sys.stderr.write("Error: That username is already taken.\n")
59  
-                username = None
60  
-        while 1:
61  
-            if not email:
62  
-                email = raw_input('E-mail address: ')
63  
-            try:
64  
-                validators.isValidEmail(email, None)
65  
-            except validators.ValidationError:
66  
-                sys.stderr.write("Error: That e-mail address is invalid.\n")
67  
-                email = None
68  
-            else:
69  
-                break
70  
-        while 1:
71  
-            if not password:
72  
-                password = getpass.getpass()
73  
-                password2 = getpass.getpass('Password (again): ')
74  
-                if password != password2:
75  
-                    sys.stderr.write("Error: Your passwords didn't match.\n")
76  
-                    password = None
77  
-                    continue
78  
-            if password.strip() == '':
79  
-                sys.stderr.write("Error: Blank passwords aren't allowed.\n")
80  
-                password = None
81  
-                continue
82  
-            break
83  
-    except KeyboardInterrupt:
84  
-        sys.stderr.write("\nOperation cancelled.\n")
85  
-        sys.exit(1)
86  
-    u = User.objects.create_user(username, email, password)
87  
-    u.is_staff = True
88  
-    u.is_active = True
89  
-    u.is_superuser = True
90  
-    u.save()
91  
-    print "Superuser created successfully."
92  
-
93 6
 if __name__ == "__main__":
94  
-    createsuperuser()
  7
+    from django.core.management import call_command
  8
+    call_command("createuseruser")
10  django/contrib/auth/management.py → django/contrib/auth/management/__init__.py
@@ -32,7 +32,7 @@ def create_permissions(app, created_models, verbosity):
32 32
 
33 33
 def create_superuser(app, created_models, verbosity, **kwargs):
34 34
     from django.contrib.auth.models import User
35  
-    from django.contrib.auth.create_superuser import createsuperuser as do_create
  35
+    from django.core.management import call_command
36 36
     if User in created_models and kwargs.get('interactive', True):
37 37
         msg = "\nYou just installed Django's auth system, which means you don't have " \
38 38
                 "any superusers defined.\nWould you like to create one now? (yes/no): "
@@ -42,8 +42,10 @@ def create_superuser(app, created_models, verbosity, **kwargs):
42 42
                 confirm = raw_input('Please enter either "yes" or "no": ')
43 43
                 continue
44 44
             if confirm == 'yes':
45  
-                do_create()
  45
+                call_command("createsuperuser")
46 46
             break
47 47
 
48  
-dispatcher.connect(create_permissions, signal=signals.post_syncdb)
49  
-dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
  48
+if 'create_permissions' not in [i.__name__ for i in dispatcher.getAllReceivers(signal=signals.post_syncdb)]:
  49
+    dispatcher.connect(create_permissions, signal=signals.post_syncdb)
  50
+if 'create_superuser' not in [i.__name__ for i in dispatcher.getAllReceivers(signal=signals.post_syncdb, sender=auth_app)]:
  51
+    dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
122  django/contrib/auth/management/commands/createsuperuser.py
... ...
@@ -0,0 +1,122 @@
  1
+"""
  2
+Management utility to create superusers.
  3
+"""
  4
+
  5
+import getpass
  6
+import os
  7
+import re
  8
+import sys
  9
+from optparse import make_option
  10
+from django.contrib.auth.models import User, UNUSABLE_PASSWORD
  11
+from django.core import validators
  12
+from django.core.management.base import BaseCommand, CommandError
  13
+
  14
+RE_VALID_USERNAME = re.compile('\w+$')
  15
+
  16
+class Command(BaseCommand):
  17
+    option_list = BaseCommand.option_list + (
  18
+        make_option('--username', dest='username', default=None,
  19
+            help='Specifies the username for the superuser.'),
  20
+        make_option('--email', dest='email', default=None,
  21
+            help='Specifies the email address for the superuser.'),
  22
+        make_option('--noinput', action='store_false', dest='interactive', default=True,
  23
+            help='Tells Django to NOT prompt the user for input of any kind. '    \
  24
+                 'You must use --username and --email with --noinput, and '      \
  25
+                 'superusers created with --noinput will not be able to log in '  \
  26
+                 'until they\'re given a valid password.'),
  27
+    )
  28
+    help = 'Used to create a superuser.'
  29
+
  30
+    def handle(self, *args, **options):
  31
+        username = options.get('username', None)
  32
+        email = options.get('email', None)
  33
+        interactive = options.get('interactive')
  34
+        
  35
+        # Do quick and dirty validation if --noinput
  36
+        if not interactive:
  37
+            if not username or not email:
  38
+                raise CommandError("You must use --username and --email with --noinput.")
  39
+            if not RE_VALID_USERNAME.match(username):
  40
+                raise CommandError("Invalid username. Use only letters, digits, and underscores")
  41
+            try:
  42
+                validators.isValidEmail(email, None)
  43
+            except validators.ValidationError:
  44
+                raise CommandError("Invalid email address.")
  45
+            password = ''
  46
+
  47
+        # Try to determine the current system user's username to use as a default.
  48
+        try:
  49
+            import pwd
  50
+        except ImportError:
  51
+            default_username = ''
  52
+        else:
  53
+            default_username = pwd.getpwuid(os.getuid())[0].replace(' ', '').lower()
  54
+
  55
+        # Determine whether the default username is taken, so we don't display
  56
+        # it as an option.
  57
+        if default_username:
  58
+            try:
  59
+                User.objects.get(username=default_username)
  60
+            except User.DoesNotExist:
  61
+                pass
  62
+            else:
  63
+                default_username = ''
  64
+
  65
+        # Prompt for username/email/password. Enclose this whole thing in a
  66
+        # try/except to trap for a keyboard interrupt and exit gracefully.
  67
+        if interactive:
  68
+            try:
  69
+            
  70
+                # Get a username
  71
+                while 1:
  72
+                    if not username:
  73
+                        input_msg = 'Username'
  74
+                        if default_username:
  75
+                            input_msg += ' (Leave blank to use %r)' % default_username
  76
+                        username = raw_input(input_msg + ': ')
  77
+                    if default_username and username == '':
  78
+                        username = default_username
  79
+                    if not RE_VALID_USERNAME.match(username):
  80
+                        sys.stderr.write("Error: That username is invalid. Use only letters, digits and underscores.\n")
  81
+                        username = None
  82
+                        continue
  83
+                    try:
  84
+                        User.objects.get(username=username)
  85
+                    except User.DoesNotExist:
  86
+                        break
  87
+                    else:
  88
+                        sys.stderr.write("Error: That username is already taken.\n")
  89
+                        username = None
  90
+            
  91
+                # Get an email
  92
+                while 1:
  93
+                    if not email:
  94
+                        email = raw_input('E-mail address: ')
  95
+                    try:
  96
+                        validators.isValidEmail(email, None)
  97
+                    except validators.ValidationError:
  98
+                        sys.stderr.write("Error: That e-mail address is invalid.\n")
  99
+                        email = None
  100
+                    else:
  101
+                        break
  102
+            
  103
+                # Get a password
  104
+                while 1:
  105
+                    if not password:
  106
+                        password = getpass.getpass()
  107
+                        password2 = getpass.getpass('Password (again): ')
  108
+                        if password != password2:
  109
+                            sys.stderr.write("Error: Your passwords didn't match.\n")
  110
+                            password = None
  111
+                            continue
  112
+                    if password.strip() == '':
  113
+                        sys.stderr.write("Error: Blank passwords aren't allowed.\n")
  114
+                        password = None
  115
+                        continue
  116
+                    break
  117
+            except KeyboardInterrupt:
  118
+                sys.stderr.write("\nOperation cancelled.\n")
  119
+                sys.exit(1)
  120
+        
  121
+        User.objects.create_superuser(username, email, password)
  122
+        print "Superuser created successfully."
7  django/contrib/auth/models.py
@@ -116,6 +116,13 @@ def create_user(self, username, email, password=None):
116 116
         user.save()
117 117
         return user
118 118
 
  119
+    def create_superuser(self, username, email, password):
  120
+        u = self.create_user(username, email, password)
  121
+        u.is_staff = True
  122
+        u.is_active = True
  123
+        u.is_superuser = True
  124
+        u.save()
  125
+
119 126
     def make_random_password(self, length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'):
120 127
         "Generates a random password with the given length and given allowed_chars"
121 128
         # Note that default value of allowed_chars does not have "I" or letters
17  django/contrib/auth/tests.py
@@ -35,4 +35,21 @@
35 35
 []
36 36
 >>> a.user_permissions.all()
37 37
 []
  38
+
  39
+#
  40
+# Tests for createsuperuser management command.
  41
+# It's nearly impossible to test the interactive mode -- a command test helper
  42
+# would be needed (and *awesome*) -- so just test the non-interactive mode.
  43
+# This covers most of the important validation, but not all.
  44
+#
  45
+>>> from django.core.management import call_command
  46
+
  47
+>>> call_command("createsuperuser", noinput=True, username="joe", email="joe@somewhere.org")
  48
+Superuser created successfully.
  49
+
  50
+>>> u = User.objects.get(username="joe")
  51
+>>> u.email
  52
+u'joe@somewhere.org'
  53
+>>> u.password
  54
+u'!'
38 55
 """
21  docs/authentication.txt
@@ -263,14 +263,25 @@ Creating superusers
263 263
 -------------------
264 264
 
265 265
 ``manage.py syncdb`` prompts you to create a superuser the first time you run
266  
-it after adding ``'django.contrib.auth'`` to your ``INSTALLED_APPS``. But if
267  
-you need to create a superuser after that via the command line, you can use the
268  
-``create_superuser.py`` utility. Just run this command::
  266
+it after adding ``'django.contrib.auth'`` to your ``INSTALLED_APPS``. If you need
  267
+to create a superuser at a later date, you can use a command line utility.
  268
+
  269
+**New in Django development version.**::
  270
+
  271
+    manage.py createsuperuser --username=joe --email=joe@example.com
  272
+
  273
+You will be prompted for a password. Once entered, the user is created. If you
  274
+leave off the ``--username`` or the ``--email`` option, It will prompt you for
  275
+those values as well.
  276
+
  277
+If you're using an older release of Django, the old way of creating a superuser
  278
+on the command line still works::
269 279
 
270 280
     python /path/to/django/contrib/auth/create_superuser.py
271 281
 
272  
-Make sure to substitute ``/path/to/`` with the path to the Django codebase on
273  
-your filesystem.
  282
+Where ``/path/to`` is the path to the Django codebase on your filesystem. The
  283
+``manage.py`` command is prefered since it'll figure out the correct path and
  284
+environment for you.
274 285
 
275 286
 Storing additional information about users
276 287
 ------------------------------------------

0 notes on commit 5675ae4

Please sign in to comment.
Something went wrong with that request. Please try again.