U
    `                     @   s   d dl mZ d dlmZ d dlmZ dejkr^d dlmZ d dl	m
Z
 d dlmZ d dlmZ G d	d
 d
ejZG dd deeZdS )    )settings)HttpRequest)serializerszallauth.socialaccount)complete_social_login)SocialToken)
OAuthError)SocialConnectMixinc                   @   s4   e Zd Ze Ze Zdd Zdd Zdd Z	dS )TwitterLoginSerializerc                 C   s    | j d}t|ts|j}|S )Nrequest)contextget
isinstancer   _request)selfr
    r   G/Users/michael/Projects/dj-rest-auth/dj_rest_auth/social_serializers.py_get_request   s    
z#TwitterLoginSerializer._get_requestc                 C   s$   |   }|j||||d}||_|S )a  
        :param adapter: allauth.socialaccount Adapter subclass.
            Usually OAuthAdapter or Auth2Adapter
        :param app: `allauth.socialaccount.SocialApp` instance
        :param token: `allauth.socialaccount.SocialToken` instance
        :param response: Provider's response for OAuth1. Not used in the
        :returns: A populated instance of the
            `allauth.socialaccount.SocialLoginView` instance
        )response)r   complete_logintoken)r   adapterappr   r   r
   social_loginr   r   r   get_social_login   s    
  z'TwitterLoginSerializer.get_social_loginc              
   C   s  | j d}|  }|s"tdt|dd }|s<td||}| |}|d}|d}||d|jd< t	||d	}	||	_
z| |||	|}
t||
 W n0 tk
r } ztt|W 5 d }~X Y nX |
js|
  |
j|d
d |
jj|d< |S )Nviewz2View is not defined, pass it as a context variableadapter_classzDefine adapter_class in viewaccess_tokentoken_secret)oauth_tokenoauth_token_secretz"oauth_api.twitter.com_access_token)r   r   T)connectuser)r   r   r   r   ValidationErrorgetattrget_providerget_appsessionr   r   r   r   r   stris_existinglookupsaveaccountr!   )r   attrsr   r
   r   r   r   r   r   r   loginer   r   r   validate,   s8    


 zTwitterLoginSerializer.validateN)
__name__
__module____qualname__r   	CharFieldr   r   r   r   r/   r   r   r   r   r	      s
   r	   c                   @   s   e Zd ZdS )TwitterConnectSerializerN)r0   r1   r2   r   r   r   r   r4   T   s   r4   N)django.confr   django.httpr   rest_frameworkr   INSTALLED_APPSallauth.socialaccount.helpersr   allauth.socialaccount.modelsr   ,allauth.socialaccount.providers.oauth.clientr   %dj_rest_auth.registration.serializersr   
Serializerr	   r4   r   r   r   r   <module>   s   
D