mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Dropped AppCache._empty, _with_app and _without_app.
It's now easier to achieve the same effect with modify_settings or override_settings.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"Utilities for loading models and the modules that contain them."
|
||||
|
||||
from collections import defaultdict, OrderedDict
|
||||
from contextlib import contextmanager
|
||||
import os
|
||||
import sys
|
||||
import warnings
|
||||
@@ -350,61 +349,6 @@ class AppCache(object):
|
||||
"""
|
||||
self.app_configs = self.stored_app_configs.pop()
|
||||
|
||||
### DANGEROUS METHODS ### (only used to preserve existing tests)
|
||||
|
||||
def _begin_with_app(self, app_name):
|
||||
# Returns an opaque value that can be passed to _end_with_app().
|
||||
app_config = AppConfig.create(app_name)
|
||||
if app_config.label in self.app_configs:
|
||||
return None
|
||||
else:
|
||||
app_config.import_models(self.all_models[app_config.label])
|
||||
self.app_configs[app_config.label] = app_config
|
||||
return app_config
|
||||
|
||||
def _end_with_app(self, app_config):
|
||||
if app_config is not None:
|
||||
del self.app_configs[app_config.label]
|
||||
|
||||
@contextmanager
|
||||
def _with_app(self, app_name):
|
||||
app_config = self._begin_with_app(app_name)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._end_with_app(app_config)
|
||||
|
||||
def _begin_without_app(self, app_name):
|
||||
# Returns an opaque value that can be passed to _end_without_app().
|
||||
return self.app_configs.pop(app_name.rpartition(".")[2], None)
|
||||
|
||||
def _end_without_app(self, app_config):
|
||||
if app_config is not None:
|
||||
self.app_configs[app_config.label] = app_config
|
||||
|
||||
@contextmanager
|
||||
def _without_app(self, app_name):
|
||||
app_config = self._begin_without_app(app_name)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._end_without_app(app_config)
|
||||
|
||||
def _begin_empty(self):
|
||||
app_configs, self.app_configs = self.app_configs, OrderedDict()
|
||||
return app_configs
|
||||
|
||||
def _end_empty(self, app_configs):
|
||||
self.app_configs = app_configs
|
||||
|
||||
@contextmanager
|
||||
def _empty(self):
|
||||
app_configs = self._begin_empty()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._end_empty(app_configs)
|
||||
|
||||
### DEPRECATED METHODS GO BELOW THIS LINE ###
|
||||
|
||||
def load_app(self, app_name):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.contenttypes.views import shortcut
|
||||
from django.contrib.sites.models import get_current_site
|
||||
@@ -222,12 +221,12 @@ class ContentTypesTests(TestCase):
|
||||
user_ct = ContentType.objects.get_for_model(FooWithUrl)
|
||||
obj = FooWithUrl.objects.create(name="john")
|
||||
|
||||
with app_cache._with_app('django.contrib.sites'):
|
||||
with self.modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'}):
|
||||
response = shortcut(request, user_ct.id, obj.id)
|
||||
self.assertEqual("http://%s/users/john/" % get_current_site(request).domain,
|
||||
response._headers.get("location")[1])
|
||||
|
||||
with app_cache._without_app('django.contrib.sites'):
|
||||
with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}):
|
||||
response = shortcut(request, user_ct.id, obj.id)
|
||||
self.assertEqual("http://Example.com/users/john/",
|
||||
response._headers.get("location")[1])
|
||||
|
||||
@@ -3,17 +3,17 @@ from __future__ import unicode_literals
|
||||
from unittest import skipUnless
|
||||
from xml.dom import minidom
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.contrib.sites.models import Site
|
||||
from django.contrib.gis.geos import HAS_GEOS
|
||||
from django.contrib.gis.tests.utils import HAS_SPATIAL_DB
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, modify_settings
|
||||
|
||||
if HAS_GEOS:
|
||||
from .models import City
|
||||
|
||||
|
||||
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
|
||||
@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.")
|
||||
class GeoFeedTest(TestCase):
|
||||
|
||||
@@ -21,10 +21,6 @@ class GeoFeedTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
||||
self._with_sites = app_cache._begin_with_app('django.contrib.sites')
|
||||
|
||||
def tearDown(self):
|
||||
app_cache._end_with_app(self._with_sites)
|
||||
|
||||
def assertChildNodes(self, elem, expected):
|
||||
"Taken from syndication/tests.py."
|
||||
|
||||
@@ -6,12 +6,11 @@ from xml.dom import minidom
|
||||
import os
|
||||
import zipfile
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.contrib.gis.geos import HAS_GEOS
|
||||
from django.contrib.gis.tests.utils import HAS_SPATIAL_DB
|
||||
from django.contrib.sites.models import Site
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, modify_settings
|
||||
from django.test.utils import IgnoreDeprecationWarningsMixin
|
||||
from django.utils._os import upath
|
||||
|
||||
@@ -19,6 +18,7 @@ if HAS_GEOS:
|
||||
from .models import City, Country
|
||||
|
||||
|
||||
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
|
||||
@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.")
|
||||
class GeoSitemapTest(IgnoreDeprecationWarningsMixin, TestCase):
|
||||
|
||||
@@ -27,11 +27,6 @@ class GeoSitemapTest(IgnoreDeprecationWarningsMixin, TestCase):
|
||||
def setUp(self):
|
||||
super(GeoSitemapTest, self).setUp()
|
||||
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
||||
self._with_sites = app_cache._begin_with_app('django.contrib.sites')
|
||||
|
||||
def tearDown(self):
|
||||
app_cache._end_with_app(self._with_sites)
|
||||
super(GeoSitemapTest, self).tearDown()
|
||||
|
||||
def assertChildNodes(self, elem, expected):
|
||||
"Taken from syndication/tests.py."
|
||||
|
||||
@@ -2,14 +2,14 @@ from unittest import skipUnless
|
||||
|
||||
from django import http
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings, global_settings
|
||||
from django.conf import global_settings
|
||||
from django.contrib.messages import constants, utils, get_level, set_level
|
||||
from django.contrib.messages.api import MessageFailure
|
||||
from django.contrib.messages.constants import DEFAULT_LEVELS
|
||||
from django.contrib.messages.storage import default_storage, base
|
||||
from django.contrib.messages.storage.base import Message
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test.utils import override_settings
|
||||
from django.test.utils import modify_settings, override_settings
|
||||
from django.utils.translation import ugettext_lazy
|
||||
|
||||
|
||||
@@ -219,55 +219,48 @@ class BaseTests(object):
|
||||
for msg in data['messages']:
|
||||
self.assertContains(response, msg)
|
||||
|
||||
@override_settings(
|
||||
MIDDLEWARE_CLASSES=filter(
|
||||
lambda m: 'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES),
|
||||
TEMPLATE_CONTEXT_PROCESSORS=filter(
|
||||
lambda p: 'context_processors.messages' not in p,
|
||||
settings.TEMPLATE_CONTEXT_PROCESSORS),
|
||||
MESSAGE_LEVEL=constants.DEBUG
|
||||
@modify_settings(
|
||||
INSTALLED_APPS={'remove': 'django.contrib.messages'},
|
||||
MIDDLEWARE_CLASSES={'remove': 'django.contrib.messages.middleware.MessageMiddleware'},
|
||||
TEMPLATE_CONTEXT_PROCESSORS={'remove': 'django.contrib.messages.context_processors.messages'},
|
||||
)
|
||||
@override_settings(MESSAGE_LEVEL=constants.DEBUG)
|
||||
def test_middleware_disabled(self):
|
||||
"""
|
||||
Tests that, when the middleware is disabled, an exception is raised
|
||||
when one attempts to store a message.
|
||||
"""
|
||||
with app_cache._without_app('django.contrib.messages'):
|
||||
data = {
|
||||
'messages': ['Test message %d' % x for x in range(5)],
|
||||
}
|
||||
reverse('django.contrib.messages.tests.urls.show')
|
||||
for level in ('debug', 'info', 'success', 'warning', 'error'):
|
||||
add_url = reverse('django.contrib.messages.tests.urls.add',
|
||||
args=(level,))
|
||||
self.assertRaises(MessageFailure, self.client.post, add_url,
|
||||
data, follow=True)
|
||||
data = {
|
||||
'messages': ['Test message %d' % x for x in range(5)],
|
||||
}
|
||||
reverse('django.contrib.messages.tests.urls.show')
|
||||
for level in ('debug', 'info', 'success', 'warning', 'error'):
|
||||
add_url = reverse('django.contrib.messages.tests.urls.add',
|
||||
args=(level,))
|
||||
self.assertRaises(MessageFailure, self.client.post, add_url,
|
||||
data, follow=True)
|
||||
|
||||
@override_settings(
|
||||
MIDDLEWARE_CLASSES=filter(
|
||||
lambda m: 'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES),
|
||||
TEMPLATE_CONTEXT_PROCESSORS=filter(
|
||||
lambda p: 'context_processors.messages' not in p,
|
||||
settings.TEMPLATE_CONTEXT_PROCESSORS),
|
||||
MESSAGE_LEVEL=constants.DEBUG
|
||||
@modify_settings(
|
||||
INSTALLED_APPS={'remove': 'django.contrib.messages'},
|
||||
MIDDLEWARE_CLASSES={'remove': 'django.contrib.messages.middleware.MessageMiddleware'},
|
||||
TEMPLATE_CONTEXT_PROCESSORS={'remove': 'django.contrib.messages.context_processors.messages'},
|
||||
)
|
||||
def test_middleware_disabled_fail_silently(self):
|
||||
"""
|
||||
Tests that, when the middleware is disabled, an exception is not
|
||||
raised if 'fail_silently' = True
|
||||
"""
|
||||
with app_cache._without_app('django.contrib.messages'):
|
||||
data = {
|
||||
'messages': ['Test message %d' % x for x in range(5)],
|
||||
'fail_silently': True,
|
||||
}
|
||||
show_url = reverse('django.contrib.messages.tests.urls.show')
|
||||
for level in ('debug', 'info', 'success', 'warning', 'error'):
|
||||
add_url = reverse('django.contrib.messages.tests.urls.add',
|
||||
args=(level,))
|
||||
response = self.client.post(add_url, data, follow=True)
|
||||
self.assertRedirects(response, show_url)
|
||||
self.assertFalse('messages' in response.context)
|
||||
data = {
|
||||
'messages': ['Test message %d' % x for x in range(5)],
|
||||
'fail_silently': True,
|
||||
}
|
||||
show_url = reverse('django.contrib.messages.tests.urls.show')
|
||||
for level in ('debug', 'info', 'success', 'warning', 'error'):
|
||||
add_url = reverse('django.contrib.messages.tests.urls.add',
|
||||
args=(level,))
|
||||
response = self.client.post(add_url, data, follow=True)
|
||||
self.assertRedirects(response, show_url)
|
||||
self.assertFalse('messages' in response.context)
|
||||
|
||||
def stored_messages_count(self, storage, response):
|
||||
"""
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
from django import http
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.contrib.sites.models import Site
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import TestCase
|
||||
from django.test.utils import override_settings
|
||||
from django.test.utils import modify_settings, override_settings
|
||||
from django.utils import six
|
||||
|
||||
from .middleware import RedirectFallbackMiddleware
|
||||
from .models import Redirect
|
||||
|
||||
|
||||
@override_settings(
|
||||
APPEND_SLASH=False,
|
||||
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
|
||||
['django.contrib.redirects.middleware.RedirectFallbackMiddleware'],
|
||||
SITE_ID=1,
|
||||
)
|
||||
@modify_settings(MIDDLEWARE_CLASSES={'append':
|
||||
'django.contrib.redirects.middleware.RedirectFallbackMiddleware'})
|
||||
@override_settings(APPEND_SLASH=False, SITE_ID=1)
|
||||
class RedirectTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@@ -57,10 +53,10 @@ class RedirectTests(TestCase):
|
||||
response = self.client.get('/initial')
|
||||
self.assertEqual(response.status_code, 410)
|
||||
|
||||
@modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'})
|
||||
def test_sites_not_installed(self):
|
||||
with app_cache._without_app('django.contrib.sites'):
|
||||
with self.assertRaises(ImproperlyConfigured):
|
||||
RedirectFallbackMiddleware()
|
||||
with self.assertRaises(ImproperlyConfigured):
|
||||
RedirectFallbackMiddleware()
|
||||
|
||||
|
||||
class OverriddenRedirectFallbackMiddleware(RedirectFallbackMiddleware):
|
||||
@@ -69,11 +65,9 @@ class OverriddenRedirectFallbackMiddleware(RedirectFallbackMiddleware):
|
||||
response_redirect_class = http.HttpResponseRedirect
|
||||
|
||||
|
||||
@override_settings(
|
||||
MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
|
||||
['django.contrib.redirects.tests.OverriddenRedirectFallbackMiddleware'],
|
||||
SITE_ID=1,
|
||||
)
|
||||
@modify_settings(MIDDLEWARE_CLASSES={'append':
|
||||
'django.contrib.redirects.tests.OverriddenRedirectFallbackMiddleware'})
|
||||
@override_settings(SITE_ID=1)
|
||||
class OverriddenRedirectMiddlewareTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
@@ -9,7 +9,7 @@ from django.conf import settings
|
||||
from django.contrib.sitemaps import Sitemap, GenericSitemap
|
||||
from django.contrib.sites.models import Site
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test.utils import override_settings
|
||||
from django.test import modify_settings, override_settings
|
||||
from django.utils.formats import localize
|
||||
from django.utils._os import upath
|
||||
from django.utils.translation import activate, deactivate
|
||||
@@ -106,17 +106,17 @@ class HTTPSitemapTests(SitemapTestsBase):
|
||||
self.assertContains(response, '<lastmod>%s</lastmod>' % date.today())
|
||||
deactivate()
|
||||
|
||||
@modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'})
|
||||
def test_requestsite_sitemap(self):
|
||||
# Make sure hitting the flatpages sitemap without the sites framework
|
||||
# installed doesn't raise an exception.
|
||||
with app_cache._without_app('django.contrib.sites'):
|
||||
response = self.client.get('/simple/sitemap.xml')
|
||||
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
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">
|
||||
<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
|
||||
</urlset>
|
||||
""" % date.today()
|
||||
self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
|
||||
self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
|
||||
|
||||
@skipUnless(app_cache.has_app('django.contrib.sites'),
|
||||
"django.contrib.sites app not installed.")
|
||||
@@ -128,14 +128,14 @@ class HTTPSitemapTests(SitemapTestsBase):
|
||||
Site.objects.all().delete()
|
||||
self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
|
||||
|
||||
@modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'})
|
||||
def test_sitemap_get_urls_no_site_2(self):
|
||||
"""
|
||||
Check we get ImproperlyConfigured when we don't pass a site object to
|
||||
Sitemap.get_urls if Site objects exists, but the sites framework is not
|
||||
actually installed.
|
||||
"""
|
||||
with app_cache._without_app('django.contrib.sites'):
|
||||
self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
|
||||
self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
|
||||
|
||||
def test_sitemap_item(self):
|
||||
"""
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.contrib.sites.models import Site, RequestSite, get_current_site
|
||||
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
||||
from django.http import HttpRequest
|
||||
from django.test import TestCase
|
||||
from django.test.utils import override_settings
|
||||
from django.test.utils import modify_settings, override_settings
|
||||
|
||||
|
||||
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.sites'})
|
||||
class SitesFrameworkTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
|
||||
self._with_sites = app_cache._begin_with_app('django.contrib.sites')
|
||||
|
||||
def tearDown(self):
|
||||
app_cache._end_with_app(self._with_sites)
|
||||
|
||||
def test_save_another(self):
|
||||
# Regression for #17415
|
||||
@@ -67,7 +63,7 @@ class SitesFrameworkTests(TestCase):
|
||||
self.assertRaises(ObjectDoesNotExist, get_current_site, request)
|
||||
|
||||
# A RequestSite is returned if the sites framework is not installed
|
||||
with app_cache._without_app('django.contrib.sites'):
|
||||
with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}):
|
||||
site = get_current_site(request)
|
||||
self.assertTrue(isinstance(site, RequestSite))
|
||||
self.assertEqual(site.name, "example.com")
|
||||
|
||||
Reference in New Issue
Block a user