Fixed #25034 -- Converted caches ImproperlyConfigured error to a system check.

This commit is contained in:
Tom Christie 2015-09-02 13:20:46 +01:00 committed by Tim Graham
parent f33b3ebd53
commit b02f08e02c
6 changed files with 65 additions and 4 deletions

View File

@ -19,7 +19,6 @@ from django.core import signals
from django.core.cache.backends.base import ( from django.core.cache.backends.base import (
BaseCache, CacheKeyWarning, InvalidCacheBackendError, BaseCache, CacheKeyWarning, InvalidCacheBackendError,
) )
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
__all__ = [ __all__ = [
@ -29,9 +28,6 @@ __all__ = [
DEFAULT_CACHE_ALIAS = 'default' DEFAULT_CACHE_ALIAS = 'default'
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
raise ImproperlyConfigured("You must define a '%s' cache" % DEFAULT_CACHE_ALIAS)
def _create_cache(backend, **kwargs): def _create_cache(backend, **kwargs):
try: try:

View File

@ -8,6 +8,7 @@ from .messages import (
from .registry import Tags, register, run_checks, tag_exists from .registry import Tags, register, run_checks, tag_exists
# Import these to force registration of checks # Import these to force registration of checks
import django.core.checks.caches # NOQA isort:skip
import django.core.checks.compatibility.django_1_8_0 # NOQA isort:skip import django.core.checks.compatibility.django_1_8_0 # NOQA isort:skip
import django.core.checks.model_checks # NOQA isort:skip import django.core.checks.model_checks # NOQA isort:skip
import django.core.checks.security.base # NOQA isort:skip import django.core.checks.security.base # NOQA isort:skip

View File

@ -0,0 +1,18 @@
from __future__ import unicode_literals
from django.conf import settings
from django.core.cache import DEFAULT_CACHE_ALIAS
from . import Error, Tags, register
E001 = Error(
"You must define a '%s' cache in your CACHES setting." % DEFAULT_CACHE_ALIAS,
id='caches.E001',
)
@register(Tags.caches)
def check_default_cache_is_configured(app_configs, **kwargs):
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
return [E001]
return []

View File

@ -11,6 +11,7 @@ class Tags(object):
Built-in tags for internal checks. Built-in tags for internal checks.
""" """
admin = 'admin' admin = 'admin'
caches = 'caches'
compatibility = 'compatibility' compatibility = 'compatibility'
models = 'models' models = 'models'
security = 'security' security = 'security'

View File

@ -80,6 +80,7 @@ Django's system checks are organized using the following tags:
* ``compatibility``: Flagging potential problems with version upgrades. * ``compatibility``: Flagging potential problems with version upgrades.
* ``security``: Checks security related configuration. * ``security``: Checks security related configuration.
* ``templates``: Checks template related configuration. * ``templates``: Checks template related configuration.
* ``caches``: Checks cache related configuration.
Some checks may be registered with multiple tags. Some checks may be registered with multiple tags.
@ -569,3 +570,12 @@ configured:
* **templates.E001**: You have ``'APP_DIRS': True`` in your * **templates.E001**: You have ``'APP_DIRS': True`` in your
:setting:`TEMPLATES` but also specify ``'loaders'`` in ``OPTIONS``. Either :setting:`TEMPLATES` but also specify ``'loaders'`` in ``OPTIONS``. Either
remove ``APP_DIRS`` or remove the ``'loaders'`` option. remove ``APP_DIRS`` or remove the ``'loaders'`` option.
Caches
------
The following checks verify that your :setting:`CACHES` setting is correctly
configured:
* **caches.E001**: You must define a ``'default'`` cache in your
:setting:`CACHES` setting.

View File

@ -0,0 +1,35 @@
from django.core.checks.caches import E001
from django.test import SimpleTestCase
from django.test.utils import override_settings
class CheckCacheSettingsAppDirsTest(SimpleTestCase):
VALID_CACHES_CONFIGURATION = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
}
INVALID_CACHES_CONFIGURATION = {
'other': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
}
@property
def func(self):
from django.core.checks.caches import check_default_cache_is_configured
return check_default_cache_is_configured
@override_settings(CACHES=VALID_CACHES_CONFIGURATION)
def test_default_cache_included(self):
"""
Don't error if 'default' is present in CACHES setting.
"""
self.assertEqual(self.func(None), [])
@override_settings(CACHES=INVALID_CACHES_CONFIGURATION)
def test_default_cache_not_included(self):
"""
Error if 'default' not present in CACHES setting.
"""
self.assertEqual(self.func(None), [E001])