[1.7.x] Fixed #22770 -- Removed create_superuser from post_migrate signals.

Moved logic to syncdb command for backwards compatibility.

Backport of 93d05536fd from master
This commit is contained in:
Tim Graham 2014-06-10 12:22:07 -04:00
parent d232a5f93f
commit 0767055dfc
6 changed files with 60 additions and 74 deletions

View File

@ -7,15 +7,13 @@ import getpass
import unicodedata import unicodedata
from django.apps import apps from django.apps import apps
from django.contrib.auth import (models as auth_app, get_permission_codename, from django.contrib.auth import models as auth_app, get_permission_codename
get_user_model)
from django.core import exceptions from django.core import exceptions
from django.core.management.base import CommandError from django.core.management.base import CommandError
from django.db import DEFAULT_DB_ALIAS, router from django.db import DEFAULT_DB_ALIAS, router
from django.db.models import signals from django.db.models import signals
from django.utils.encoding import DEFAULT_LOCALE_ENCODING from django.utils.encoding import DEFAULT_LOCALE_ENCODING
from django.utils import six from django.utils import six
from django.utils.six.moves import input
def _get_all_permissions(opts, ctype): def _get_all_permissions(opts, ctype):
@ -119,30 +117,6 @@ def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_
print("Adding permission '%s'" % perm) print("Adding permission '%s'" % perm)
def create_superuser(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
try:
apps.get_model('auth', 'Permission')
except LookupError:
return
UserModel = get_user_model()
from django.core.management import call_command
if not UserModel._default_manager.exists() and interactive:
msg = ("\nYou have installed Django's auth system, and "
"don't have any superusers defined.\nWould you like to create one "
"now? (yes/no): ")
confirm = input(msg)
while 1:
if confirm not in ('yes', 'no'):
confirm = input('Please enter either "yes" or "no": ')
continue
if confirm == 'yes':
call_command("createsuperuser", interactive=True, database=using)
break
def get_system_username(): def get_system_username():
""" """
Try to determine the current system user's username. Try to determine the current system user's username.
@ -207,6 +181,3 @@ def get_default_username(check_db=True):
signals.post_migrate.connect(create_permissions, signals.post_migrate.connect(create_permissions,
dispatch_uid="django.contrib.auth.management.create_permissions") dispatch_uid="django.contrib.auth.management.create_permissions")
signals.post_migrate.connect(create_superuser,
sender=apps.get_app_config('auth'),
dispatch_uid="django.contrib.auth.management.create_superuser")

View File

@ -1,10 +1,13 @@
import warnings import warnings
from optparse import make_option from optparse import make_option
from django.apps import apps
from django.contrib.auth import get_user_model
from django.db import DEFAULT_DB_ALIAS from django.db import DEFAULT_DB_ALIAS
from django.core.management import call_command from django.core.management import call_command
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
from django.utils.deprecation import RemovedInDjango19Warning from django.utils.deprecation import RemovedInDjango19Warning
from django.utils.six.moves import input
class Command(NoArgsCommand): class Command(NoArgsCommand):
@ -22,3 +25,23 @@ class Command(NoArgsCommand):
def handle_noargs(self, **options): def handle_noargs(self, **options):
warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning) warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
call_command("migrate", **options) call_command("migrate", **options)
try:
apps.get_model('auth', 'Permission')
except LookupError:
return
UserModel = get_user_model()
if not UserModel._default_manager.exists() and options.get('interactive'):
msg = ("\nYou have installed Django's auth system, and "
"don't have any superusers defined.\nWould you like to create one "
"now? (yes/no): ")
confirm = input(msg)
while 1:
if confirm not in ('yes', 'no'):
confirm = input('Please enter either "yes" or "no": ')
continue
if confirm == 'yes':
call_command("createsuperuser", interactive=True, database=options['database'])
break

View File

