diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py
index b3a3479..64847a4 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):
|
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 | |
diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
index 7cb0dcb..86c5ce2 100644
a
|
b
|
|
1 | 1 | from django.contrib.auth.tests.auth_backends import (BackendTest, |
2 | 2 | RowlevelBackendTest, AnonymousUserBackendTest, NoBackendsTest, |
3 | 3 | InActiveUserBackendTest, NoInActiveUserBackendTest) |
4 | | from django.contrib.auth.tests.basic import BasicTestCase, PasswordUtilsTestCase |
| 4 | from django.contrib.auth.tests.basic import BasicTestCase, MultiDBTestCase, PasswordUtilsTestCase |
5 | 5 | from django.contrib.auth.tests.context_processors import AuthContextProcessorTests |
6 | 6 | from django.contrib.auth.tests.decorators import LoginRequiredTestCase |
7 | 7 | from django.contrib.auth.tests.forms import (UserCreationFormTest, |
diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py
index 9f94c2a..98a20fb 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 StringIO |
7 | 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, |
… |
… |
class BasicTestCase(TestCase):
|
113 | 113 | self.assertFalse(u.has_usable_password()) |
114 | 114 | |
115 | 115 | |
| 116 | class MultiDBTestCase(TestCase): |
| 117 | multi_db = True |
| 118 | |
| 119 | def test_createsuperuser_command_with_database_option(self): |
| 120 | " createsuperuser command should operate on specified DB" |
| 121 | new_io = StringIO() |
| 122 | call_command("createsuperuser", |
| 123 | interactive=False, |
| 124 | username="joe", |
| 125 | email="joe@somewhere.org", |
| 126 | database='other', |
| 127 | stdout=new_io |
| 128 | ) |
| 129 | command_output = new_io.getvalue().strip() |
| 130 | self.assertEqual(command_output, 'Superuser created successfully.') |
| 131 | u = User.objects.using('other').get(username="joe") |
| 132 | self.assertEqual(u.email, 'joe@somewhere.org') |
| 133 | |
| 134 | |
116 | 135 | class PasswordUtilsTestCase(TestCase): |
117 | 136 | |
118 | 137 | def _test_make_password(self, algo): |
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index 8b83a43..658b267 100644
a
|
b
|
a password has been manually set for it.
|
1108 | 1108 | |
1109 | 1109 | .. django-admin-option:: --username |
1110 | 1110 | .. django-admin-option:: --email |
| 1111 | .. django-admin-option:: --database |
1111 | 1112 | |
1112 | 1113 | The username and email address for the new account can be supplied by |
1113 | 1114 | using the ``--username`` and ``--email`` arguments on the command |
1114 | 1115 | line. If either of those is not supplied, ``createsuperuser`` will prompt for |
1115 | 1116 | it when running interactively. |
1116 | 1117 | |
| 1118 | The ``--database`` option can be used to specify the database into which the |
| 1119 | superuser object will be saved. |
| 1120 | |
1117 | 1121 | ``django.contrib.gis`` |
1118 | 1122 | ---------------------- |
1119 | 1123 | |