app/django/contrib/auth/create_superuser.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     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.
       
     6 """
       
     7 
       
     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 if __name__ == "__main__":
       
    94     createsuperuser()