U
    `\                     @   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 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mZ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+ d dl,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6 ddlm7Z7m8Z8 eeddZ9G dd deZ:G dd de'eZ;G dd de6Z<G d d! d!e6Z=G d"d# d#e Z>G d$d% d%eZ?d&S )'    )app_settings)get_adapter)complete_signup)ConfirmEmailView)signals)SocialAccount)settings)method_decorator)gettext_lazy)sensitive_post_parametersstatus)MethodNotAllowedNotFound)CreateAPIViewGenericAPIViewListAPIView)AllowAnyIsAuthenticated)Response)APIView)JWTSerializerTokenSerializercreate_token)
TokenModel)SocialAccountSerializerSocialConnectSerializerSocialLoginSerializerVerifyEmailSerializer)
jwt_encode)	LoginView   )RegisterSerializerregister_permission_classes	password1	password2c                       sJ   e Zd ZeZe ZeZdZ	e
 fddZdd Zdd Zdd	 Z  ZS )
RegisterViewdj_rest_authc                    s   t  j||S N)superdispatchselfargskwargs	__class__ G/Users/michael/Projects/dj-rest-auth/dj_rest_auth/registration/views.pyr*   ,   s    zRegisterView.dispatchc                 C   s`   t jt jjkrdtdiS ttddrH|| j| jd}t	|| 
 djS t|j| 
 djS d S )NdetailzVerification e-mail sent.REST_USE_JWTF)useraccess_tokenrefresh_token)context)allauth_settingsEMAIL_VERIFICATIONEmailVerificationMethod	MANDATORY_getattrr   r6   r7   r   Zget_serializer_contextdatar   
auth_token)r,   r5   r?   r1   r1   r2   get_response_data0   s    zRegisterView.get_response_datac                 O   sF   | j |jd}|jdd | |}| |j}t| |tj|dS )Nr?   Traise_exception)r   headers)	get_serializerr?   is_validperform_createZget_success_headersr   rA   r   HTTP_201_CREATED)r,   requestr-   r.   
serializerr5   rE   r1   r1   r2   create?   s    
zRegisterView.createc                 C   s^   | | j}tjtjjkrFttddr8t|\| _	| _
nt| j|| t| jj|tjd  |S )Nr4   F)saverJ   r9   r:   r;   r<   r>   r   r   r6   r7   r   token_modelr   _request)r,   rK   r5   r1   r1   r2   rH   K   s     zRegisterView.perform_create)__name__
__module____qualname__r"   serializer_classr#   permission_classesr   rN   Zthrottle_scopesensitive_post_parameters_mr*   rA   rL   rH   __classcell__r1   r1   r/   r2   r&   &   s   r&   c                   @   s.   e Zd ZefZdZdd Zdd Zdd ZdS )	VerifyEmailView)POSTOPTIONSHEADc                 O   s
   t ||S r(   )r   r+   r1   r1   r2   rF   `   s    zVerifyEmailView.get_serializerc                 O   s   t dd S )NGET)r   r+   r1   r1   r2   getc   s    zVerifyEmailView.getc                 O   sT   | j |jd}|jdd |jd | jd< |  }|| j tdt	dit
jdS )NrB   TrC   keyr3   okr   )rF   r?   rG   validated_datar.   
get_objectconfirmrJ   r   r=   r   HTTP_200_OK)r,   rJ   r-   r.   rK   confirmationr1   r1   r2   postf   s    zVerifyEmailView.postN)	rP   rQ   rR   r   rT   allowed_methodsrF   r\   rd   r1   r1   r1   r2   rW   \   s
   rW   c                   @   s   e Zd ZdZeZdd ZdS )SocialLoginViewa  
    class used for social authentications
    example usage for facebook with access_token
    -------------
    from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter

    class FacebookLogin(SocialLoginView):
        adapter_class = FacebookOAuth2Adapter
    -------------

    example usage for facebook with code

    -------------
    from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
    from allauth.socialaccount.providers.oauth2.client import OAuth2Client

    class FacebookLogin(SocialLoginView):
        adapter_class = FacebookOAuth2Adapter
        client_class = OAuth2Client
        callback_url = 'localhost:8000'
    -------------
    c                 C   s   t | j| j| j d S r(   r   rJ   loginr5   r,   r1   r1   r2   process_login   s    zSocialLoginView.process_loginN)rP   rQ   rR   __doc__r   rS   rj   r1   r1   r1   r2   rf   o   s   rf   c                   @   s"   e Zd ZdZeZefZdd ZdS )SocialConnectViewa7  
    class used for social account linking

    example usage for facebook with access_token
    -------------
    from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter

    class FacebookConnect(SocialConnectView):
        adapter_class = FacebookOAuth2Adapter
    -------------
    c                 C   s   t | j| j| j d S r(   rg   ri   r1   r1   r2   rj      s    zSocialConnectView.process_loginN)	rP   rQ   rR   rk   r   rS   r   rT   rj   r1   r1   r1   r2   rl      s   rl   c                   @   s"   e Zd ZdZeZefZdd ZdS )SocialAccountListViewz>
    List SocialAccounts for the currently logged in user
    c                 C   s   t jj| jjdS N)r5   r   objectsfilterrJ   r5   ri   r1   r1   r2   get_queryset   s    z"SocialAccountListView.get_querysetN)	rP   rQ   rR   rk   r   rS   r   rT   rr   r1   r1   r1   r2   rm      s   rm   c                   @   s*   e Zd ZdZeZefZdd Zdd Z	dS )SocialAccountDisconnectViewz[
    Disconnect SocialAccount from remote service for
    the currently logged in user
    c                 C   s   t jj| jjdS rn   ro   ri   r1   r1   r2   rr      s    z(SocialAccountDisconnectView.get_querysetc                 O   sb   |   }|j|d d }|s$tt| j|| |  tj	j
t| j|d t| |jS )Npk)rt   )senderrJ   socialaccount)rr   rq   firstr   get_social_adapterrJ   validate_disconnectdeleter   social_account_removedsendr   r   rF   r?   )r,   rJ   r-   r.   accountsaccountr1   r1   r2   rd      s    z SocialAccountDisconnectView.postN)
rP   rQ   rR   rk   r   rS   r   rT   rr   rd   r1   r1   r1   r2   rs      s
   rs   N)@allauth.accountr   r9   Zallauth.account.adapterr   allauth.account.utilsr   Zallauth.account.viewsr   Zallauth.socialaccountr   Zallauth.socialaccount.adapterrx   allauth.socialaccount.modelsr   django.confr   django.utils.decoratorsr	   django.utils.translationr
   r=   django.views.decorators.debugr   rest_frameworkr   Zrest_framework.exceptionsr   r   Zrest_framework.genericsr   r   r   rest_framework.permissionsr   r   Zrest_framework.responser   rest_framework.viewsr   Zdj_rest_auth.app_settingsr   r   r   Zdj_rest_auth.modelsr   %dj_rest_auth.registration.serializersr   r   r   r   Zdj_rest_auth.utilsr   Zdj_rest_auth.viewsr    r"   r#   rU   r&   rW   rf   rl   rm   rs   r1   r1   r1   r2   <module>   s>   6