register update

This commit is contained in:
Philippe Luickx 2015-04-14 12:12:28 +03:00
parent 6450439520
commit 71a1071208
2 changed files with 34 additions and 7 deletions

View File

@ -2,21 +2,42 @@ from django.http import HttpRequest
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework.authtoken.models import Token
from rest_framework import status
from allauth.account.views import SignupView, ConfirmEmailView
from allauth.account.utils import complete_signup
from allauth.account import app_settings
from rest_auth.app_settings import UserDetailsSerializer
from rest_auth.app_settings import (
UserDetailsSerializer,
TokenSerializer,
)
from rest_auth.registration.serializers import SocialLoginSerializer
from rest_auth.views import Login
from rest_auth.views import (
Login,
EverybodyCanAuthentication,
)
class Register(APIView, SignupView):
"""
Accepts the credentials and creates a new user
if user does not exist already
Return the REST Token and the user object
if the credentials are valid and authenticated.
Calls allauth complete_signup method
Accept the following POST parameters: username, password
Return the REST Framework Token Object's key
and user object.
"""
permission_classes = (AllowAny,)
user_serializer_class = UserDetailsSerializer
authentication_classes = (EverybodyCanAuthentication,)
token_model = Token
token_serializer = TokenSerializer
user_serializer = UserDetailsSerializer
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
def get(self, *args, **kwargs):
@ -27,6 +48,8 @@ class Register(APIView, SignupView):
def form_valid(self, form):
self.user = form.save(self.request)
self.token, created = self.token_model.objects.get_or_create(
user=self.user)
if isinstance(self.request, HttpRequest):
request = self.request
else:
@ -47,8 +70,10 @@ class Register(APIView, SignupView):
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)
response = self.token_serializer(self.token).data
user = self.user_serializer(instance=self.user).data
response['user'] = user
return Response(response, status=status.HTTP_201_CREATED)
def get_response_with_errors(self):
return Response(self.form.errors, status=status.HTTP_400_BAD_REQUEST)

View File

@ -27,16 +27,18 @@ class EverybodyCanAuthentication(SessionAuthentication):
class Login(GenericAPIView):
"""
Check the credentials and return the REST Token
and the user object
if the credentials are valid and authenticated.
Calls Django Auth login method to register User ID
in Django session framework
Accept the following POST parameters: username, password
Return the REST Framework Token Object's key.
Return the REST Framework Token Object's key
and user object.
"""
permission_classes = (AllowAny,)
authentication_classes = (EverybodyCanAuthentication,)
serializer_class = LoginSerializer