Ticket #17327: createsuperuser-database-option.diff

File createsuperuser-database-option.diff, 5.6 KB (added by Brian Riley, 13 years ago)
  • 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..64847a4 100644
    a b import getpass  
    66import re
    77import sys
    88from optparse import make_option
     9
    910from django.contrib.auth.models import User
    1011from django.contrib.auth.management import get_default_username
    1112from django.core import exceptions
    1213from django.core.management.base import BaseCommand, CommandError
     14from django.db import DEFAULT_DB_ALIAS
    1315from django.utils.translation import ugettext as _
    1416
    1517RE_VALID_USERNAME = re.compile('[\w.@+-]+$')
    EMAIL_RE = re.compile(  
    1921    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"' # quoted-string
    2022    r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
    2123
     24
    2225def is_valid_email(value):
    2326    if not EMAIL_RE.search(value):
    2427        raise exceptions.ValidationError(_('Enter a valid e-mail address.'))
    2528
     29
    2630class Command(BaseCommand):
    2731    option_list = BaseCommand.option_list + (
    2832        make_option('--username', dest='username', default=None,
    class Command(BaseCommand):  
    3438                  'You must use --username and --email with --noinput, and '
    3539                  'superusers created with --noinput will not be able to log '
    3640                  '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.'),
    3744    )
    3845    help = 'Used to create a superuser.'
    3946
    class Command(BaseCommand):  
    4249        email = options.get('email', None)
    4350        interactive = options.get('interactive')
    4451        verbosity = int(options.get('verbosity', 1))
     52        database = options.get('database')
    4553
    4654        # Do quick and dirty validation if --noinput
    4755        if not interactive:
    class Command(BaseCommand):  
    114122                sys.stderr.write("\nOperation cancelled.\n")
    115123                sys.exit(1)
    116124
    117         User.objects.create_superuser(username, email, password)
     125        User.objects.db_manager(database).create_superuser(username, email, password)
    118126        if verbosity >= 1:
    119127          self.stdout.write("Superuser created successfully.\n")
    120128
  • django/contrib/auth/tests/__init__.py

    diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py
    index 7cb0dcb..86c5ce2 100644
    a b  
    11from django.contrib.auth.tests.auth_backends import (BackendTest,
    22    RowlevelBackendTest, AnonymousUserBackendTest, NoBackendsTest,
    33    InActiveUserBackendTest, NoInActiveUserBackendTest)
    4 from django.contrib.auth.tests.basic import BasicTestCase, PasswordUtilsTestCase
     4from django.contrib.auth.tests.basic import BasicTestCase, MultiDBTestCase, PasswordUtilsTestCase
    55from django.contrib.auth.tests.context_processors import AuthContextProcessorTests
    66from django.contrib.auth.tests.decorators import LoginRequiredTestCase
    77from django.contrib.auth.tests.forms import (UserCreationFormTest,
  • django/contrib/auth/tests/basic.py

    diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py
    index 9f94c2a..98a20fb 100644
    a b  
     1from StringIO import StringIO
     2
    13from django.test import TestCase
    24from django.utils.unittest import skipUnless
    35from django.contrib.auth.models import User, AnonymousUser
    46from django.contrib.auth import utils
    57from django.core.management import call_command
    6 from StringIO import StringIO
    78
    89try:
    910    import crypt as crypt_module
    class BasicTestCase(TestCase):  
    100101        self.assertEqual(u.email, 'joe2@somewhere.org')
    101102        self.assertFalse(u.has_usable_password())
    102103
    103 
    104104        new_io = StringIO()
    105105        call_command("createsuperuser",
    106106            interactive=False,
    class BasicTestCase(TestCase):  
    113113        self.assertFalse(u.has_usable_password())
    114114
    115115
     116class 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
    116135class PasswordUtilsTestCase(TestCase):
    117136
    118137    def _test_make_password(self, algo):
  • docs/ref/django-admin.txt

    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.  
    11081108
    11091109.. django-admin-option:: --username
    11101110.. django-admin-option:: --email
     1111.. django-admin-option:: --database
    11111112
    11121113The username and email address for the new account can be supplied by
    11131114using the ``--username`` and ``--email`` arguments on the command
    11141115line. If either of those is not supplied, ``createsuperuser`` will prompt for
    11151116it when running interactively.
    11161117
     1118The ``--database`` option can be used to specify the database into which the
     1119superuser object will be saved.
     1120
    11171121``django.contrib.gis``
    11181122----------------------
    11191123
Back to Top