Skip to content

Commit b54019f

Browse files
browniebrokealeksihakli
authored andcommitted
Change AXES_COOLOFF_TIME callable to take exactly 1 argument
1 parent 8ed0d82 commit b54019f

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

axes/helpers.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import functools
2-
import inspect
31
from datetime import timedelta
42
from hashlib import sha256
53
from logging import getLogger
@@ -58,7 +56,7 @@ def get_cool_off(request: Optional[HttpRequest] = None) -> Optional[timedelta]:
5856
The return value is either None or timedelta.
5957
6058
Notice that the settings.AXES_COOLOFF_TIME is either None, timedelta, integer/float of hours,
61-
a path to a callable or a callable taking zero or 1 argument (the request). This function
59+
a path to a callable or a callable taking 1 argument (the request). This function
6260
offers a unified _timedelta or None_ representation of that configuration for use with the
6361
Axes internal implementations.
6462
@@ -73,21 +71,13 @@ def get_cool_off(request: Optional[HttpRequest] = None) -> Optional[timedelta]:
7371
return timedelta(minutes=cool_off * 60)
7472
if isinstance(cool_off, str):
7573
cool_off_func = import_string(cool_off)
76-
return _maybe_partial(cool_off_func, request)()
74+
return cool_off_func(request)
7775
if callable(cool_off):
78-
return _maybe_partial(cool_off, request)() # pylint: disable=not-callable
76+
return cool_off(request) # pylint: disable=not-callable
7977

8078
return cool_off
8179

8280

83-
def _maybe_partial(func: Callable, request: Optional[HttpRequest] = None):
84-
"""Bind the given request to the function if it accepts a single argument."""
85-
sig = inspect.signature(func)
86-
if len(sig.parameters) == 1:
87-
return functools.partial(func, request)
88-
return func
89-
90-
9181
def get_cool_off_iso8601(delta: timedelta) -> str:
9282
"""
9383
Return datetime.timedelta translated to ISO 8601 formatted duration for use in e.g. cool offs.

tests/test_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ def test_get_lockout_response_lockout_response(self):
947947
self.assertEqual(type(response), HttpResponse)
948948

949949

950-
def mock_get_cool_off_str():
950+
def mock_get_cool_off_str(req):
951951
return timedelta(seconds=30)
952952

953953

@@ -972,7 +972,7 @@ def test_get_cool_off_float_lt_0(self):
972972
def test_get_cool_off_float_gt_0(self):
973973
self.assertEqual(get_cool_off(), timedelta(seconds=6120))
974974

975-
@override_settings(AXES_COOLOFF_TIME=lambda: timedelta(seconds=30))
975+
@override_settings(AXES_COOLOFF_TIME=lambda r: timedelta(seconds=30))
976976
def test_get_cool_off_callable(self):
977977
self.assertEqual(get_cool_off(), timedelta(seconds=30))
978978

0 commit comments

Comments
 (0)