@ -206,40 +206,7 @@ The :djadmin:`migrate` command looks at the :setting:`INSTALLED_APPS` setting
and creates any necessary database tables according to the database settings and creates any necessary database tables according to the database settings
in your :file:`mysite/settings.py` file and the database migrations shipped in your :file:`mysite/settings.py` file and the database migrations shipped
with the app (we'll cover those later). You'll see a message for each with the app (we'll cover those later). You'll see a message for each
migration it applies, and you'll get a prompt asking you if you'd like to migration it applies. If you're interested, run the command-line client for your
create a superuser account for the authentication system.
First, you'll be asked if you would like to create a superuser. Type the word
``yes`` and hit enter.
.. code-block:: text
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Next, enter a username. By default, this will be your system username. Enter
your desired username and press enter.
.. code-block:: text
Username (leave blank to use 'your_username'): admin
You will then be prompted for your desired email address:
.. code-block:: text
Email address: admin@example.com
The final step is to enter your password. You will be asked to enter your
password twice, the second time as a confirmation of the first.
.. code-block:: text
Password: **********
Password (again): *********
Superuser created successfully.
With that done, if you're interested, run the command-line client for your
database and type ``\dt`` (PostgreSQL), ``SHOW TABLES;`` (MySQL), or database and type ``\dt`` (PostgreSQL), ``SHOW TABLES;`` (MySQL), or
``.schema`` (SQLite) to display the tables Django created. ``.schema`` (SQLite) to display the tables Django created.

View File

@ -21,6 +21,37 @@ automatically-generated admin site.
The admin isn't intended to be used by site visitors. It's for site The admin isn't intended to be used by site visitors. It's for site
managers. managers.
Creating an admin user
======================
First we'll need to create a user who can login to the admin site. Run the
following command:
.. code-block:: bash
$ python manage.py createsuperuser
Enter your desired username and press enter.
.. code-block:: text
Username: admin
You will then be prompted for your desired email address:
.. code-block:: text
Email address: admin@example.com
The final step is to enter your password. You will be asked to enter your
password twice, the second time as a confirmation of the first.
.. code-block:: text
Password: **********
Password (again): *********
Superuser created successfully.
Start the development server Start the development server
============================ ============================
@ -59,10 +90,7 @@ browser's settings and on whether Django has a translation for this language.
Enter the admin site Enter the admin site
==================== ====================
Now, try logging in. You created a superuser account in the first part of this Now, try logging in with the superuser account you created in the previous step.
tutorial, remember? If you didn't create one or forgot the password you can
:ref:`create another one <topics-auth-creating-superusers>`.
You should see the Django admin index page: You should see the Django admin index page:
.. image:: _images/admin02.png .. image:: _images/admin02.png

View File

@ -1420,9 +1420,8 @@ This command is only available if Django's :doc:`authentication system
</topics/auth/index>` (``django.contrib.auth``) is installed. </topics/auth/index>` (``django.contrib.auth``) is installed.
Creates a superuser account (a user who has all permissions). This is Creates a superuser account (a user who has all permissions). This is
useful if you need to create an initial superuser account but did not useful if you need to create an initial superuser account or if you need to
do so during the first :djadmin:`migrate`, or if you need to programmatically programmatically generate superuser accounts for your site(s).
generate superuser accounts for your site(s).
When run interactively, this command will prompt for a password for When run interactively, this command will prompt for a password for
the new superuser account. When run non-interactively, no password the new superuser account. When run non-interactively, no password

View File

@ -66,9 +66,7 @@ interactively <auth-admin>`.
Creating superusers Creating superusers
------------------- -------------------
:djadmin:`manage.py migrate <migrate>` prompts you to create a superuser the Create superusers using the :djadmin:`createsuperuser` command::
first time you run it with ``'django.contrib.auth'`` installed. If you need to
create a superuser at a later date, you can use a command line utility::
$ python manage.py createsuperuser --username=joe --email=joe@example.com $ python manage.py createsuperuser --username=joe --email=joe@example.com