From 7eea9bf30399e9a49fe164851c6132cd19a88e08 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 16 Dec 2013 19:30:33 +0100 Subject: [PATCH] Normalized Model._meta.installed. Used the information from the app cache instead of creating a duplicate based on INSTALLED_APPS. Model._meta.installed is no longer writable. It was a rather sketchy way to alter private internals anyway. --- django/contrib/contenttypes/tests.py | 15 ++++++++------- django/contrib/gis/tests/geoapp/test_feeds.py | 6 +++--- django/contrib/gis/tests/geoapp/test_sitemaps.py | 6 +++--- django/contrib/sitemaps/tests/base.py | 4 ++-- django/contrib/sitemaps/tests/test_http.py | 8 +++++--- django/contrib/sites/tests.py | 8 ++++---- django/db/models/options.py | 10 +++++++++- 7 files changed, 34 insertions(+), 23 deletions(-) diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py index 8a0010061e..756430f393 100644 --- a/django/contrib/contenttypes/tests.py +++ b/django/contrib/contenttypes/tests.py @@ -52,12 +52,13 @@ class FooWithBrokenAbsoluteUrl(FooWithoutUrl): class ContentTypesTests(TestCase): + def setUp(self): - self.old_Site_meta_installed = Site._meta.installed + self._old_installed = Site._meta.app_config.installed ContentType.objects.clear_cache() def tearDown(self): - Site._meta.installed = self.old_Site_meta_installed + Site._meta.app_config.installed = self._old_installed ContentType.objects.clear_cache() def test_lookup_cache(self): @@ -222,12 +223,12 @@ class ContentTypesTests(TestCase): user_ct = ContentType.objects.get_for_model(FooWithUrl) obj = FooWithUrl.objects.create(name="john") - if Site._meta.installed: - response = shortcut(request, user_ct.id, obj.id) - self.assertEqual("http://%s/users/john/" % get_current_site(request).domain, - response._headers.get("location")[1]) + Site._meta.app_config.installed = True + response = shortcut(request, user_ct.id, obj.id) + self.assertEqual("http://%s/users/john/" % get_current_site(request).domain, + response._headers.get("location")[1]) - Site._meta.installed = False + Site._meta.app_config.installed = False response = shortcut(request, user_ct.id, obj.id) self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1]) diff --git a/django/contrib/gis/tests/geoapp/test_feeds.py b/django/contrib/gis/tests/geoapp/test_feeds.py index 7b8fc159b4..0817e65cb4 100644 --- a/django/contrib/gis/tests/geoapp/test_feeds.py +++ b/django/contrib/gis/tests/geoapp/test_feeds.py @@ -20,11 +20,11 @@ class GeoFeedTest(TestCase): def setUp(self): Site(id=settings.SITE_ID, domain="example.com", name="example.com").save() - self.old_Site_meta_installed = Site._meta.installed - Site._meta.installed = True + self._old_installed = Site._meta.app_config.installed + Site._meta.app_config.installed = True def tearDown(self): - Site._meta.installed = self.old_Site_meta_installed + Site._meta.app_config.installed = self._old_installed def assertChildNodes(self, elem, expected): "Taken from syndication/tests.py." diff --git a/django/contrib/gis/tests/geoapp/test_sitemaps.py b/django/contrib/gis/tests/geoapp/test_sitemaps.py index 0439a37644..ee9974ceaa 100644 --- a/django/contrib/gis/tests/geoapp/test_sitemaps.py +++ b/django/contrib/gis/tests/geoapp/test_sitemaps.py @@ -26,11 +26,11 @@ class GeoSitemapTest(IgnoreDeprecationWarningsMixin, TestCase): def setUp(self): super(GeoSitemapTest, self).setUp() Site(id=settings.SITE_ID, domain="example.com", name="example.com").save() - self.old_Site_meta_installed = Site._meta.installed - Site._meta.installed = True + self._old_installed = Site._meta.app_config.installed + Site._meta.app_config.installed = True def tearDown(self): - Site._meta.installed = self.old_Site_meta_installed + Site._meta.app_config.installed = self._old_installed super(GeoSitemapTest, self).tearDown() def assertChildNodes(self, elem, expected): diff --git a/django/contrib/sitemaps/tests/base.py b/django/contrib/sitemaps/tests/base.py index 8e027d491e..98abb3dca4 100644 --- a/django/contrib/sitemaps/tests/base.py +++ b/django/contrib/sitemaps/tests/base.py @@ -25,10 +25,10 @@ class SitemapTestsBase(TestCase): def setUp(self): self.base_url = '%s://%s' % (self.protocol, self.domain) - self.old_Site_meta_installed = Site._meta.installed + self._old_installed = Site._meta.app_config.installed cache.clear() # Create an object for sitemap content. TestModel.objects.create(name='Test Object') def tearDown(self): - Site._meta.installed = self.old_Site_meta_installed + Site._meta.app_config.installed = self._old_installed diff --git a/django/contrib/sitemaps/tests/test_http.py b/django/contrib/sitemaps/tests/test_http.py index fc6660af0a..61be8c842a 100644 --- a/django/contrib/sitemaps/tests/test_http.py +++ b/django/contrib/sitemaps/tests/test_http.py @@ -107,8 +107,9 @@ class HTTPSitemapTests(SitemapTestsBase): def test_requestsite_sitemap(self): # Make sure hitting the flatpages sitemap without the sites framework - # installed doesn't raise an exception - Site._meta.installed = False + # installed doesn't raise an exception. + # Reset by SitemapTestsBase.tearDown(). + Site._meta.app_config.installed = False response = self.client.get('/simple/sitemap.xml') expected_content = """ @@ -133,7 +134,8 @@ class HTTPSitemapTests(SitemapTestsBase): Sitemap.get_urls if Site objects exists, but the sites framework is not actually installed. """ - Site._meta.installed = False + # Reset by SitemapTestsBase.tearDown(). + Site._meta.app_config.installed = False self.assertRaises(ImproperlyConfigured, Sitemap().get_urls) def test_sitemap_item(self): diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py index 6bfbfd7cf2..2be28a1429 100644 --- a/django/contrib/sites/tests.py +++ b/django/contrib/sites/tests.py @@ -12,11 +12,11 @@ class SitesFrameworkTests(TestCase): def setUp(self): Site(id=settings.SITE_ID, domain="example.com", name="example.com").save() - self.old_Site_meta_installed = Site._meta.installed - Site._meta.installed = True + self._old_installed = Site._meta.app_config.installed + Site._meta.app_config.installed = True def tearDown(self): - Site._meta.installed = self.old_Site_meta_installed + Site._meta.app_config.installed = self._old_installed def test_save_another(self): # Regression for #17415 @@ -67,7 +67,7 @@ class SitesFrameworkTests(TestCase): self.assertRaises(ObjectDoesNotExist, get_current_site, request) # A RequestSite is returned if the sites framework is not installed - Site._meta.installed = False + Site._meta.app_config.installed = False site = get_current_site(request) self.assertTrue(isinstance(site, RequestSite)) self.assertEqual(site.name, "example.com") diff --git a/django/db/models/options.py b/django/db/models/options.py index 386e90b057..cd68d5eae9 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -91,13 +91,21 @@ class Options(object): # A custom AppCache to use, if you're making a separate model set. self.app_cache = app_cache + @property + def app_config(self): + # Don't go through get_app_config to avoid triggering populate(). + return self.app_cache.app_configs[self.app_label] + + @property + def installed(self): + return self.app_config.installed + def contribute_to_class(self, cls, name): from django.db import connection from django.db.backends.utils import truncate_name cls._meta = self self.model = cls - self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS # First, construct the default values for these options. self.object_name = cls.__name__ self.model_name = self.object_name.lower()