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.
This commit is contained in:
Aymeric Augustin 2013-12-16 19:30:33 +01:00
parent 0242c56fd8
commit 7eea9bf303
7 changed files with 34 additions and 23 deletions

View File

@ -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])

View File

@ -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."

View File

@ -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):

View File

@ -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

View File

@ -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 = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@ -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):

View File

@ -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")

View File

@ -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()