2015-01-07 19:02:25 +03:00
|
|
|
from django.http import HttpRequest
|
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
|
|
|
|
from rest_framework import status
|
2015-08-07 13:26:57 +03:00
|
|
|
from rest_framework.authtoken.models import Token
|
2014-10-01 16:13:21 +04:00
|
|
|
|
2014-10-01 18:34:51 +04:00
|
|
|
from allauth.account.views import SignupView, ConfirmEmailView
|
2014-10-01 16:13:21 +04:00
|
|
|
from allauth.account.utils import complete_signup
|
|
|
|
from allauth.account import app_settings
|
|
|
|
|
2015-08-14 14:49:47 +03:00
|
|
|
from rest_auth.app_settings import TokenSerializer
|
2014-10-02 18:54:55 +04:00
|
|
|
from rest_auth.registration.serializers import SocialLoginSerializer
|
2015-08-07 13:54:45 +03:00
|
|
|
from rest_auth.views import LoginView
|
2014-10-01 16:13:21 +04:00
|
|
|
|
|
|
|
|
2015-08-07 13:54:45 +03:00
|
|
|
class RegisterView(APIView, SignupView):
|
2015-08-07 13:26:57 +03:00
|
|
|
"""
|
|
|
|
Accepts the credentials and creates a new user
|
|
|
|
if user does not exist already
|
|
|
|
Return the REST Token if the credentials are valid and authenticated.
|
|
|
|
Calls allauth complete_signup method
|
|
|
|
|
|
|
|
Accept the following POST parameters: username, email, password
|
|
|
|
Return the REST Framework Token Object's key.
|
|
|
|
"""
|
2014-10-01 16:13:21 +04:00
|
|
|
|
|
|
|
permission_classes = (AllowAny,)
|
2014-11-12 05:52:06 +03:00
|
|
|
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
2015-08-07 13:26:57 +03:00
|
|
|
token_model = Token
|
|
|
|
serializer_class = TokenSerializer
|
2014-11-12 05:52:06 +03:00
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
return Response({}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
|
|
|
def put(self, *args, **kwargs):
|
|
|
|
return Response({}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
2014-10-01 16:13:21 +04:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.user = form.save(self.request)
|
2015-08-07 13:26:57 +03:00
|
|
|
self.token, created = self.token_model.objects.get_or_create(
|
|
|
|
user=self.user
|
|
|
|
)
|
2015-01-07 19:02:25 +03:00
|
|
|
if isinstance(self.request, HttpRequest):
|
|
|
|
request = self.request
|
|
|
|
else:
|
|
|
|
request = self.request._request
|
|
|
|
return complete_signup(request, self.user,
|
2014-10-01 16:13:21 +04:00
|
|
|
app_settings.EMAIL_VERIFICATION,
|
|
|
|
self.get_success_url())
|
|
|
|
|
2015-11-06 16:07:12 +03:00
|
|
|
def get_form_kwargs(self, *args, **kwargs):
|
|
|
|
kwargs = super(RegisterView, self).get_form_kwargs(*args, **kwargs)
|
|
|
|
kwargs['data'] = self.request.data
|
|
|
|
return kwargs
|
|
|
|
|
2014-10-01 16:13:21 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.initial = {}
|
|
|
|
form_class = self.get_form_class()
|
|
|
|
self.form = self.get_form(form_class)
|
|
|
|
if self.form.is_valid():
|
|
|
|
self.form_valid(self.form)
|
|
|
|
return self.get_response()
|
|
|
|
else:
|
|
|
|
return self.get_response_with_errors()
|
|
|
|
|
|
|
|
def get_response(self):
|
2015-08-07 13:26:57 +03:00
|
|
|
# serializer = self.user_serializer_class(instance=self.user)
|
2015-10-28 02:55:08 +03:00
|
|
|
serializer = self.serializer_class(instance=self.token,
|
2015-11-06 16:09:47 +03:00
|
|
|
context={'request': self.request})
|
2014-10-01 16:13:21 +04:00
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
def get_response_with_errors(self):
|
|
|
|
return Response(self.form.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
return Response({}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
2014-10-01 18:34:51 +04:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2015-08-07 14:31:33 +03:00
|
|
|
self.kwargs['key'] = self.request.data.get('key', '')
|
2014-10-01 18:34:51 +04:00
|
|
|
confirmation = self.get_object()
|
|
|
|
confirmation.confirm(self.request)
|
|
|
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
serializer_class = SocialLoginSerializer
|