a
    }`>                     @   s  d dl Z d dlmZ d dlmZ d dl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 d d
lmZ d dlmZ d dlmZmZmZ d dlmZ ddlmZ ddlmZm Z m!Z!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl$m(Z( dd Z)dAddZ*dBddZ+da,dd Z-dd  Z.d!d" Z/d#d$ Z0d%d& Z1d'd( Z2d)d* Z3dCd+d,Z4dDd-d.Z5d/d0 Z6d1d2 Z7dEd3d4Z8d5d6 Z9d7d8 Z:dFd9d:Z;d;d< Z<d=d> Z=d?d@ Z>dS )G    N)OrderedDict)	timedelta)settings)messages)update_session_auth_hash)FieldDoesNotExistValidationError)models)Q)HttpResponseRedirect)	force_str)base36_to_intint_to_base36	urlencode)now   )ImmediateHttpResponse)get_request_paramget_user_modelimport_callablevalid_email_or_none   )app_settingssignals)get_adapter)EmailVerificationMethodc                 C   s(   t d|  }t d| }||kS )z
    Perform case-insensitive comparison of two identifiers, using the
    recommended algorithm from Unicode Technical Report 36, section
    2.11.2(B)(2).
    NFKC)unicodedata	normalizecasefold)s1s2Znorm_s1Znorm_s2 r"   M/var/www/html/Ranjet/env/lib/python3.9/site-packages/allauth/account/utils.py_unicode_ci_compare   s    r$   nextc                 C   s    t | |}t| |sd}|S )z_
    Returns the next URL to redirect to, if it was explicitly passed
    via the request.
    N)r   r   Zis_safe_url)requestredirect_field_nameZredirect_tor"   r"   r#   get_next_redirect_url'   s    
r(   Fc                 C   sP   |}|rt |r| }|s&t| |d}|sL|r>t| | }nt| | }|S )N)r'   )callabler(   r   Zget_signup_redirect_urlget_login_redirect_url)r&   urlr'   signupretr"   r"   r#   r*   2   s    r*   c                 C   s   t jst| | d S N)r   ZLOGOUT_ON_PASSWORD_CHANGEr   )r&   userr"   r"   r#   logout_on_password_changeE   s    r0   c                 C   s   t jrt| t jS t| S d S r.   )r   USER_MODEL_USERNAME_FIELDgetattrr   r/   r"   r"   r#   default_user_displayM   s    r4   c                 C   s    t sttdt}t|a t | S )NZACCOUNT_USER_DISPLAY)_user_display_callabler2   r   r4   r   )r/   fr"   r"   r#   user_displayT   s    r7   c                 G   s   |sdS t  }z|j|}|j}W n& tyJ   t| |sBY dS d}Y n0 |rv|d }|rh|d| }t| || n
t| |S dS )zR
    Gets or sets (optional) user model fields. No-op if fields do not exist.
    Nr   )r   _meta	get_field
max_lengthr   hasattrsetattrr2   )r/   fieldargsUserZ
field_metar:   vr"   r"   r#   
user_field\   s     


rA   c                 G   s4   |r t js |d r |d  g}t| t jg|R  S )Nr   )r   PRESERVE_USERNAME_CASINGlowerrA   r1   r/   r>   r"   r"   r#   user_usernameu   s    rE   c                 G   s   t | tjg|R  S r.   )rA   r   USER_MODEL_EMAIL_FIELDrD   r"   r"   r#   
user_email{   s    rG   c                 C   s`   ddl m} d }|rHd}z|j| |}|j}W q\ |jyD   Y q\0 n|jj| dd }|S )Nr   EmailAddressFT)r/   verified)r	   rI   objectsget_for_userrJ   DoesNotExistfilterexists)r/   emailrI   Zemailaddressr-   r"   r"   r#   _has_verified_for_login   s    
rQ   c           
   
   C   s  t | }|js|| |S |tjkr&nZ|tjkrPt||s|rt| |||d n0|tjkrt||st| |||d |	| |S zb|
