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 dd
lmZmZmZ ddlmZmZmZmZmZ ddlm
Z
 G dd deZdddZdS )    )absolute_import)ValidationError)reverse)gettext_lazy   )app_settings)get_adapter)EmailVerificationMethod)EmailAddress)
user_email
user_fielduser_username)deserialize_instanceemail_address_existsimport_attributeserialize_instancevalid_email_or_none   c                   @   s   e Zd ZdediZd ddZdd Z			d!dd	Zd
d Zd 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d Zdd ZdS )"DefaultSocialAccountAdapteremail_takenzwAn account already exists with this e-mail address. Please sign in to that account first, then connect your %s account.Nc                 C   s
   || _ d S Nrequest)selfr    r   T/var/www/ideatree/venv/lib/python3.10/site-packages/allauth/socialaccount/adapter.py__init__       
z$DefaultSocialAccountAdapter.__init__c                 C      dS )a	  
        Invoked just after a user successfully authenticates via a
        social provider, but before the login is actually processed
        (and before the pre_social_login signal is emitted).

        You can use this hook to intervene, e.g. abort the login by
        raising an ImmediateHttpResponse

        Why both an adapter hook and the signal? Intervening in
        e.g. the flow from within a signal handler is bad -- multiple
        handlers may be active and are executed in undetermined order.
        Nr   r   r   socialloginr   r   r   pre_social_login#   s   z,DefaultSocialAccountAdapter.pre_social_loginc                 C   r   )a  
        Invoked when there is an error in the authentication cycle. In this
        case, pre_social_login will not be reached.

        You can use this hook to intervene, e.g. redirect to an
        educational flow by raising an ImmediateHttpResponse.
        Nr   )r   r   provider_iderror	exceptionextra_contextr   r   r   authentication_error2   s   z0DefaultSocialAccountAdapter.authentication_errorc                 C   s   t  |S )z3
        Instantiates a new User instance.
        )get_account_adapternew_userr   r   r   r   r(   C   s   z$DefaultSocialAccountAdapter.new_userc                 C   s@   |j }|  |rt ||| nt || || |S )zy
        Saves a newly signed up social login. In case of auto-signup,
        the signup form is not available.
        )userset_unusable_passwordr'   	save_userpopulate_usernamesave)r   r   r    formur   r   r   r+   I   s   
z%DefaultSocialAccountAdapter.save_userc                 C   s   | d}| d}| d}| d}| d}|j}	t|	|p!d t|	t|p*d |p/dd}
t|	d|p;|
d  t|	d|pE|
d	  |	S )
a  
        Hook that can be used to further populate the user instance.

        For convenience, we populate several common fields.

        Note that the user instance being populated represents a
        suggested User instance that represents the social user that is
        in the process of being logged in.

        The User instance need not be completely valid and conflict
        free. For example, verifying whether or not the username
        already exists, is not a responsibility.
        username
first_name	last_nameemailname  r   r   )getr)   r   r   r   	partitionr   )r   r   r    datar0   r1   r2   r3   r4   r)   
name_partsr   r   r   populate_userW   s   




z)DefaultSocialAccountAdapter.populate_userc                 C   s   |j jsJ td}|S )zp
        Returns the default URL to redirect to after successfully
        connecting a social account.
        socialaccount_connections)r)   is_authenticatedr   )r   r   socialaccounturlr   r   r   get_connect_redirect_urlr   s   z4DefaultSocialAccountAdapter.get_connect_redirect_urlc                 C   s\   t |dkr(|j sttdtjtjkr*t	j
j|jdd s,ttddS dS dS )zg
        Validate whether or not the socialaccount account can be
        safely disconnected.
        r   z$Your account has no password set up.T)r)   verifiedz,Your account has no verified e-mail address.N)lenr)   has_usable_passwordr   _r   EMAIL_VERIFICATIONr	   	MANDATORYr
   objectsfilterexists)r   accountaccountsr   r   r   validate_disconnect{   s   
z/DefaultSocialAccountAdapter.validate_disconnectc                 C   s<   t j}|rt|j}|rtjrt|rd}|S t jrd}|S )NF)r   AUTO_SIGNUPr   r)   account_settingsUNIQUE_EMAILr   EMAIL_REQUIRED)r   r   r    auto_signupr3   r   r   r   is_auto_signup_allowed   s   
z2DefaultSocialAccountAdapter.is_auto_signup_allowedc                 C   s   t ||S )z
        Checks whether or not the site is open for signups.

        Next to simply returning True/False you can also intervene the
        regular flow by raising an ImmediateHttpResponse
        )r'   is_open_for_signupr   r   r   r   rS      s   z.DefaultSocialAccountAdapter.is_open_for_signupc                 C   s<   |j }t|pdt|pdt|dpdt|dpdd}|S )Nr5   r1   r2   )r3   r0   r1   r2   )r)   r   r   r   )r   r    r)   initialr   r   r   get_signup_form_initial_data   s   

z8DefaultSocialAccountAdapter.get_signup_form_initial_datac                 C   s
   t ||S r   )r   )r   modelr9   r   r   r   r      r   z0DefaultSocialAccountAdapter.deserialize_instancec                 C   s   t |S r   )r   )r   instancer   r   r   r      s   z.DefaultSocialAccountAdapter.serialize_instancec                 C   s`   ddl m} tj|i d}|r'||d}dD ]}t|||| q|S |j||}|S )Nr   )	SocialAppAPP)provider)	client_idsecretkeycertificate_key)allauth.socialaccount.modelsrX   r   	PROVIDERSr7   setattrrG   get_current)r   r   rZ   rX   configappfieldr   r   r   get_app   s   
z#DefaultSocialAccountAdapter.get_appr   )NNN)__name__
__module____qualname__rD   error_messagesr   r!   r&   r(   r+   r;   r@   rL   rR   rS   rU   r   r   rf   r   r   r   r   r      s,    


		
r   Nc                 C   s   t tj| S r   )r   r   ADAPTERr   r   r   r   r      s   r   r   ) 
__future__r   django.core.exceptionsr   django.urlsr   django.utils.translationr   rD   rJ   r   rN   account.adapterr   r'   account.app_settingsr	   account.modelsr
   account.utilsr   r   r   utilsr   r   r   r   r   r5   objectr   r   r   r   r   <module>   s     ;