Updated get_individual_attempt_expiry() func placement & renamed to get_attempt_expiration()

This commit is contained in:
kuldeepkhatke 2025-06-08 14:31:08 +05:30 committed by Aleksi Häkli
parent d8e6c939fe
commit 01ccf5b213
3 changed files with 20 additions and 20 deletions

View file

@ -24,19 +24,3 @@ def get_cool_off_threshold(request: Optional[HttpRequest] = None) -> datetime:
if attempt_time is None:
return now() - cool_off
return attempt_time - cool_off
def get_individual_attempt_expiry(request: Optional[HttpRequest] = None) -> datetime:
"""
Get threshold for fetching access attempts from the database.
"""
cool_off = get_cool_off(request)
if cool_off is None:
raise TypeError(
"Cool off threshold can not be calculated with settings.AXES_COOLOFF_TIME set to None"
)
attempt_time = request.axes_attempt_time
if attempt_time is None:
return now() + cool_off
return attempt_time + cool_off

View file

@ -7,7 +7,7 @@ from django.db.models.functions import Concat
from django.http import HttpRequest
from django.utils import timezone
from axes.attempts import get_cool_off_threshold, get_individual_attempt_expiry
from axes.attempts import get_cool_off_threshold
from axes.conf import settings
from axes.handlers.base import AbstractAxesHandler, AxesBaseHandler
from axes.helpers import (
@ -19,6 +19,7 @@ from axes.helpers import (
get_failure_limit,
get_lockout_parameters,
get_query_str,
get_attempt_expiration,
)
from axes.models import AccessAttempt, AccessAttemptExpiration, AccessFailureLog, AccessLog
from axes.signals import user_locked_out
@ -226,11 +227,11 @@ class AxesDatabaseHandler(AbstractAxesHandler, AxesBaseHandler):
)
attempt.expiration = AccessAttemptExpiration.objects.create(
access_attempt=attempt,
expires_at=get_individual_attempt_expiry(request)
expires_at=get_attempt_expiration(request)
)
else:
attempt.expiration.expires_at = max(
get_individual_attempt_expiry(request), attempt.expiration.expires_at
get_attempt_expiration(request), attempt.expiration.expires_at
)
attempt.expiration.save()

View file

@ -1,4 +1,4 @@
from datetime import timedelta
from datetime import timedelta, datetime
from hashlib import sha256
from logging import getLogger
from string import Template
@ -100,6 +100,21 @@ def get_cool_off_iso8601(delta: timedelta) -> str:
return f"P{days_str}T{time_str}"
return f"P{days_str}"
def get_attempt_expiration(request: Optional[HttpRequest] = None) -> datetime:
"""
Get threshold for fetching access attempts from the database.
"""
cool_off = get_cool_off(request)
if cool_off is None:
raise TypeError(
"Cool off threshold can not be calculated with settings.AXES_COOLOFF_TIME set to None"
)
attempt_time = request.axes_attempt_time
if attempt_time is None:
return datetime.now() + cool_off
return attempt_time + cool_off
def get_credentials(username: Optional[str] = None, **kwargs) -> dict:
"""