Ticket #5614: 5614-checkin.patch
File 5614-checkin.patch, 20.9 KB (added by , 16 years ago) |
---|
-
django/contrib/auth/create_superuser.py
diff --git a/django/contrib/auth/create_superuser.py b/django/contrib/auth/create_superuser.py index bb89e30..7b6cefd 100644
a b 1 1 """ 2 Helper function for creating superusers in the authentication system. 3 2 4 If run from the command line, this module lets you create a superuser 3 5 interactively. 4 6 """ 5 7 6 from django.contrib.auth.management.commands.createsuperuser import createsuperuser 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." 7 92 8 93 if __name__ == "__main__": 9 94 createsuperuser() -
django/contrib/auth/management.py
diff --git a/django/contrib/auth/management.py b/django/contrib/auth/management.py index e69de29..2b4cb8b 100644
a b 1 """ 2 Creates permissions for all installed apps that need permissions. 3 """ 4 5 from django.dispatch import dispatcher 6 from django.db.models import get_models, signals 7 from django.contrib.auth import models as auth_app 8 9 def _get_permission_codename(action, opts): 10 return u'%s_%s' % (action, opts.object_name.lower()) 11 12 def _get_all_permissions(opts): 13 "Returns (codename, name) for all permissions in the given opts." 14 perms = [] 15 for action in ('add', 'change', 'delete'): 16 perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw))) 17 return perms + list(opts.permissions) 18 19 def create_permissions(app, created_models, verbosity): 20 from django.contrib.contenttypes.models import ContentType 21 from django.contrib.auth.models import Permission 22 app_models = get_models(app) 23 if not app_models: 24 return 25 for klass in app_models: 26 ctype = ContentType.objects.get_for_model(klass) 27 for codename, name in _get_all_permissions(klass._meta): 28 p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id, 29 defaults={'name': name, 'content_type': ctype}) 30 if created and verbosity >= 2: 31 print "Adding permission '%s'" % p 32 33 def create_superuser(app, created_models, verbosity, **kwargs): 34 from django.contrib.auth.models import User 35 from django.contrib.auth.create_superuser import createsuperuser as do_create 36 if User in created_models and kwargs.get('interactive', True): 37 msg = "\nYou just installed Django's auth system, which means you don't have " \ 38 "any superusers defined.\nWould you like to create one now? (yes/no): " 39 confirm = raw_input(msg) 40 while 1: 41 if confirm not in ('yes', 'no'): 42 confirm = raw_input('Please enter either "yes" or "no": ') 43 continue 44 if confirm == 'yes': 45 do_create() 46 break 47 48 dispatcher.connect(create_permissions, signal=signals.post_syncdb) 49 dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb) -
deleted file django/contrib/auth/management/__init__.py
diff --git a/django/contrib/auth/management/__init__.py b/django/contrib/auth/management/__init__.py deleted file mode 100644 index d82340d..0000000
+ - 1 """2 Creates permissions for all installed apps that need permissions.3 """4 5 from django.dispatch import dispatcher6 from django.db.models import get_models, signals7 from django.contrib.auth import models as auth_app8 9 def _get_permission_codename(action, opts):10 return u'%s_%s' % (action, opts.object_name.lower())11 12 def _get_all_permissions(opts):13 "Returns (codename, name) for all permissions in the given opts."14 perms = []15 for action in ('add', 'change', 'delete'):16 perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))17 return perms + list(opts.permissions)18 19 def create_permissions(app, created_models, verbosity):20 from django.contrib.contenttypes.models import ContentType21 from django.contrib.auth.models import Permission22 app_models = get_models(app)23 if not app_models:24 return25 for klass in app_models:26 ctype = ContentType.objects.get_for_model(klass)27 for codename, name in _get_all_permissions(klass._meta):28 p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,29 defaults={'name': name, 'content_type': ctype})30 if created and verbosity >= 2:31 print "Adding permission '%s'" % p32 33 def create_superuser(app, created_models, verbosity, **kwargs):34 from django.contrib.auth.models import User35 from django.contrib.auth.management.commands.createsuperuser import createsuperuser as do_create36 if User in created_models and kwargs.get('interactive', True):37 msg = "\nYou just installed Django's auth system, which means you don't have " \38 "any superusers defined.\nWould you like to create one now? (yes/no): "39 confirm = raw_input(msg)40 while 1:41 if confirm not in ('yes', 'no'):42 confirm = raw_input('Please enter either "yes" or "no": ')43 continue44 if confirm == 'yes':45 do_create()46 break47 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)52 """53 Creates permissions for all installed apps that need permissions.54 """55 56 from django.dispatch import dispatcher57 from django.db.models import get_models, signals58 from django.contrib.auth import models as auth_app59 60 def _get_permission_codename(action, opts):61 return u'%s_%s' % (action, opts.object_name.lower())62 63 def _get_all_permissions(opts):64 "Returns (codename, name) for all permissions in the given opts."65 perms = []66 for action in ('add', 'change', 'delete'):67 perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))68 return perms + list(opts.permissions)69 70 def create_permissions(app, created_models, verbosity):71 from django.contrib.contenttypes.models import ContentType72 from django.contrib.auth.models import Permission73 app_models = get_models(app)74 if not app_models:75 return76 for klass in app_models:77 ctype = ContentType.objects.get_for_model(klass)78 for codename, name in _get_all_permissions(klass._meta):79 p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,80 defaults={'name': name, 'content_type': ctype})81 if created and verbosity >= 2:82 print "Adding permission '%s'" % p83 84 def create_superuser(app, created_models, verbosity, **kwargs):85 from django.contrib.auth.models import User86 from django.contrib.auth.management.commands.createsuperuser import createsuperuser as do_create87 if User in created_models and kwargs.get('interactive', True):88 msg = "\nYou just installed Django's auth system, which means you don't have " \89 "any superusers defined.\nWould you like to create one now? (yes/no): "90 confirm = raw_input(msg)91 while 1:92 if confirm not in ('yes', 'no'):93 confirm = raw_input('Please enter either "yes" or "no": ')94 continue95 if confirm == 'yes':96 do_create()97 break98 99 if 'create_permissions' not in [i.__name__ for i in dispatcher.getAllReceivers(signal=signals.post_syncdb)]:100 dispatcher.connect(create_permissions, signal=signals.post_syncdb)101 if 'create_superuser' not in [i.__name__ for i in dispatcher.getAllReceivers(signal=signals.post_syncdb, sender=auth_app)]:102 dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb) -
deleted file django/contrib/auth/management/commands/createsuperuser.py
diff --git a/django/contrib/auth/management/commands/__init__.py b/django/contrib/auth/management/commands/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py deleted file mode 100644 index a75db4f..0000000
+ - 1 from django.core.management.base import BaseCommand2 from optparse import make_option3 4 """5 Helper function for creating superusers in the authentication system.6 7 If this management command is run, this command lets you create a superuser8 interactively.9 """10 11 from django.core import validators12 from django.contrib.auth.models import User13 import getpass14 import os15 import sys16 import re17 18 RE_VALID_USERNAME = re.compile('\w+$')19 20 def createsuperuser(username=None, email=None, password=None):21 """22 Helper function for creating a superuser from the command line. All23 arguments are optional and will be prompted-for if invalid or not given.24 """25 try:26 import pwd27 except ImportError:28 default_username = ''29 else:30 # Determine the current system user's username, to use as a default.31 default_username = pwd.getpwuid(os.getuid())[0].replace(' ', '').lower()32 33 # Determine whether the default username is taken, so we don't display34 # it as an option.35 if default_username:36 try:37 User.objects.get(username=default_username)38 except User.DoesNotExist:39 pass40 else:41 default_username = ''42 43 try:44 while 1:45 if not username:46 input_msg = 'Username'47 if default_username:48 input_msg += ' (Leave blank to use %r)' % default_username49 username = raw_input(input_msg + ': ')50 if default_username and username == '':51 username = default_username52 if not RE_VALID_USERNAME.match(username):53 sys.stderr.write("Error: That username is invalid. Use only letters, digits and underscores.\n")54 username = None55 continue56 try:57 User.objects.get(username=username)58 except User.DoesNotExist:59 break60 else:61 sys.stderr.write("Error: That username is already taken.\n")62 username = None63 while 1:64 if not email:65 email = raw_input('E-mail address: ')66 try:67 validators.isValidEmail(email, None)68 except validators.ValidationError:69 sys.stderr.write("Error: That e-mail address is invalid.\n")70 email = None71 else:72 break73 while 1:74 if not password:75 password = getpass.getpass()76 password2 = getpass.getpass('Password (again): ')77 if password != password2:78 sys.stderr.write("Error: Your passwords didn't match.\n")79 password = None80 continue81 if password.strip() == '':82 sys.stderr.write("Error: Blank passwords aren't allowed.\n")83 password = None84 continue85 break86 except KeyboardInterrupt:87 sys.stderr.write("\nOperation cancelled.\n")88 sys.exit(1)89 User.objects.create_superuser(username, email, password)90 print "Superuser created successfully."91 92 class Command(BaseCommand):93 option_list = BaseCommand.option_list + (94 make_option('--username', dest='username', default=None,95 help='Specifies the username for the superuser.'),96 make_option('--email', dest='email', default=None,97 help='Specifies the email address for the superuser.'),98 )99 help = 'Used to create a superuser.'100 101 def handle(self, *args, **options):102 username = options.get('username', None)103 email = options.get('email', None)104 105 createsuperuser(username=username, password=None, email=email)106 107 from django.core.management.base import BaseCommand108 from optparse import make_option109 110 """111 Helper function for creating superusers in the authentication system.112 113 If this management command is run, this command lets you create a superuser114 interactively.115 """116 117 from django.core import validators118 from django.contrib.auth.models import User119 import getpass120 import os121 import sys122 import re123 124 RE_VALID_USERNAME = re.compile('\w+$')125 126 def createsuperuser(username=None, email=None, password=None):127 """128 Helper function for creating a superuser from the command line. All129 arguments are optional and will be prompted-for if invalid or not given.130 """131 try:132 import pwd133 except ImportError:134 default_username = ''135 else:136 # Determine the current system user's username, to use as a default.137 default_username = pwd.getpwuid(os.getuid())[0].replace(' ', '').lower()138 139 # Determine whether the default username is taken, so we don't display140 # it as an option.141 if default_username:142 try:143 User.objects.get(username=default_username)144 except User.DoesNotExist:145 pass146 else:147 default_username = ''148 149 try:150 while 1:151 if not username:152 input_msg = 'Username'153 if default_username:154 input_msg += ' (Leave blank to use %r)' % default_username155 username = raw_input(input_msg + ': ')156 if default_username and username == '':157 username = default_username158 if not RE_VALID_USERNAME.match(username):159 sys.stderr.write("Error: That username is invalid. Use only letters, digits and underscores.\n")160 username = None161 continue162 try:163 User.objects.get(username=username)164 except User.DoesNotExist:165 break166 else:167 sys.stderr.write("Error: That username is already taken.\n")168 username = None169 while 1:170 if not email:171 email = raw_input('E-mail address: ')172 try:173 validators.isValidEmail(email, None)174 except validators.ValidationError:175 sys.stderr.write("Error: That e-mail address is invalid.\n")176 email = None177 else:178 break179 while 1:180 if not password:181 password = getpass.getpass()182 password2 = getpass.getpass('Password (again): ')183 if password != password2:184 sys.stderr.write("Error: Your passwords didn't match.\n")185 password = None186 continue187 if password.strip() == '':188 sys.stderr.write("Error: Blank passwords aren't allowed.\n")189 password = None190 continue191 break192 except KeyboardInterrupt:193 sys.stderr.write("\nOperation cancelled.\n")194 sys.exit(1)195 User.objects.create_superuser(username, email, password)196 print "Superuser created successfully."197 198 class Command(BaseCommand):199 option_list = BaseCommand.option_list + (200 make_option('--username', dest='username', default=None,201 help='Specifies the username for the superuser.'),202 make_option('--email', dest='email', default=None,203 help='Specifies the email address for the superuser.'),204 )205 help = 'Used to create a superuser.'206 207 def handle(self, *args, **options):208 username = options.get('username', None)209 email = options.get('email', None)210 211 createsuperuser(username=username, password=None, email=email)212 -
django/contrib/auth/models.py
diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index f74d1d7..c867065 100644
a b class UserManager(models.Manager): 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 = True122 u.is_active = True123 u.is_superuser = True124 u.save()125 126 119 def make_random_password(self, length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'): 127 120 "Generates a random password with the given length and given allowed_chars" 128 121 # Note that default value of allowed_chars does not have "I" or letters -
docs/authentication.txt
diff --git a/docs/authentication.txt b/docs/authentication.txt index 0c51051..79eaf67 100644
a b Creating superusers 264 264 265 265 ``manage.py syncdb`` prompts you to create a superuser the first time you run 266 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 268 ``manage.py createsuperuser`` **New in Django development version.**:: 269 270 manage.py createsuperuser --username=joe --email=joe@example.com 271 272 You will be prompted for a password. Once entered, the user is created. If you 273 leave off the ``--username`` or the ``--email`` option, It will prompt you for 274 those values as well. 275 276 The old way of creating a superuser on the command line is still available:: 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:: 277 269 278 270 python /path/to/django/contrib/auth/create_superuser.py 279 271 280 Where ``/path/to`` is the path to the Django codebase on your filesystem. 272 Make sure to substitute ``/path/to/`` with the path to the Django codebase on 273 your filesystem. 281 274 282 275 Storing additional information about users 283 276 ------------------------------------------