django-rest-auth/rest_auth/registration/views.py

78 lines
2.6 KiB
Python
Raw Normal View History

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework import status
2014-10-01 18:34:51 +04:00
from allauth.account.views import SignupView, ConfirmEmailView
from allauth.account.utils import complete_signup
from allauth.account import app_settings
from ..serializers import UserDetailsSerializer
from rest_auth.registration.serializers import SocialLoginSerializer
from rest_auth.views import Login
class Register(APIView, SignupView):
permission_classes = (AllowAny,)
user_serializer_class = UserDetailsSerializer
2014-11-12 05:52:06 +03:00
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
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)
def form_valid(self, form):
self.user = form.save(self.request)
return complete_signup(self.request, self.user,
app_settings.EMAIL_VERIFICATION,
self.get_success_url())
def post(self, request, *args, **kwargs):
self.initial = {}
self.request.POST = self.request.DATA.copy()
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):
serializer = self.user_serializer_class(instance=self.user)
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)
2014-10-01 18:34:51 +04:00
class VerifyEmail(APIView, ConfirmEmailView):
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):
self.kwargs['key'] = self.request.DATA.get('key', '')
confirmation = self.get_object()
confirmation.confirm(self.request)
return Response({'message': 'ok'}, status=status.HTTP_200_OK)
class SocialLogin(Login):
"""
class used for social authentications
example usage for facebook
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
class FacebookLogin(SocialLogin):
adapter_class = FacebookOAuth2Adapter
"""
serializer_class = SocialLoginSerializer