Fixed #23641 -- Moved post_migrate signals for contrib apps to AppConfig.ready().

This commit is contained in:
wrwrwr 2014-11-25 13:35:39 +01:00 committed by Tim Graham
parent 7cd3f1c295
commit dd35cc232a
7 changed files with 30 additions and 15 deletions

View File

@ -1,13 +1,17 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.core import checks from django.core import checks
from django.contrib.auth.checks import check_user_model from django.contrib.auth.checks import check_user_model
from django.db.models.signals import post_migrate
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .management import create_permissions
class AuthConfig(AppConfig): class AuthConfig(AppConfig):
name = 'django.contrib.auth' name = 'django.contrib.auth'
verbose_name = _("Authentication and Authorization") verbose_name = _("Authentication and Authorization")
def ready(self): def ready(self):
post_migrate.connect(create_permissions,
dispatch_uid="django.contrib.auth.management.create_permissions")
checks.register(check_user_model, checks.Tags.models) checks.register(check_user_model, checks.Tags.models)

View File

@ -7,11 +7,10 @@ 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 get_permission_codename
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.utils.encoding import DEFAULT_LOCALE_ENCODING from django.utils.encoding import DEFAULT_LOCALE_ENCODING
from django.utils import six from django.utils import six
@ -149,6 +148,9 @@ def get_default_username(check_db=True):
:returns: The username, or an empty string if no username can be :returns: The username, or an empty string if no username can be
determined. determined.
""" """
# This file is used in apps.py, it should not trigger models import.
from django.contrib.auth import models as auth_app
# If the User model has been swapped out, we can't make any assumptions # If the User model has been swapped out, we can't make any assumptions
# about the default user name. # about the default user name.
if auth_app.User._meta.swapped: if auth_app.User._meta.swapped:
@ -177,7 +179,3 @@ def get_default_username(check_db=True):
else: else:
return '' return ''
return default_username return default_username
signals.post_migrate.connect(create_permissions,
dispatch_uid="django.contrib.auth.management.create_permissions")

View File

@ -1,12 +1,16 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.contrib.contenttypes.checks import check_generic_foreign_keys from django.contrib.contenttypes.checks import check_generic_foreign_keys
from django.core import checks from django.core import checks
from django.db.models.signals import post_migrate
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .management import update_contenttypes
class ContentTypesConfig(AppConfig): class ContentTypesConfig(AppConfig):
name = 'django.contrib.contenttypes' name = 'django.contrib.contenttypes'
verbose_name = _("Content Types") verbose_name = _("Content Types")
def ready(self): def ready(self):
post_migrate.connect(update_contenttypes)
checks.register(check_generic_foreign_keys, checks.Tags.models) checks.register(check_generic_foreign_keys, checks.Tags.models)

View File

@ -1,6 +1,5 @@
from django.apps import apps from django.apps import apps
from django.db import DEFAULT_DB_ALIAS, router from django.db import DEFAULT_DB_ALIAS, router
from django.db.models import signals
from django.utils.encoding import smart_text from django.utils.encoding import smart_text
from django.utils import six from django.utils import six
from django.utils.six.moves import input from django.utils.six.moves import input
@ -92,8 +91,5 @@ def update_all_contenttypes(**kwargs):
update_contenttypes(app_config, **kwargs) update_contenttypes(app_config, **kwargs)
signals.post_migrate.connect(update_contenttypes)
if __name__ == "__main__": if __name__ == "__main__":
update_all_contenttypes() update_all_contenttypes()

View File

@ -1,8 +1,14 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .management import create_default_site
class SitesConfig(AppConfig): class SitesConfig(AppConfig):
name = 'django.contrib.sites' name = 'django.contrib.sites'
verbose_name = _("Sites") verbose_name = _("Sites")
def ready(self):
post_migrate.connect(create_default_site, sender=self)

View File

@ -5,7 +5,6 @@ Creates the default Site object.
from django.apps import apps from django.apps import apps
from django.core.management.color import no_style from django.core.management.color import no_style
from django.db import DEFAULT_DB_ALIAS, connections, router from django.db import DEFAULT_DB_ALIAS, connections, router
from django.db.models import signals
def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_ALIAS, **kwargs): def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB_ALIAS, **kwargs):
@ -38,6 +37,3 @@ def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB
cursor.execute(command) cursor.execute(command)
Site.objects.clear_cache() Site.objects.clear_cache()
signals.post_migrate.connect(create_default_site, sender=apps.get_app_config('sites'))

View File

@ -1,7 +1,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.apps import apps
from django.conf import settings from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models.signals import post_migrate
from django.http import HttpRequest from django.http import HttpRequest
from django.test import TestCase, modify_settings, override_settings from django.test import TestCase, modify_settings, override_settings
@ -118,6 +120,15 @@ class SitesFrameworkTests(TestCase):
clear_site_cache(Site, instance=self.site) clear_site_cache(Site, instance=self.site)
self.assertEqual(models.SITE_CACHE, {}) self.assertEqual(models.SITE_CACHE, {})
def test_create_default_site_signal(self):
"""
Checks that sending ``post_migrate`` creates the default site.
"""
Site.objects.all().delete()
app_config = apps.get_app_config('sites')
post_migrate.send(sender=app_config, app_config=app_config, verbosity=0)
self.assertTrue(Site.objects.exists())
class MiddlewareTest(TestCase): class MiddlewareTest(TestCase):