a
    Sicz                     @   s   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZ eeZdee
 e
d
ddZdeee ee dddZdeee ee dddZdee
 ed
ddZdeee edddZd	S )    )	getLogger)ListOptional)QuerySet)HttpRequest)datetimenow)settings)get_client_usernameget_client_parametersget_cool_off)AccessAttemptN)attempt_timereturnc                 C   s0   t  }|du rtd| du r(t | S | | S )zG
    Get threshold for fetching access attempts from the database.
    NzTCool off threshold can not be calculated with settings.AXES_COOLOFF_TIME set to None)r   	TypeErrorr   )r   cool_off r   I/var/www/html/django/DPS/env/lib/python3.9/site-packages/axes/attempts.pyget_cool_off_threshold   s    
r   )requestcredentialsr   c                 C   s,   t | |}t|| j| j}dd |D }|S )za
    Return a list querysets of AccessAttempts that match the given request and credentials.
    c                 S   s   g | ]}t jjf i |qS r   )r   objectsfilter).0filter_kwargsr   r   r   
<listcomp>+   s   z(filter_user_attempts.<locals>.<listcomp>)r
   r   axes_ip_addressaxes_user_agent)r   r   usernamefilter_kwargs_listattempts_listr   r   r   filter_user_attempts   s    

r!   c                    sJ   t | |}tjdu r"td |S t| j td   fdd|D S )zf
    Get list of querysets with valid user attempts that match the given request and credentials.
    NzZAXES: Getting all access attempts from database because no AXES_COOLOFF_TIME is configuredz4AXES: Getting access attempts that are newer than %sc                    s   g | ]}|j  d qS ))Zattempt_time__gte)r   )r   attempts	thresholdr   r   r   C       z%get_user_attempts.<locals>.<listcomp>)r!   r	   AXES_COOLOFF_TIMElogdebugr   axes_attempt_time)r   r   r    r   r#   r   get_user_attempts2   s    


r*   c                 C   sH   t jdu rtd dS t| }tjj|d \}}t	d|| |S )z8
    Clean expired user attempts from the database.
    Nz[AXES: Skipping clean for expired access attempts because no AXES_COOLOFF_TIME is configuredr   )Zattempt_time__ltzQAXES: Cleaned up %s expired access attempts from database that were older than %s)
r	   r&   r'   r(   r   r   r   r   deleteinfo)r   r$   count_r   r   r   clean_expired_user_attemptsF   s    
r/   c                 C   s<   t | |}d}|D ]}| \}}||7 }qtd| |S )zO
    Reset all user attempts that match the given request and credentials.
    r   z-AXES: Reset %s access attempts from database.)r!   r+   r'   r,   )r   r   r    r-   r"   _countr.   r   r   r   reset_user_attempts[   s    

r1   )N)N)N)N)N)loggingr   typingr   r   django.db.modelsr   django.httpr   django.utils.timezoner   r   	axes.confr	   axes.helpersr
   r   r   axes.modelsr   __name__r'   r   dictr!   r*   intr/   r1   r   r   r   r   <module>   s2      