o
    LfL%                     @   s6  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mZmZmZ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"m#Z# dd Z$dd Z%dd Z&dd Z'e"j(ddfddZ)dd Z*dd Z+dd Z,dd  Z-d!d" Z.d#d$ Z/d%d& Z0dS )'    )messages)ValidationError)HttpResponseRedirect)render)reverse)app_settingsauthentication)get_adapter)reauthenticate_then_callback)assess_unique_emailcomplete_signupperform_loginuser_display
user_emailuser_username)ImmediateHttpResponse)r   signals)SocialLogin)	AuthErrorAuthProcessc                 C   s   t  | |}|sdS t|j}|rBt|}|du r	 |d fS |du r)d}|d fS |d u s/J t| }|| || d }d|fS tj	rGd}|d fS )N)FNTF)
r	   is_auto_signup_allowedr   userr   get_account_adapter send_account_already_exists_mailrespond_email_verification_sentr   EMAIL_REQUIRED)requestsocialloginauto_signupemail
assessmentaccount_adapterresp r#   ^/var/www/html/humari/django-venv/lib/python3.10/site-packages/allauth/socialaccount/helpers.py_process_auto_signup   s(   

r%   c                 C   s   t | |\}}|r|S |s| | jd< td}t|}|S tjr?t|j}z	t	| 
| W n ty>   t|jd Y nw t | |sNt| dtj S t j| |d d t| |}|S )Nsocialaccount_socialloginsocialaccount_signup zaccount/signup_closed.)form)r%   	serializesessionr   r   account_settingsUSER_MODEL_USERNAME_FIELDr   r   r   clean_usernamer   r	   is_open_for_signupr   TEMPLATE_EXTENSION	save_usercomplete_social_signup)r   r   r   r"   urlusernamer#   r#   r$   _process_signupA   s.   

r5   c                 C   s&   t j| dfi |jj|jjd d S )Nsocialaccount)provideruid)r   record_authenticationaccountr7   r8   r   r   r#   r#   r$   r9   `   s   
r9   c                 C       t | |jtj|| d|idS )Nr   )email_verificationredirect_urlsignal_kwargs)r   r   r   EMAIL_VERIFICATIONget_redirect_urlr;   r#   r#   r$   _login_social_accountk      rB   Nc              
   C   s   z|d u ri }t  j| ||||d W n ty( } z	|jW  Y d }~S d }~ww |tjkr4ttdS d|||di}|| t	| dt
j |S )N)error	exceptionextra_contextsocialaccount_login_cancelled
auth_error)r7   coderE   z#socialaccount/authentication_error.)r	   on_authentication_errorr   responser   	CANCELLEDr   r   updater   r,   r0   )r   r7   rD   rE   rF   econtextr#   r#   r$   render_authentication_erroru   s8   


rP   c                 C   s   t |}t| |S N)r   deserialize_add_social_account)r   serialized_stater   r#   r#   r$   resume_add_social_account   s   

rU   c           	         s   | j jrt |  j}t|S tjr!t|  fddd}|r!|S t	j
}d}d } jr< j | j kr7t	j}d}nd}d}n	d} | | j  | j jsKJ t |  j} | pY|}t| j| || |d	d
 t|S )Nc                    s      S rQ   )r*   )r   r   r#   r$   <lambda>   s    z%_add_social_account.<locals>.<lambda>z7allauth.socialaccount.helpers.resume_add_social_accountz,socialaccount/messages/account_connected.txtz2socialaccount/messages/account_connected_other.txtupdatedz4socialaccount/messages/account_connected_updated.txtadded)r   action)message_context)r   is_anonymousr	   get_connect_redirect_urlr:   r   r,   REAUTHENTICATION_REQUIREDr
   r   INFOis_existingERRORconnectis_authenticatedrA   r   add_message)	r   r   connect_redirect_urlrK   levelmessagerZ   default_nextnext_urlr#   rV   r$   rS      sD   
rS   c              
   C   s   |j rJ |  z2t | | tjjt| |d |jd}|t	j
kr+t| |W S |t	jkr6t| |W S t| |W S  tyP } z	|jW  Y d }~S d }~ww )N)senderr   r   process)r`   lookupr	   pre_social_loginr   sendr   stategetr   REDIRECT_social_login_redirectCONNECTrS   _complete_social_loginr   rK   )r   r   rk   rN   r#   r#   r$   complete_social_login   s"   


ru   c                 C   s   | | pd}t|S )N/)rA   r   )r   r   ri   r#   r#   r$   rr      s   rr   c                 C   sB   | j jrt| |  |jrt| | t| |}|S t| |}|S rQ   )r   rc   r   logoutr`   r9   rB   r5   )r   r   retr#   r#   r$   rt      s   


rt   c                 C   r<   )Nr   )r?   )r   r   r   r@   rA   r;   r#   r#   r$   r2      rC   r2   c                 C   s   t j}|s
t| jS || S rQ   )r   SOCIALACCOUNT_STRr   r   )r6   funcr#   r#   r$   socialaccount_user_display   s   
r{   )1django.contribr   django.formsr   django.httpr   django.shortcutsr   django.urlsr   allauth.accountr   r,   r   allauth.account.adapterr	   r    allauth.account.reauthenticationr
   allauth.account.utilsr   r   r   r   r   r   allauth.core.exceptionsr   allauth.socialaccountr   allauth.socialaccount.adapterallauth.socialaccount.modelsr   $allauth.socialaccount.providers.baser   r   r%   r5   r9   rB   UNKNOWNrP   rU   rS   ru   rr   rt   r2   r{   r#   r#   r#   r$   <module>   s:     (
$0
