custom ObtainToken function to add PopulateVisite function when a user connects
authorrougeronj
Fri, 03 Apr 2015 15:01:41 +0200
changeset 61 5abd0a9eafbc
parent 60 7d624c1013a3
child 62 a5f9739dcd46
custom ObtainToken function to add PopulateVisite function when a user connects
server/authentication/urls.py
server/authentication/views.py
--- a/server/authentication/urls.py	Fri Apr 03 10:44:34 2015 +0200
+++ b/server/authentication/urls.py	Fri Apr 03 15:01:41 2015 +0200
@@ -1,11 +1,10 @@
 from django.conf.urls import patterns, url
 from rest_framework.authtoken import views
 
-from authentication.views import User, AuthView
+from authentication.views import User, ObtainAuthToken
 
 
 urlpatterns = patterns('',
     url(r'^user', User.as_view()),
-    url(r'^auth', AuthView.as_view(), name='auth-view'),
-    url(r'^api-token-auth', views.obtain_auth_token)
+    url(r'^api-token-auth', ObtainAuthToken.as_view())
 )
--- a/server/authentication/views.py	Fri Apr 03 10:44:34 2015 +0200
+++ b/server/authentication/views.py	Fri Apr 03 15:01:41 2015 +0200
@@ -3,15 +3,14 @@
 
 from django.contrib.auth import get_user_model
 import requests
-from rest_framework import serializers, status, permissions
-from rest_framework.authentication import TokenAuthentication
+from rest_framework import serializers, status, permissions, parsers, renderers
 from rest_framework.authtoken.models import Token
-from rest_framework.exceptions import ParseError
-from rest_framework.permissions import IsAuthenticated
+from rest_framework.authtoken.serializers import AuthTokenSerializer
 from rest_framework.response import Response
 from rest_framework.views import APIView
 
 from config import URL_JAMESPOT
+from ammico.views import populateVisite
 
 
 class UserSerializer(serializers.ModelSerializer):
@@ -45,22 +44,25 @@
             params = {'o': 'user', 'f': 'get', 'mail': user_data['email']}
             r = requests.get(URL_JAMESPOT, params=params)
             infoUser = json.loads(r.content.decode('utf-8'))
-            
-            user_data.update({"idUser":infoUser['VAL']['idUser']})
-            
+            if ('idUser' in infoUser['VAL']):
+                user_data.update({"idUser":infoUser['VAL']['idUser']})
             user = get_user_model().objects.create_user(
                 **user_data
             )
             return Response(UserSerializer(instance=user).data, status=status.HTTP_201_CREATED)
         else:
             return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
+        
+class ObtainAuthToken(APIView):
+    throttle_classes = ()
+    permission_classes = ()
+    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
+    renderer_classes = (renderers.JSONRenderer,)
 
-class AuthView(APIView):
-    """
-    Authentication is needed for this methods
-    """
-    authentication_classes = (TokenAuthentication,)
-    permission_classes = (IsAuthenticated,)
- 
-    def get(self, request, format=None):
-        return Response({'detail': "I suppose you are authenticated"})
\ No newline at end of file
+    def post(self, request):
+        serializer = AuthTokenSerializer(data=request.data)
+        serializer.is_valid(raise_exception=True)
+        user = serializer.validated_data['user']
+        populateVisite(user)
+        token, created = Token.objects.get_or_create(user=user)
+        return Response({'token': token.key})
\ No newline at end of file