Commit da16bb30 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Dropped AppCache._empty, _with_app and _without_app.

It's now easier to achieve the same effect with modify_settings or
override_settings.
parent 5241763c
Loading
Loading
Loading
Loading
+0 −56
Original line number Diff line number Diff line
"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):
+2 −3
Original line number Diff line number Diff line
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])
+2 −6
Original line number Diff line number Diff line
@@ -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."
+2 −7
Original line number Diff line number Diff line
@@ -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."
+32 −39
Original line number Diff line number Diff line
@@ -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,20 +219,17 @@ 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)],
        }
@@ -243,20 +240,16 @@ class BaseTests(object):
            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,
Loading