| | tt| ||d}|du ri }tjjf |j| ||d| || tjdd|i W n, ty }	 z|	j}W Y d}	~	n
d}	~	0 0 |S )z
    Keyword arguments:

    signup -- Indicates whether or not sending the
    email is essential (during signup), or if it can be skipped (e.g. in
    case email verification is optional and we are only logging in).
    )r,   rP   r,   N)senderr&   responser/   zaccount/messages/logged_in.txtr/   )r   	is_activeZrespond_user_inactiver   NONEZOPTIONALrQ   send_email_confirmationZ	MANDATORYZrespond_email_verification_sentZloginr   r*   r   Zuser_logged_insend	__class__add_messager   SUCCESSr   rT   )
r&   r/   email_verificationredirect_urlsignal_kwargsr,   rP   adapterrT   er"   r"   r#   perform_login   sH    



ra   c                 C   s<   |d u ri }t jjf |j| |d| t| ||d||dS )N)rS   r&   r/   T)r\   r,   r]   r^   )r   Zuser_signed_uprX   rY   ra   )r&   r/   r\   Zsuccess_urlr^   r"   r"   r#   complete_signup   s    rb   c                 C   sZ  ddl m} t| }t }g }g }g }|D ]}t|j}	|	s>q*tjrX|jj	|	d
 rXq*||	 }
|
r|
jpt|j|
_|
jp|j|
_n&|}
|
jp|| |
j|
_|
||	 < |
jr||
 |
jr||
 |
jr*||
 q*|r|d }n6|r|d }n(|r|d }n|r | d }nd}| D ]}
|j |
j k|
_q,t| |fS )a  
    Takes a list of EmailAddress instances and cleans it up, making
    sure only valid ones remain, without multiple primaries etc.

    Order is important: e.g. if multiple primary e-mail addresses
    exist, the first one encountered will be kept as primary.
    r   rH   email__iexactr   N)r	   rI   r   r   r   rP   r   UNIQUE_EMAILrK   rN   rO   getrC   primaryrJ   Zis_email_verifiedappendkeysvalueslist)r&   	addressesrI   r_   Ze2aZprimary_addressesZverified_addressesZprimary_verified_addressesaddressrP   aZprimary_addressr"   r"   r#   cleanup_email_addresses   sP    





ro   c           
      C   s   ddl m} |jj|d r"J g }t| }|| }|rR||||ddd t|}|rt||||ddd t	| || \}}|D ]}	||	_
|	  q|j|| |r|r| |j krt||j |  |S )z
    Creates proper EmailAddress for the user that was just signed
    up. Only sets up, doesn't do any other handling such as sending
    out email confirmation mails etc.
    r   rH   r3   Tr/   rP   rg   rJ   F)r	   rI   rK   rN   rO   r   Zunstash_verified_emailrh   rG   ro   r/   saveZfill_cache_for_userrC   rP   )
r&   r/   rl   rI   Zpriority_addressesr_   Zstashed_emailrP   rg   rn   r"   r"   r#   setup_user_email  s2    

rr   c           	   
   C   s   ddl m}m} ttjd}|s(t|}|rzX|j||}|j	s~tj
rNd}n|jjt | |d  }|r|j| |d nd}W n6 |jy   d}|jj| |||dd}|sJ Y n0 |rt| | tjd	d
|i |rt| | t| dS )a  
    E-mail verification mails are sent:
    a) Explicitly: when a user signs up
    b) Implicitly: when a user attempts to log in using an unverified
    e-mail while EMAIL_VERIFICATION is mandatory.

    Especially in case of b), we want to limit the number of mails
    sent (consider a user retrying a few times), which is why there is
    a cooldown period before sending a new mail. This cooldown period
    can be configured in ACCOUNT_EMAIL_CONFIRMATION_COOLDOWN setting.
    r   )rI   EmailConfirmation)secondsT)Zsent__gtemail_addressrR   F)r,   Zconfirmz,account/messages/email_confirmation_sent.txtrP   N)r	   rI   rs   r   r   ZEMAIL_CONFIRMATION_COOLDOWNrG   rK   rL   rJ   ZEMAIL_CONFIRMATION_HMACrN   r   rO   Zsend_confirmationrM   Z	add_emailr   rZ   r   INFOZ
