o
    &zhF                     @   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 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 dd Zdd Zdd Zdd Zdd ZdS )    )messages)PermissionDeniedValidationError)HttpResponseRedirect)app_settings)get_adapter)flows)EmailAddress)ReauthenticationRequired)signals)SocialAccountSocialLoginc                 C   s   t jj|jd}|j|jd  }t }|r@tj	r|
d|j s)|
dtjtjjkr@tjj|jdd s@|
d||| dS )	z[
    Validate whether or not the socialaccount account can be
    safely disconnected.
    )user_id)pkdisconnect_lastno_passwordT)userverifiedno_verified_emailN)r   objectsfilterr   excluder   existsr   allauth_settingsSOCIALACCOUNT_ONLYvalidation_errorr   has_usable_passwordaccount_settingsEMAIL_VERIFICATIONEmailVerificationMethod	MANDATORYr	   validate_disconnect)requestaccountaccountsis_lastadapter r'   g/var/www/html/kangema/venv/lib/python3.10/site-packages/allauth/socialaccount/internal/flows/connect.pyr!      s$   



r!   c                 C   sd   t jr	tj|  t | tjd |	 }|
  tjjt| |d t jd| j||dd d S )Nz/socialaccount/messages/account_disconnected.txt)senderr"   socialaccountz(socialaccount/email/account_disconnected)r#   provider)context)r   REAUTHENTICATION_REQUIREDr   reauthentication"raise_if_reauthentication_requiredget_account_adapteradd_messager   INFOget_providerdeleter   social_account_removedsendr   r   send_notification_mailr   )r"   r#   r+   r'   r'   r(   
disconnect*   s&   
r8   c                 C   s   t |}t| |S )N)r   deserializeconnect)r"   serialized_statesocialloginr'   r'   r(   resume_connectB   s   

r=   c           	      C   s   z
t | |\}}}W n4 ty   t | |j}t| Y S  ty1   tj	| |
 d Y S  ty>   d\}}}Y nw |rDtjntj}t | |j}|| pU|}t| j| ||||dd t|S )Nz;allauth.socialaccount.internal.flows.connect.resume_connect)FNz2socialaccount/messages/account_connected_other.txt)r<   action)message_context)
do_connectr   r   get_connect_redirect_urlr#   r   r
   r   r.   stash_and_reauthenticate	serializer   r   r2   ERRORget_redirect_urlr0   r1   )	r"   r<   okr>   messageconnect_redirect_urlleveldefault_nextnext_urlr'   r'   r(   r:   G   s4   r:   c                 C   s   | j jrt tjrtj|  d}d }d}|jr9|j | j kr%t	 
d|jjjs/d}d}nd}|| | j  n	d}|| | j  |||fS )Nz,socialaccount/messages/account_connected.txtTconnected_otherupdatedz4socialaccount/messages/account_connected_updated.txtadded)r   is_anonymousr   r   r-   r   r.   r/   is_existingr   r   r#   _stateaddingr:   )r"   r<   rG   r>   rF   r'   r'   r(   r@   i   s$   

r@   N) django.contribr   django.core.exceptionsr   r   django.httpr   allauthr   r   allauth.accountr   allauth.account.adapterr   r0   allauth.account.internalr   allauth.account.modelsr	   allauth.core.exceptionsr
   allauth.socialaccountr   allauth.socialaccount.adapterallauth.socialaccount.modelsr   r   r!   r8   r=   r:   r@   r'   r'   r'   r(   <module>   s"    "