o
    ˷e                     @   s  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 d dlmZ d dl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 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" Z)dS )#    )messages)ValidationError)HttpResponseRedirect)render)reverse)app_settings)get_adapter)complete_signupperform_loginuser_displayuser_username)ImmediateHttpResponse   )r   signals)SocialLogin)	AuthErrorAuthProcessc                 C   s   t | | |}|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 | | |sLt| dtj S t | j| |d d t| |}|S )Nsocialaccount_socialloginsocialaccount_signup zaccount/signup_closed.)form)r   is_auto_signup_allowed	serializesessionr   r   account_settingsUSER_MODEL_USERNAME_FIELDr   userget_account_adapterclean_usernamer   is_open_for_signupr   TEMPLATE_EXTENSION	save_usercomplete_social_signup)requestsocialloginauto_signupurlretusername r)   T/var/www/ideatree/venv/lib/python3.10/site-packages/allauth/socialaccount/helpers.py_process_signup   s*   

r+   c                 C       t | |jtj|| d|idS )Nr$   )email_verificationredirect_urlsignal_kwargs)r
   r   r   EMAIL_VERIFICATIONget_redirect_urlr#   r$   r)   r)   r*   _login_social_account4      r3   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r5ttdS d|||di}|| t	| dt
j |S )N)error	exceptionextra_contextsocialaccount_login_cancelled
auth_error)providercoder6   z#socialaccount/authentication_error.)r   authentication_errorr   responser   	CANCELLEDr   r   updater   r   r    )r#   provider_idr5   r6   r7   econtextr)   r)   r*   render_authentication_error>   s8   


rC   c                 C   s   | j jr
ttdS tj}d}d }|jr.|j | j kr tj}d}n d}d}tj	j
t| |d nd}|| | j  tjj
t| |d t| | |j}|| pO|}t| j| ||||dd	 t|S )
Nsocialaccount_connectionsz,socialaccount/messages/account_connected.txtz2socialaccount/messages/account_connected_other.txtupdatedz4socialaccount/messages/account_connected_updated.txtsenderr#   r$   added)r$   action)message_context)r   is_anonymousr   r   r   INFOis_existingERRORr   social_account_updatedsendr   connectsocial_account_addedr   get_connect_redirect_urlaccountr1   r   add_message)r#   r$   levelmessagerI   default_nextnext_urlr)   r)   r*   _add_social_accountb   s<   rZ   c              
   C   s   |j rJ |  z3t| | | tjjt| |d |jd}|t	j
kr,t| |W S |t	jkr7t| |W S t| |W S  tyQ } z	|jW  Y d }~S d }~ww )NrF   process)rM   lookupr   pre_social_loginr   rP   r   stategetr   REDIRECT_social_login_redirectCONNECTrZ   _complete_social_loginr   r=   )r#   r$   r[   rA   r)   r)   r*   complete_social_login   s"   


rd   c                 C   s   | | pd}t|S )N/)r1   r   )r#   r$   rY   r)   r)   r*   ra      s   ra   c                 C   sJ   | j jrt| |  |jrt| |}tjjt	| |d |S t
| |}|S )NrF   )r   is_authenticatedr   logoutrM   r3   r   rO   rP   r   r+   )r#   r$   r'   r)   r)   r*   rc      s   

rc   c                 C   r,   )Nr$   )r/   )r	   r   r   r0   r1   r2   r)   r)   r*   r"      r4   r"   c                 C   s(   |  d\}}}t||gd}t||S )N.)fromlist)
rpartition
__import__getattr)pathmodname_attrmr)   r)   r*   import_path   s   
rr   c                 C   s   t j}|s
t| jS || S )N)r   SOCIALACCOUNT_STRr   r   )socialaccountfuncr)   r)   r*   socialaccount_user_display   s   
rv   )*django.contribr   django.formsr   django.httpr   django.shortcutsr   django.urlsr   allauth.accountr   r   allauth.account.adapterr   r   allauth.account.utilsr	   r
   r   r   allauth.exceptionsr   r   r   adaptermodelsr   providers.baser   r   r+   r3   UNKNOWNrC   rZ   rd   ra   rc   r"   rr   rv   r)   r)   r)   r*   <module>   s4    
$,