Skip to content

Commit 8ed0d82

Browse files
browniebrokealeksihakli
authored andcommitted
refactor: remove attempt_time parameter
As we pass down the whole request, we no longer need to extract the axes_attempt_time anymore. This is a potential breaking change, but the impacted functions are not part of the documented API.
1 parent a304380 commit 8ed0d82

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

axes/attempts.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
log = getLogger(__name__)
1313

1414

15-
def get_cool_off_threshold(
16-
attempt_time: Optional[datetime] = None, request: Optional[HttpRequest] = None
17-
) -> datetime:
15+
def get_cool_off_threshold(request: Optional[HttpRequest] = None) -> datetime:
1816
"""
1917
Get threshold for fetching access attempts from the database.
2018
"""
@@ -25,6 +23,7 @@ def get_cool_off_threshold(
2523
"Cool off threshold can not be calculated with settings.AXES_COOLOFF_TIME set to None"
2624
)
2725

26+
attempt_time = request.axes_attempt_time
2827
if attempt_time is None:
2928
return now() - cool_off
3029
return attempt_time - cool_off
@@ -64,14 +63,12 @@ def get_user_attempts(
6463
)
6564
return attempts_list
6665

67-
threshold = get_cool_off_threshold(request.axes_attempt_time, request)
66+
threshold = get_cool_off_threshold(request)
6867
log.debug("AXES: Getting access attempts that are newer than %s", threshold)
6968
return [attempts.filter(attempt_time__gte=threshold) for attempts in attempts_list]
7069

7170

72-
def clean_expired_user_attempts(
73-
attempt_time: Optional[datetime] = None, request: Optional[HttpRequest] = None
74-
) -> int:
71+
def clean_expired_user_attempts(request: Optional[HttpRequest] = None) -> int:
7572
"""
7673
Clean expired user attempts from the database.
7774
"""
@@ -82,7 +79,7 @@ def clean_expired_user_attempts(
8279
)
8380
return 0
8481

85-
threshold = get_cool_off_threshold(attempt_time, request)
82+
threshold = get_cool_off_threshold(request)
8683
count, _ = AccessAttempt.objects.filter(attempt_time__lt=threshold).delete()
8784
log.info(
8885
"AXES: Cleaned up %s expired access attempts from database that were older than %s",

axes/handlers/database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def user_login_failed(self, sender, credentials: dict, request=None, **kwargs):
132132
return
133133

134134
# 1. database query: Clean up expired user attempts from the database before logging new attempts
135-
clean_expired_user_attempts(request.axes_attempt_time, request)
135+
clean_expired_user_attempts(request)
136136

137137
username = get_client_username(request, credentials)
138138
client_str = get_client_str(
@@ -262,7 +262,7 @@ def user_logged_in(self, sender, request, user, **kwargs):
262262
"""
263263

264264
# 1. database query: Clean up expired user attempts from the database
265-
clean_expired_user_attempts(request.axes_attempt_time, request)
265+
clean_expired_user_attempts(request)
266266

267267
username = user.get_username()
268268
credentials = get_credentials(username)
@@ -305,7 +305,7 @@ def user_logged_out(self, sender, request, user, **kwargs):
305305
"""
306306

307307
# 1. database query: Clean up expired user attempts from the database
308-
clean_expired_user_attempts(request.axes_attempt_time, request)
308+
clean_expired_user_attempts(request)
309309

310310
username = user.get_username() if user else None
311311
client_str = get_client_str(

tests/test_attempts.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import patch
22

33
from django.http import HttpRequest
4-
from django.test import override_settings
4+
from django.test import override_settings, RequestFactory
55
from django.utils.timezone import now
66

77
from axes.attempts import get_cool_off_threshold
@@ -15,12 +15,13 @@ class GetCoolOffThresholdTestCase(AxesTestCase):
1515
def test_get_cool_off_threshold(self):
1616
timestamp = now()
1717

18+
request = RequestFactory().post("/")
1819
with patch("axes.attempts.now", return_value=timestamp):
19-
attempt_time = timestamp
20-
threshold_now = get_cool_off_threshold(attempt_time)
20+
request.axes_attempt_time = timestamp
21+
threshold_now = get_cool_off_threshold(request)
2122

22-
attempt_time = None
23-
threshold_none = get_cool_off_threshold(attempt_time)
23+
request.axes_attempt_time = None
24+
threshold_none = get_cool_off_threshold(request)
2425

2526
self.assertEqual(threshold_now, threshold_none)
2627

0 commit comments

Comments
 (0)