2019-02-12 22:55:53 +00:00
|
|
|
from logging import getLogger
|
2025-05-03 13:40:04 +00:00
|
|
|
from typing import Optional
|
2017-07-20 14:06:41 +00:00
|
|
|
|
2022-04-26 13:09:10 +00:00
|
|
|
from django.http import HttpRequest
|
2019-02-22 23:22:11 +00:00
|
|
|
from django.utils.timezone import datetime, now
|
2017-07-20 14:06:41 +00:00
|
|
|
|
2025-05-03 13:40:04 +00:00
|
|
|
from axes.helpers import get_cool_off
|
2017-07-20 14:06:41 +00:00
|
|
|
|
2020-09-26 14:50:13 +00:00
|
|
|
log = getLogger(__name__)
|
2019-02-12 22:55:53 +00:00
|
|
|
|
2017-07-20 14:06:41 +00:00
|
|
|
|
2024-10-01 13:13:49 +00:00
|
|
|
def get_cool_off_threshold(request: Optional[HttpRequest] = None) -> datetime:
|
2019-02-13 12:50:50 +00:00
|
|
|
"""
|
2019-02-22 23:22:11 +00:00
|
|
|
Get threshold for fetching access attempts from the database.
|
2019-02-13 12:50:50 +00:00
|
|
|
"""
|
2017-07-20 14:06:41 +00:00
|
|
|
|
2024-10-01 13:02:12 +00:00
|
|
|
cool_off = get_cool_off(request)
|
2019-03-09 19:47:03 +00:00
|
|
|
if cool_off is None:
|
2019-09-28 16:27:50 +00:00
|
|
|
raise TypeError(
|
|
|
|
|
"Cool off threshold can not be calculated with settings.AXES_COOLOFF_TIME set to None"
|
|
|
|
|
)
|
2019-02-07 16:17:06 +00:00
|
|
|
|
2026-02-11 19:53:12 +00:00
|
|
|
attempt_time = request.axes_attempt_time # type: ignore[union-attr]
|
2019-03-09 19:47:03 +00:00
|
|
|
if attempt_time is None:
|
|
|
|
|
return now() - cool_off
|
|
|
|
|
return attempt_time - cool_off
|