o
    &zh                     @   s   d dl 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 d dlmZ d dlmZ d d	lmZ d d
lmZmZ e	dd ZdefddZedd ZdefddZdefddZdS )    N)SimpleNamespace)HttpResponseRedirect)NoReverseMatchreverse)sync_and_async_middleware)iscoroutinefunctionsync_to_async)get_adapter)flows)context)ImmediateHttpResponseReauthenticationRequiredc                    s4   t  r fdd}n fdd}dd }||_|S )Nc                    sb   t  | _t|   | I d H }t| |rt| I d H }|W  d    S 1 s*w   Y  d S N)r   allauthr   request_context_should_redirect_accounts_aredirect_accountsrequestresponseget_response U/var/www/html/kangema/venv/lib/python3.10/site-packages/allauth/account/middleware.py
middleware   s   
$z%AccountMiddleware.<locals>.middlewarec                    sT   t  | _t|   | }t| |rt| }|W  d    S 1 s#w   Y  d S r   )r   r   r   r   r   _redirect_accountsr   r   r   r   r      s   
$c                 S   sR   t |tr|jS t |tr'td}t | j}|r |d d }tj	
| |S d S )Naccount_loginr   url)
isinstancer   r   r   r   r	   get_reauthentication_methodsuserr
   reauthenticationsuspend_request)r   	exceptionredirect_urlmethodsr   r   r   process_exception&   s   

z,AccountMiddleware.<locals>.process_exception)r   r&   )r   r   r&   r   r   r   AccountMiddleware   s   

r'   returnc                 C   sf   |j dkrdS z
td}td}W n
 ty   Y dS w tj||g}t|dks/|| jkr1dS dS )a  
    URLs should be hackable. Yet, assuming allauth is included like this...

        path("accounts/", include("allauth.urls")),

    ... and a user would attempt to navigate to /accounts/, a 404 would be
    presented. This code catches that 404, and redirects to either the email
    management overview or the login page, depending on whether or not the user
    is authenticated.
    i  Fr   account_email   T)status_coder   r   ospathcommonprefixlen)r   r   
login_path
email_pathprefixr   r   r   r   4   s   
r   c                 C   s   | j S r   )r    )r   r   r   r   _async_get_userO   s   r3   c                    sN   t d}t d}t| dr|  I d H }nt| I d H }t|jr$|S |S )Nr)   r   auser)r   hasattrr4   r3   r   is_authenticatedr   r1   r0   r    r   r   r   r   T   s   
r   c                 C   s*   t d}t d}| j}t|jr|S |S )Nr)   r   )r   r    r   r6   r7   r   r   r   r   _   s   r   )r,   typesr   django.httpr   django.urlsr   r   django.utils.decoratorsr   asgiref.syncr   r   allauth.account.adapterr	   allauth.account.internalr
   allauth.corer   allauth.core.exceptionsr   r   r'   boolr   r3   r   r   r   r   r   r   <module>   s"    
#
