Django

Code

Changeset 7590

Show
Ignore:
Timestamp:
06/08/08 00:31:16 (4 months ago)
Author:
jacob
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/auth/create_superuser.py

    r7018 r7590  
    11""" 
    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. 
     2Create a superuser from the command line. Deprecated; use manage.py 
     3createsuperuser instead. 
    64""" 
    75 
    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  
    936if __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  
    3333def create_superuser(app, created_models, verbosity, **kwargs): 
    3434    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 
    3636    if User in created_models and kwargs.get('interactive', True): 
    3737        msg = "\nYou just installed Django's auth system, which means you don't have " \ 
     
    4343                continue 
    4444            if confirm == 'yes': 
    45                 do_create(
     45                call_command("createsuperuser"
    4646            break 
    4747 
    48 dispatcher.connect(create_permissions, signal=signals.post_syncdb) 
    49 dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb) 
     48if 'create_permissions' not in [i.__name__ for i in dispatcher.getAllReceivers(signal=signals.post_syncdb)]: 
     49    dispatcher.connect(create_permissions, signal=signals.post_syncdb) 
     50if '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  
    117117        return user 
    118118 
     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 
    119126    def make_random_password(self, length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'): 
    120127        "Generates a random password with the given length and given allowed_chars" 
  • django/trunk/django/contrib/auth/tests.py

    r6912 r7590  
    3636>>> a.user_permissions.all() 
    3737[] 
     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") 
     48Superuser created successfully. 
     49 
     50>>> u = User.objects.get(username="joe") 
     51>>> u.email 
     52u'joe@somewhere.org' 
     53>>> u.password 
     54u'!' 
    3855""" 
  • django/trunk/docs/authentication.txt

    r7388 r7590  
    264264 
    265265``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:: 
     266it after adding ``'django.contrib.auth'`` to your ``INSTALLED_APPS``. If you need 
     267to 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 
     273You will be prompted for a password. Once entered, the user is created. If you 
     274leave off the ``--username`` or the ``--email`` option, It will prompt you for 
     275those values as well. 
     276 
     277If you're using an older release of Django, the old way of creating a superuser 
     278on the command line still works:: 
    269279 
    270280    python /path/to/django/contrib/auth/create_superuser.py 
    271281 
    272 Make sure to substitute ``/path/to/`` with the path to the Django codebase on 
    273 your filesystem. 
     282Where ``/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 
     284environment for you. 
    274285 
    275286Storing additional information about users