From b54019fa0f3307fe0641329ffc737964f05a6699 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Wed, 2 Oct 2024 11:39:01 -0300 Subject: [PATCH] Change AXES_COOLOFF_TIME callable to take exactly 1 argument --- axes/helpers.py | 16 +++------------- tests/test_helpers.py | 4 ++-- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/axes/helpers.py b/axes/helpers.py index 021b35ef..198460f8 100644 --- a/axes/helpers.py +++ b/axes/helpers.py @@ -1,5 +1,3 @@ -import functools -import inspect from datetime import timedelta from hashlib import sha256 from logging import getLogger @@ -58,7 +56,7 @@ def get_cool_off(request: Optional[HttpRequest] = None) -> Optional[timedelta]: The return value is either None or timedelta. Notice that the settings.AXES_COOLOFF_TIME is either None, timedelta, integer/float of hours, - a path to a callable or a callable taking zero or 1 argument (the request). This function + a path to a callable or a callable taking 1 argument (the request). This function offers a unified _timedelta or None_ representation of that configuration for use with the Axes internal implementations. @@ -73,21 +71,13 @@ def get_cool_off(request: Optional[HttpRequest] = None) -> Optional[timedelta]: return timedelta(minutes=cool_off * 60) if isinstance(cool_off, str): cool_off_func = import_string(cool_off) - return _maybe_partial(cool_off_func, request)() + return cool_off_func(request) if callable(cool_off): - return _maybe_partial(cool_off, request)() # pylint: disable=not-callable + return cool_off(request) # pylint: disable=not-callable return cool_off -def _maybe_partial(func: Callable, request: Optional[HttpRequest] = None): - """Bind the given request to the function if it accepts a single argument.""" - sig = inspect.signature(func) - if len(sig.parameters) == 1: - return functools.partial(func, request) - return func - - def get_cool_off_iso8601(delta: timedelta) -> str: """ Return datetime.timedelta translated to ISO 8601 formatted duration for use in e.g. cool offs. diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 13c9aec0..1d5c8aa4 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -947,7 +947,7 @@ def test_get_lockout_response_lockout_response(self): self.assertEqual(type(response), HttpResponse) -def mock_get_cool_off_str(): +def mock_get_cool_off_str(req): return timedelta(seconds=30) @@ -972,7 +972,7 @@ def test_get_cool_off_float_lt_0(self): def test_get_cool_off_float_gt_0(self): self.assertEqual(get_cool_off(), timedelta(seconds=6120)) - @override_settings(AXES_COOLOFF_TIME=lambda: timedelta(seconds=30)) + @override_settings(AXES_COOLOFF_TIME=lambda r: timedelta(seconds=30)) def test_get_cool_off_callable(self): self.assertEqual(get_cool_off(), timedelta(seconds=30))