2016-02-23 13:59:47 +03:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-02-29 15:57:54 +03:00
|
|
|
from django.conf import settings
|
2016-01-04 20:45:33 +03:00
|
|
|
|
2014-10-01 16:13:21 +04:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.permissions import AllowAny
|
2015-11-24 13:11:46 +03:00
|
|
|
from rest_framework.generics import CreateAPIView
|
2014-10-01 16:13:21 +04:00
|
|
|
from rest_framework import status
|
|
|
|
|
2016-07-18 08:06:28 +03:00
|
|
|
from allauth.account.adapter import get_adapter
|
2015-11-24 13:11:46 +03:00
|
|
|
from allauth.account.views import ConfirmEmailView
|
2014-10-01 16:13:21 +04:00
|
|
|
from allauth.account.utils import complete_signup
|
2015-11-24 13:11:46 +03:00
|
|
|
from allauth.account import app_settings as allauth_settings
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2016-01-08 00:45:28 +03:00
|
|
|
from rest_auth.app_settings import (TokenSerializer,
|
2016-02-16 08:42:18 +03:00
|
|
|
JWTSerializer,
|
2016-01-08 00:45:28 +03:00
|
|
|
create_token)
|
2015-11-24 13:11:46 +03:00
|
|
|
from rest_auth.registration.serializers import (SocialLoginSerializer,
|
|
|
|
VerifyEmailSerializer)
|
2015-08-07 13:54:45 +03:00
|
|
|
from rest_auth.views import LoginView
|
2016-01-01 00:10:52 +03:00
|
|
|
from rest_auth.models import TokenModel
|
2016-01-08 00:45:28 +03:00
|
|
|
from .app_settings import RegisterSerializer
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2016-01-04 20:45:33 +03:00
|
|
|
from rest_auth.utils import jwt_encode
|
|
|
|
|
2016-03-01 14:51:01 +03:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
class RegisterView(CreateAPIView):
|
|
|
|
serializer_class = RegisterSerializer
|
2015-11-24 17:07:20 +03:00
|
|
|
permission_classes = (AllowAny, )
|
2016-01-01 00:10:52 +03:00
|
|
|
token_model = TokenModel
|
2016-05-05 09:03:34 +03:00
|
|
|
throttle_scope = 'register_view'
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2016-01-12 00:33:14 +03:00
|
|
|
def get_response_data(self, user):
|
|
|
|
if allauth_settings.EMAIL_VERIFICATION == \
|
|
|
|
allauth_settings.EmailVerificationMethod.MANDATORY:
|
|
|
|
return {}
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2016-02-16 08:42:18 +03:00
|
|
|
if getattr(settings, 'REST_USE_JWT', False):
|
|
|
|
data = {
|
|
|
|
'user': user,
|
|
|
|
'token': self.token
|
|
|
|
}
|
|
|
|
return JWTSerializer(data).data
|
|
|
|
else:
|
|
|
|
return TokenSerializer(user.auth_token).data
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
user = self.perform_create(serializer)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
2014-11-12 05:52:06 +03:00
|
|
|
|
2016-01-12 00:33:14 +03:00
|
|
|
return Response(self.get_response_data(user), status=status.HTTP_201_CREATED, headers=headers)
|
2015-11-06 16:07:12 +03:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
user = serializer.save(self.request)
|
2016-02-16 08:42:18 +03:00
|
|
|
if getattr(settings, 'REST_USE_JWT', False):
|
|
|
|
self.token = jwt_encode(user)
|
|
|
|
else:
|
|
|
|
create_token(self.token_model, user, serializer)
|
2015-11-24 13:11:46 +03:00
|
|
|
complete_signup(self.request._request, user,
|
|
|
|
allauth_settings.EMAIL_VERIFICATION,
|
2015-11-24 17:16:39 +03:00
|
|
|
None)
|
2015-11-24 13:11:46 +03:00
|
|
|
return user
|
2014-10-01 16:13:21 +04:00
|
|
|
|
|
|
|
|
2015-08-07 13:54:45 +03:00
|
|
|
class VerifyEmailView(APIView, ConfirmEmailView):
|
2014-10-01 18:34:51 +04:00
|
|
|
|
|
|
|
permission_classes = (AllowAny,)
|
2014-11-12 12:33:29 +03:00
|
|
|
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
|
|
|
|
2014-10-01 18:34:51 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
2015-11-24 13:11:46 +03:00
|
|
|
serializer = VerifyEmailSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
self.kwargs['key'] = serializer.validated_data['key']
|
2014-10-01 18:34:51 +04:00
|
|
|
confirmation = self.get_object()
|
|
|
|
confirmation.confirm(self.request)
|
2016-02-02 17:29:16 +03:00
|
|
|
return Response({'message': _('ok')}, status=status.HTTP_200_OK)
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
|
2015-08-07 13:54:45 +03:00
|
|
|
class SocialLoginView(LoginView):
|
2014-10-02 18:54:55 +04:00
|
|
|
"""
|
|
|
|
class used for social authentications
|
2015-08-07 13:26:57 +03:00
|
|
|
example usage for facebook with access_token
|
|
|
|
-------------
|
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
2014-10-02 18:54:55 +04:00
|
|
|
|
2015-08-07 13:54:45 +03:00
|
|
|
class FacebookLogin(SocialLoginView):
|
2015-08-07 13:26:57 +03:00
|
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
-------------
|
|
|
|
|
|
|
|
example usage for facebook with code
|
|
|
|
|
|
|
|
-------------
|
2014-10-02 18:54:55 +04:00
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
2015-08-07 13:26:57 +03:00
|
|
|
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
|
|
|
|
|
2015-08-07 13:54:45 +03:00
|
|
|
class FacebookLogin(SocialLoginView):
|
2014-10-02 18:54:55 +04:00
|
|
|
adapter_class = FacebookOAuth2Adapter
|
2015-08-07 13:26:57 +03:00
|
|
|
client_class = OAuth2Client
|
|
|
|
callback_url = 'localhost:8000'
|
|
|
|
-------------
|
2014-10-02 18:54:55 +04:00
|
|
|
"""
|
|
|
|
|
2016-02-29 16:01:39 +03:00
|
|
|
serializer_class = SocialLoginSerializer
|
2016-07-18 08:06:28 +03:00
|
|
|
|
|
|
|
def process_login(self):
|
|
|
|
get_adapter(self.request).login(self.request, self.user)
|