stash_useruser_pk_to_url_str)	r&   r/   r,   rP   rI   rs   Zcooldown_periodru   Z
send_emailr"   r"   r#   rW   <  s@    
rW   c                 C   s`   ddl m} t| }|r\|jj| |d s\tjrH|jj|d rHdS |jj| |ddd dS )z
    Keep user.email in sync with user.emailaddress_set.

    Under some circumstances the user.email may not have ended up as
    an EmailAddress record, e.g. in the case of manually created admin
    users.
    r   rH   )r/   rd   rc   NFrp   )	r	   rI   rG   rK   rN   rO   r   re   create)r/   rI   rP   r"   r"   r#   sync_user_email_addresseso  s    ry   c                  G   sr   t jrFdd | D }|d }|dd  D ]}||B }q(t j|}n(t jjf i t jd dd | D i}|S )Nc                 S   s$   g | ]}t f i tjd  |iqS )__iexact)r
   r   r1   .0ur"   r"   r#   
<listcomp>  s   z,filter_users_by_username.<locals>.<listcomp>r   r   Z__inc                 S   s   g | ]}|  qS r"   )rC   r{   r"   r"   r#   r~         )r   rB   r   Z_default_managerrN   r1   )usernameZqlistqZq2r-   r"   r"   r#   filter_users_by_username  s     
r   c                 C   s   ddl m} t }|jj| d}|dur4|j|d}g }|dD ]}t|j| rB||j	 qBt
jrt
jd | i}|jjf i |}|dur|j|d}| D ]$}	t|	t
j}
t|
| r||	 qtt|S )	zReturn list of users by email address

    Typically one, at most just a few in length.  First we look through
    EmailAddress table, than customisable User model table. Add results
    together avoiding SQL joins and deduplicate.
    r   rH   rc   N)Zuser__is_activer/   rz   )rU   )r	   rI   r   rK   rN   Zprefetch_relatedr$   rP   rh   r/   r   rF   iteratorr2   rk   set)rP   rU   rI   r?   ZmailsZusersr`   Zq_dictZuser_qsr/   rG   r"   r"   r#   filter_users_by_email  s&    
r   c                 C   s8   | ddk sJ t| |}|r4|d t||i }|S )N?r   )findr(   r   )r&   r+   r'   Znext_urlr"   r"   r#   passthrough_next_redirect_url  s
    
r   c                 C   sV   t  }tt|jjtjr4t| jtr,| jS | jj	S | j}t|t
rNt| j}t|S )z&
    This should return a string.
    )r   
issubclasstyper8   pkr	   	UUIDField
isinstancestrhexintr   )r/   r?   r-   r"   r"   r#   rw     s    

rw   c                 C   s   t  }t|jjdd }|r8t|dd r8|jjjjjj}n|jj}tt|tj	rZ|
| S z|
d | }W n ty   t| }Y n0 |S )Nremote_fieldtorn   )r   r2   r8   r   r   r   r   r   r	   r   Z	to_pythonr   r   )sr?   r   Zpk_fieldr   r"   r"   r#   url_str_to_user_pk  s    

r   )r%   )Nr%   F)NNFN)N)FN)N)?r   collectionsr   datetimer   Zdjango.confr   Zdjango.contribr   Zdjango.contrib.authr   Zdjango.core.exceptionsr   r   Z	django.dbr	   Zdjango.db.modelsr
   Zdjango.httpr   Zdjango.utils.encodingr   Zdjango.utils.httpr   r   r   Zdjango.utils.timezoner   
exceptionsr   utilsr   r   r   r    r   r   r_   r   r   r$   r(   r*   r5   r0   r4   r7   rA   rE   rG   rQ   ra   rb   ro   rr   rW   ry   r   r   r   rw   r   r"   r"   r"   r#   <module>   sV   

    
<
>#
3
