Changeset 7590
- Timestamp:
- 06/08/08 00:31:16 (4 months ago)
- Files:
-
- django/trunk/django/contrib/auth/create_superuser.py (modified) (1 diff)
- django/trunk/django/contrib/auth/management (added)
- django/trunk/django/contrib/auth/management/commands (added)
- django/trunk/django/contrib/auth/management/commands/createsuperuser.py (added)
- django/trunk/django/contrib/auth/management/commands/__init__.py (added)
- django/trunk/django/contrib/auth/management/__init__.py (moved) (moved from django/trunk/django/contrib/auth/management.py) (2 diffs)
- django/trunk/django/contrib/auth/models.py (modified) (1 diff)
- django/trunk/django/contrib/auth/tests.py (modified) (1 diff)
- django/trunk/docs/authentication.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/auth/create_superuser.py
r7018 r7590 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 validators9 from django.contrib.auth.models import User10 import getpass11 import os12 import sys13 import re14 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. All20 arguments are optional and will be prompted-for if invalid or not given.21 """22 try:23 import pwd24 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 display31 # it as an option.32 if default_username:33 try:34 User.objects.get(username=default_username)35 except User.DoesNotExist:36 pass37 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_username46 username = raw_input(input_msg + ': ')47 if default_username and username == '':48 username = default_username49 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 = None52 continue53 try:54 User.objects.get(username=username)55 except User.DoesNotExist:56 break57 else:58 sys.stderr.write("Error: That username is already taken.\n")59 username = None60 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 = None68 else:69 break70 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 = None77 continue78 if password.strip() == '':79 sys.stderr.write("Error: Blank passwords aren't allowed.\n")80 password = None81 continue82 break83 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 = True88 u.is_active = True89 u.is_superuser = True90 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") django/trunk/django/contrib/auth/management/__init__.py
r5609 r7590 33 33 def create_superuser(app, created_models, verbosity, **kwargs): 34 34 from django.contrib.auth.models import User 35 from django.co ntrib.auth.create_superuser import createsuperuser as do_create35 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 " \ … … 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) django/trunk/django/contrib/auth/models.py
r7414 r7590 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" django/trunk/django/contrib/auth/tests.py
r6912 r7590 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 """ django/trunk/docs/authentication.txt
r7388 r7590 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
