Ticket #17327: auth-management-commands-database-option.diff
File auth-management-commands-database-option.diff, 11.6 KB (added by , 13 years ago) |
---|
-
django/contrib/auth/management/commands/changepassword.py
diff --git a/django/contrib/auth/management/commands/changepassword.py b/django/contrib/auth/management/commands/changepassword.py index 56448f1..e2815ed 100644
a b 1 import getpass 2 from optparse import make_option 3 1 4 from django.core.management.base import BaseCommand, CommandError 2 5 from django.contrib.auth.models import User 3 import getpass 6 from django.db import DEFAULT_DB_ALIAS 7 4 8 5 9 class Command(BaseCommand): 10 option_list = BaseCommand.option_list + ( 11 make_option('--database', action='store', dest='database', 12 default=DEFAULT_DB_ALIAS, help='Nominates a database to query for the user. ' 13 'Defaults to the "default" database.'), 14 ) 6 15 help = "Change a user's password for django.contrib.auth." 7 16 8 17 requires_model_validation = False … … class Command(BaseCommand): 23 32 username = getpass.getuser() 24 33 25 34 try: 26 u = User.objects. get(username=username)35 u = User.objects.using(options.get('database')).get(username=username) 27 36 except User.DoesNotExist: 28 37 raise CommandError("user '%s' does not exist" % username) 29 38 30 print "Changing password for user '%s'" % u.username39 self.stdout.write("Changing password for user '%s'\n" % u.username) 31 40 32 41 MAX_TRIES = 3 33 42 count = 0 … … class Command(BaseCommand): 36 45 p1 = self._get_pass() 37 46 p2 = self._get_pass("Password (again): ") 38 47 if p1 != p2: 39 print "Passwords do not match. Please try again."48 self.stdout.write("Passwords do not match. Please try again.\n") 40 49 count = count + 1 41 50 42 51 if count == MAX_TRIES: -
django/contrib/auth/management/commands/createsuperuser.py
diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index b3a3479..89a76b3 100644
a b import getpass 6 6 import re 7 7 import sys 8 8 from optparse import make_option 9 9 10 from django.contrib.auth.models import User 10 11 from django.contrib.auth.management import get_default_username 11 12 from django.core import exceptions 12 13 from django.core.management.base import BaseCommand, CommandError 14 from django.db import DEFAULT_DB_ALIAS 13 15 from django.utils.translation import ugettext as _ 14 16 15 17 RE_VALID_USERNAME = re.compile('[\w.@+-]+$') … … EMAIL_RE = re.compile( 19 21 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"' # quoted-string 20 22 r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain 21 23 24 22 25 def is_valid_email(value): 23 26 if not EMAIL_RE.search(value): 24 27 raise exceptions.ValidationError(_('Enter a valid e-mail address.')) 25 28 29 26 30 class Command(BaseCommand): 27 31 option_list = BaseCommand.option_list + ( 28 32 make_option('--username', dest='username', default=None, … … class Command(BaseCommand): 34 38 'You must use --username and --email with --noinput, and ' 35 39 'superusers created with --noinput will not be able to log ' 36 40 'in until they\'re given a valid password.')), 41 make_option('--database', action='store', dest='database', 42 default=DEFAULT_DB_ALIAS, help='Nominates a database to save the user to. ' 43 'Defaults to the "default" database.'), 37 44 ) 38 45 help = 'Used to create a superuser.' 39 46 … … class Command(BaseCommand): 42 49 email = options.get('email', None) 43 50 interactive = options.get('interactive') 44 51 verbosity = int(options.get('verbosity', 1)) 52 database = options.get('database') 45 53 46 54 # Do quick and dirty validation if --noinput 47 55 if not interactive: … … class Command(BaseCommand): 77 85 username = None 78 86 continue 79 87 try: 80 User.objects. get(username=username)88 User.objects.using(database).get(username=username) 81 89 except User.DoesNotExist: 82 90 break 83 91 else: … … class Command(BaseCommand): 114 122 sys.stderr.write("\nOperation cancelled.\n") 115 123 sys.exit(1) 116 124 117 User.objects. create_superuser(username, email, password)125 User.objects.db_manager(database).create_superuser(username, email, password) 118 126 if verbosity >= 1: 119 127 self.stdout.write("Superuser created successfully.\n") 120 128 -
django/contrib/auth/tests/__init__.py
diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py index 7cb0dcb..cde5c8d 100644
a b from django.contrib.auth.tests.forms import (UserCreationFormTest, 9 9 UserChangeFormTest, PasswordResetFormTest) 10 10 from django.contrib.auth.tests.remote_user import (RemoteUserTest, 11 11 RemoteUserNoCreateTest, RemoteUserCustomTest) 12 from django.contrib.auth.tests.management import GetDefaultUsernameTestCase 12 from django.contrib.auth.tests.management import GetDefaultUsernameTestCase, ChangepasswordManagementCommandTestCase, MultiDBChangepasswordManagementCommandTestCase, MultiDBCreatesuperuserTestCase 13 13 from django.contrib.auth.tests.models import ProfileTestCase 14 14 from django.contrib.auth.tests.signals import SignalTestCase 15 15 from django.contrib.auth.tests.tokens import TokenGeneratorTest -
django/contrib/auth/tests/basic.py
diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py index 9f94c2a..2a7685b 100644
a b 1 from StringIO import StringIO 2 1 3 from django.test import TestCase 2 4 from django.utils.unittest import skipUnless 3 5 from django.contrib.auth.models import User, AnonymousUser 4 6 from django.contrib.auth import utils 5 7 from django.core.management import call_command 6 from StringIO import StringIO7 8 8 9 try: 9 10 import crypt as crypt_module … … class BasicTestCase(TestCase): 100 101 self.assertEqual(u.email, 'joe2@somewhere.org') 101 102 self.assertFalse(u.has_usable_password()) 102 103 103 104 104 new_io = StringIO() 105 105 call_command("createsuperuser", 106 106 interactive=False, -
django/contrib/auth/tests/management.py
diff --git a/django/contrib/auth/tests/management.py b/django/contrib/auth/tests/management.py index 0af6873..2287ab9 100644
a b 1 from django.test import TestCase 1 from StringIO import StringIO 2 2 3 from django.contrib.auth import models, management 4 from django.contrib.auth.management.commands import changepassword 5 from django.core.management import call_command 6 from django.test import TestCase 3 7 4 8 5 9 class GetDefaultUsernameTestCase(TestCase): … … class GetDefaultUsernameTestCase(TestCase): 25 29 # 'Julia' with accented 'u': 26 30 management.get_system_username = lambda: u'J\xfalia' 27 31 self.assertEqual(management.get_default_username(), 'julia') 32 33 34 class ChangepasswordManagementCommandTestCase(TestCase): 35 36 def setUp(self): 37 self.user = models.User.objects.create_user(username='joe', password='qwerty') 38 self.stdout = StringIO() 39 self.stderr = StringIO() 40 41 def tearDown(self): 42 self.stdout.close() 43 self.stderr.close() 44 45 def test_that_changepassword_command_changes_joes_password(self): 46 " Executing the changepassword management command should change joe's password " 47 self.assertTrue(self.user.check_password('qwerty')) 48 command = changepassword.Command() 49 command._get_pass = lambda *args: 'not qwerty' 50 51 command.execute("joe", stdout=self.stdout) 52 command_output = self.stdout.getvalue().strip() 53 54 self.assertEquals(command_output, "Changing password for user 'joe'\nPassword changed successfully for user 'joe'") 55 self.assertTrue(models.User.objects.get(username="joe").check_password("not qwerty")) 56 57 def test_that_max_tries_exits_1(self): 58 """ 59 A CommandError should be thrown by handle() if the user enters in 60 mismatched passwords three times. This should be caught by execute() and 61 converted to a SystemExit 62 """ 63 command = changepassword.Command() 64 command._get_pass = lambda *args: args or 'foo' 65 66 self.assertRaises( 67 SystemExit, 68 command.execute, 69 "joe", 70 stdout=self.stdout, 71 stderr=self.stderr 72 ) 73 74 75 class MultiDBChangepasswordManagementCommandTestCase(TestCase): 76 multi_db = True 77 78 def setUp(self): 79 self.user = models.User.objects.db_manager('other').create_user(username='joe', password='qwerty') 80 self.stdout = StringIO() 81 82 def tearDown(self): 83 self.stdout.close() 84 85 def test_that_changepassword_command_with_database_option_uses_given_db(self): 86 """ 87 Executing the changepassword management command with a database option 88 should operate on the specified DB 89 """ 90 self.assertTrue(self.user.check_password('qwerty')) 91 command = changepassword.Command() 92 command._get_pass = lambda *args: 'not qwerty' 93 94 command.execute("joe", database='other', stdout=self.stdout) 95 command_output = self.stdout.getvalue().strip() 96 97 self.assertEquals(command_output, "Changing password for user 'joe'\nPassword changed successfully for user 'joe'") 98 self.assertTrue(models.User.objects.using('other').get(username="joe").check_password("not qwerty")) 99 100 101 class MultiDBCreatesuperuserTestCase(TestCase): 102 multi_db = True 103 104 def test_createsuperuser_command_with_database_option(self): 105 " createsuperuser command should operate on specified DB" 106 new_io = StringIO() 107 108 call_command("createsuperuser", 109 interactive=False, 110 username="joe", 111 email="joe@somewhere.org", 112 database='other', 113 stdout=new_io 114 ) 115 command_output = new_io.getvalue().strip() 116 117 self.assertEqual(command_output, 'Superuser created successfully.') 118 119 u = models.User.objects.using('other').get(username="joe") 120 self.assertEqual(u.email, 'joe@somewhere.org') 121 122 new_io.close() -
docs/ref/django-admin.txt
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 8b83a43..6eff2ab 100644
a b the user given as parameter. If they both match, the new password will be 1084 1084 changed immediately. If you do not supply a user, the command will attempt to 1085 1085 change the password whose username matches the current user. 1086 1086 1087 .. django-admin-option:: --database 1088 1089 The ``--database`` option can be used to specify the database to query for the 1090 user. If it is not supplied the ``default`` database will be used. 1091 1087 1092 Example usage:: 1088 1093 1089 1094 django-admin.py changepassword ringo … … a password has been manually set for it. 1108 1113 1109 1114 .. django-admin-option:: --username 1110 1115 .. django-admin-option:: --email 1116 .. django-admin-option:: --database 1111 1117 1112 1118 The username and email address for the new account can be supplied by 1113 1119 using the ``--username`` and ``--email`` arguments on the command 1114 1120 line. If either of those is not supplied, ``createsuperuser`` will prompt for 1115 1121 it when running interactively. 1116 1122 1123 The ``--database`` option can be used to specify the database into which the 1124 superuser object will be saved. 1125 1117 1126 ``django.contrib.gis`` 1118 1127 ---------------------- 1119 1128