From 2fff7a6254f370fcdc7bfdcd3ee538a8ac790237 Mon Sep 17 00:00:00 2001 From: Philippe Luickx Date: Tue, 14 Apr 2015 12:12:28 +0300 Subject: [PATCH] register update --- rest_auth/registration/views.py | 35 ++++++++++++++++++++++++++++----- rest_auth/views.py | 6 ++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/rest_auth/registration/views.py b/rest_auth/registration/views.py index ab9c705..2642e8f 100644 --- a/rest_auth/registration/views.py +++ b/rest_auth/registration/views.py @@ -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) diff --git a/rest_auth/views.py b/rest_auth/views.py index 94f4609..33d93b1 100644 --- a/rest_auth/views.py +++ b/rest_auth/views.py @@ -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