1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #24332 -- Fixed contrib.sites create_default_site() when 'default' DATABASES is empty.

This commit is contained in:
Tim Graham 2015-02-12 13:58:37 -05:00
parent bd4afef984
commit e8cf4f8abe
4 changed files with 34 additions and 4 deletions

View File

@ -17,7 +17,7 @@ def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT
if not router.allow_migrate(using, Site): if not router.allow_migrate(using, Site):
return return
if not Site.objects.exists(): if not Site.objects.using(using).exists():
# The default settings set SITE_ID = 1, and some tests in Django's test # The default settings set SITE_ID = 1, and some tests in Django's test
# suite rely on this value. However, if database sequences are reused # suite rely on this value. However, if database sequences are reused
# (e.g. in the test suite after flush/syncdb), it isn't guaranteed that # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that

View File

@ -92,12 +92,13 @@ def clear_site_cache(sender, **kwargs):
Clears the cache (if primed) each time a site is saved or deleted Clears the cache (if primed) each time a site is saved or deleted
""" """
instance = kwargs['instance'] instance = kwargs['instance']
using = kwargs['using']
try: try:
del SITE_CACHE[instance.pk] del SITE_CACHE[instance.pk]
except KeyError: except KeyError:
pass pass
try: try:
del SITE_CACHE[Site.objects.get(pk=instance.pk).domain] del SITE_CACHE[Site.objects.using(using).get(pk=instance.pk).domain]
except (KeyError, Site.DoesNotExist): except (KeyError, Site.DoesNotExist):
pass pass
pre_save.connect(clear_site_cache, sender=Site) pre_save.connect(clear_site_cache, sender=Site)

View File

@ -16,3 +16,6 @@ Bugfixes
* Fixed a regression that prevented custom fields inheriting from * Fixed a regression that prevented custom fields inheriting from
``ManyToManyField`` from being recognized in migrations (:ticket:`24236`). ``ManyToManyField`` from being recognized in migrations (:ticket:`24236`).
* Fixed crash in ``contrib.sites`` migrations when a default database isn't
used (:ticket:`24332`).

View File

@ -17,6 +17,7 @@ from django.test.utils import captured_stdout
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}) @modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
class SitesFrameworkTests(TestCase): class SitesFrameworkTests(TestCase):
multi_db = True
def setUp(self): def setUp(self):
self.site = Site( self.site = Site(
@ -112,7 +113,26 @@ class SitesFrameworkTests(TestCase):
expected_cache.update({self.site.domain: self.site}) expected_cache.update({self.site.domain: self.site})
self.assertEqual(models.SITE_CACHE, expected_cache) self.assertEqual(models.SITE_CACHE, expected_cache)
clear_site_cache(Site, instance=self.site) clear_site_cache(Site, instance=self.site, using='default')
self.assertEqual(models.SITE_CACHE, {})
@override_settings(SITE_ID='')
def test_clear_site_cache_domain(self):
site = Site.objects.create(name='example2.com', domain='example2.com')
request = HttpRequest()
request.META = {
"SERVER_NAME": "example2.com",
"SERVER_PORT": "80",
}
get_current_site(request) # prime the models.SITE_CACHE
expected_cache = {site.domain: site}
self.assertEqual(models.SITE_CACHE, expected_cache)
# Site exists in 'default' database so using='other' shouldn't clear.
clear_site_cache(Site, instance=site, using='other')
self.assertEqual(models.SITE_CACHE, expected_cache)
# using='default' should clear.
clear_site_cache(Site, instance=site, using='default')
self.assertEqual(models.SITE_CACHE, {}) self.assertEqual(models.SITE_CACHE, {})
@ -146,7 +166,7 @@ class CreateDefaultSiteTests(TestCase):
self.assertEqual("", stdout.getvalue()) self.assertEqual("", stdout.getvalue())
@override_settings(DATABASE_ROUTERS=[JustOtherRouter()]) @override_settings(DATABASE_ROUTERS=[JustOtherRouter()])
def test_multi_db(self): def test_multi_db_with_router(self):
""" """
#16353, #16828 - The default site creation should respect db routing. #16353, #16828 - The default site creation should respect db routing.
""" """
@ -155,6 +175,12 @@ class CreateDefaultSiteTests(TestCase):
self.assertFalse(Site.objects.using('default').exists()) self.assertFalse(Site.objects.using('default').exists())
self.assertTrue(Site.objects.using('other').exists()) self.assertTrue(Site.objects.using('other').exists())
def test_multi_db(self):
create_default_site(self.app_config, using='default', verbosity=0)
create_default_site(self.app_config, using='other', verbosity=0)
self.assertTrue(Site.objects.using('default').exists())
self.assertTrue(Site.objects.using('other').exists())
def test_save_another(self): def test_save_another(self):
""" """
#17415 - Another site can be created right after the default one. #17415 - Another site can be created right after the default one.