2016-02-29 15:57:54 +03:00
|
|
|
from django.conf import settings
|
2016-12-31 23:55:19 +03:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.views.decorators.debug import sensitive_post_parameters
|
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
|
2017-12-02 17:00:16 +03:00
|
|
|
from rest_framework.permissions import (AllowAny,
|
|
|
|
IsAuthenticated)
|
2018-01-19 05:08:41 +03:00
|
|
|
from rest_framework.generics import CreateAPIView, ListAPIView, GenericAPIView
|
|
|
|
from rest_framework.exceptions import NotFound
|
2014-10-01 16:13:21 +04:00
|
|
|
from rest_framework import status
|
|
|
|
|
2018-01-19 05:08:41 +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
|
2018-01-19 05:08:41 +03:00
|
|
|
from allauth.socialaccount import signals
|
|
|
|
from allauth.socialaccount.adapter import get_adapter as get_social_adapter
|
|
|
|
from allauth.socialaccount.models import SocialAccount
|
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)
|
2016-12-31 23:55:19 +03:00
|
|
|
from rest_auth.models import TokenModel
|
2018-01-19 05:08:41 +03:00
|
|
|
from rest_auth.registration.serializers import (VerifyEmailSerializer,
|
|
|
|
SocialLoginSerializer,
|
|
|
|
SocialAccountSerializer,
|
2017-12-02 17:00:16 +03:00
|
|
|
SocialConnectSerializer)
|
2016-12-31 23:55:19 +03:00
|
|
|
from rest_auth.utils import jwt_encode
|
2015-08-07 13:54:45 +03:00
|
|
|
from rest_auth.views import LoginView
|
2017-02-13 02:35:28 +03:00
|
|
|
from .app_settings import RegisterSerializer, register_permission_classes
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2016-12-31 23:55:19 +03:00
|
|
|
sensitive_post_parameters_m = method_decorator(
|
|
|
|
sensitive_post_parameters('password1', 'password2')
|
|
|
|
)
|
2016-01-04 20:45:33 +03:00
|
|
|
|
2016-03-01 14:51:01 +03:00
|
|
|
|
2015-11-24 13:11:46 +03:00
|
|
|
class RegisterView(CreateAPIView):
|
|
|
|
serializer_class = RegisterSerializer
|
2017-02-13 02:35:28 +03:00
|
|
|
permission_classes = register_permission_classes()
|
2016-01-01 00:10:52 +03:00
|
|
|
token_model = TokenModel
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2016-12-31 23:55:19 +03:00
|
|
|
@sensitive_post_parameters_m
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(RegisterView, self).dispatch(*args, **kwargs)
|
|
|
|
|
2016-01-12 00:33:14 +03:00
|
|
|
def get_response_data(self, user):
|
|
|
|
if allauth_settings.EMAIL_VERIFICATION == \
|
|
|
|
allauth_settings.EmailVerificationMethod.MANDATORY:
|
2016-12-22 01:08:56 +03:00
|
|
|
return {"detail": _("Verification e-mail sent.")}
|
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-12-22 01:08:56 +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)
|
2016-07-28 21:14:26 +03:00
|
|
|
|
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')
|
|
|
|
|
2016-12-09 12:53:09 +03:00
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
return VerifyEmailSerializer(*args, **kwargs)
|
|
|
|
|
2014-10-01 18:34:51 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
2016-12-09 12:53:09 +03:00
|
|
|
serializer = self.get_serializer(data=request.data)
|
2015-11-24 13:11:46 +03:00
|
|
|
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-03-31 11:58:14 +03:00
|
|
|
return Response({'detail': _('ok')}, status=status.HTTP_200_OK)
|
2014-10-02 18:54:55 +04:00
|
|
|
|
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialLoginView(LoginView):
|
|
|
|
"""
|
|
|
|
class used for social authentications
|
|
|
|
example usage for facebook with access_token
|
|
|
|
-------------
|
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
2014-10-02 18:54:55 +04:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class FacebookLogin(SocialLoginView):
|
|
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
-------------
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
example usage for facebook with code
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
-------------
|
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
|
|
|
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
|
2015-08-07 13:26:57 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class FacebookLogin(SocialLoginView):
|
|
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
client_class = OAuth2Client
|
|
|
|
callback_url = 'localhost:8000'
|
|
|
|
-------------
|
|
|
|
"""
|
|
|
|
serializer_class = SocialLoginSerializer
|
2014-10-02 18:54:55 +04:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def process_login(self):
|
|
|
|
get_adapter(self.request).login(self.request, self.user)
|
2016-07-18 08:06:28 +03:00
|
|
|
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialConnectView(LoginView):
|
|
|
|
"""
|
|
|
|
class used for social account linking
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
example usage for facebook with access_token
|
|
|
|
-------------
|
|
|
|
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class FacebookConnect(SocialConnectView):
|
|
|
|
adapter_class = FacebookOAuth2Adapter
|
|
|
|
-------------
|
|
|
|
"""
|
|
|
|
serializer_class = SocialConnectSerializer
|
|
|
|
permission_classes = (IsAuthenticated,)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def process_login(self):
|
|
|
|
get_adapter(self.request).login(self.request, self.user)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialAccountListView(ListAPIView):
|
|
|
|
"""
|
|
|
|
List SocialAccounts for the currently logged in user
|
|
|
|
"""
|
|
|
|
serializer_class = SocialAccountSerializer
|
|
|
|
permission_classes = (IsAuthenticated,)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def get_queryset(self):
|
|
|
|
return SocialAccount.objects.filter(user=self.request.user)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
class SocialAccountDisconnectView(GenericAPIView):
|
|
|
|
"""
|
|
|
|
Disconnect SocialAccount from remote service for
|
|
|
|
the currently logged in user
|
|
|
|
"""
|
|
|
|
serializer_class = SocialConnectSerializer
|
|
|
|
permission_classes = (IsAuthenticated,)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def get_queryset(self):
|
|
|
|
return SocialAccount.objects.filter(user=self.request.user)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
accounts = self.get_queryset()
|
|
|
|
account = accounts.filter(pk=kwargs['pk']).first()
|
|
|
|
if not account:
|
|
|
|
raise NotFound
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
get_social_adapter(self.request).validate_disconnect(account, accounts)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
account.delete()
|
|
|
|
signals.social_account_removed.send(
|
|
|
|
sender=SocialAccount,
|
|
|
|
request=self.request,
|
|
|
|
socialaccount=account
|
|
|
|
)
|
2017-12-02 17:00:16 +03:00
|
|
|
|
2018-01-19 05:08:41 +03:00
|
|
|
return Response(self.get_serializer(account).data)
|