diff --git a/axes/checks.py b/axes/checks.py index c191db4..56f6283 100644 --- a/axes/checks.py +++ b/axes/checks.py @@ -4,14 +4,10 @@ from axes.conf import settings class Messages: - CACHE_MISSING = 'missing cache configuration for AXES_CACHE' CACHE_INVALID = 'invalid cache configuration for settings.AXES_CACHE' class Hints: - CACHE_MISSING = ( - 'django-axes needs to have a cache configured with settings.AXES_CACHE' - ) CACHE_INVALID = ( 'django-axes does not work properly with LocMemCache as the cache backend' ' please add e.g. a DummyCache backend and configure it with settings.AXES_CACHE' @@ -19,8 +15,7 @@ class Hints: class Codes: - CACHE_MISSING = 'axes.E001' - CACHE_INVALID = 'axes.E002' + CACHE_INVALID = 'axeslE001' @register(Tags.caches) @@ -35,20 +30,15 @@ def axes_cache_backend_check(app_configs, **kwargs): # pylint: disable=unused-a 'django.core.cache.backends.locmem.LocMemCache', ] - if not axes_cache_config: - errors.append(Error( - msg=Messages.CACHE_MISSING, - hint=Hints.CACHE_MISSING, - obj=settings.CACHES, - id=Codes.CACHE_MISSING, - )) + axes_handler = getattr(settings, 'AXES_HANDLER', '') - if axes_cache_backend in axes_cache_incompatible_backends: - errors.append(Error( - msg=Messages.CACHE_INVALID, - hint=Hints.CACHE_INVALID, - obj=settings.CACHES, - id=Codes.CACHE_INVALID, - )) + if axes_handler == 'axes.handlers.cache.AxesCacheHandler': + if axes_cache_backend in axes_cache_incompatible_backends: + errors.append(Error( + msg=Messages.CACHE_INVALID, + hint=Hints.CACHE_INVALID, + obj=settings.CACHES, + id=Codes.CACHE_INVALID, + )) return errors diff --git a/axes/handlers/cache.py b/axes/handlers/cache.py index 43eca0c..0557b98 100644 --- a/axes/handlers/cache.py +++ b/axes/handlers/cache.py @@ -5,7 +5,8 @@ from axes.exceptions import AxesSignalPermissionDenied from axes.handlers.base import AxesBaseHandler from axes.signals import user_locked_out from axes.utils import ( - get_axes_cache, + get_cache, + get_cache_timeout, get_client_cache_key, get_client_ip_address, get_client_path_info, @@ -13,7 +14,6 @@ from axes.utils import ( get_client_username, get_client_user_agent, get_credentials, - get_cool_off, ) log = getLogger(settings.AXES_LOGGER) @@ -25,8 +25,8 @@ class AxesCacheHandler(AxesBaseHandler): # pylint: disable=too-many-locals """ def __init__(self): - self.cache = get_axes_cache() - self.cache_timeout = get_cool_off().total_seconds() + self.cache = get_cache() + self.cache_timeout = get_cache_timeout() def get_failures(self, request, credentials=None, attempt_time=None) -> int: cache_key = get_client_cache_key(request, credentials) diff --git a/axes/tests/base.py b/axes/tests/base.py index 05186df..2e75973 100644 --- a/axes/tests/base.py +++ b/axes/tests/base.py @@ -9,7 +9,7 @@ from django.urls import reverse from axes.attempts import reset from axes.conf import settings -from axes.utils import get_axes_cache, get_cool_off, get_credentials +from axes.utils import get_cache, get_cool_off, get_credentials from axes.models import AccessLog, AccessAttempt @@ -64,7 +64,7 @@ class AxesTestCase(TestCase): self.credentials = get_credentials(self.username) def tearDown(self): - get_axes_cache().clear() + get_cache().clear() def get_kwargs_with_defaults(self, **kwargs): defaults = { diff --git a/axes/tests/test_checks.py b/axes/tests/test_checks.py index 217f04c..7916098 100644 --- a/axes/tests/test_checks.py +++ b/axes/tests/test_checks.py @@ -8,20 +8,7 @@ from axes.tests.base import AxesTestCase class CacheCheckTestCase(AxesTestCase): @override_settings( - AXES_CACHE='nonexistent', - ) - def test_cache_missing_produces_check_error(self): - errors = run_checks() - error = Error( - msg=Messages.CACHE_MISSING, - hint=Hints.CACHE_MISSING, - obj=settings.CACHES, - id=Codes.CACHE_MISSING, - ) - - self.assertIn(error, errors) - - @override_settings( + AXES_HANDLER='axes.handlers.cache.AxesCacheHandler', AXES_CACHE='axes', CACHES={ 'axes': { diff --git a/axes/utils.py b/axes/utils.py index 22458fc..292240d 100644 --- a/axes/utils.py +++ b/axes/utils.py @@ -16,7 +16,7 @@ from axes.conf import settings logger = getLogger(__name__) -def get_axes_cache() -> BaseCache: +def get_cache() -> BaseCache: """ Get the cache instance Axes is configured to use with ``settings.AXES_CACHE`` and use ``'default'`` if not set. """