server/authentication/views.py
author rougeronj
Thu, 02 Apr 2015 11:57:39 +0200
changeset 51 032280909e65
child 61 5abd0a9eafbc
permissions -rw-r--r--
add authentication module using Token Auth
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     1
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     2
import json
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     3
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     4
from django.contrib.auth import get_user_model
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     5
import requests
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     6
from rest_framework import serializers, status, permissions
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     7
from rest_framework.authentication import TokenAuthentication
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     8
from rest_framework.authtoken.models import Token
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     9
from rest_framework.exceptions import ParseError
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    10
from rest_framework.permissions import IsAuthenticated
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    11
from rest_framework.response import Response
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    12
from rest_framework.views import APIView
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    13
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    14
from config import URL_JAMESPOT
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    15
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    16
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    17
class UserSerializer(serializers.ModelSerializer):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    18
    class Meta:
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    19
        model = get_user_model()
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    20
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    21
class User(APIView):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    22
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    23
    get list user or add user
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    24
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    25
    permission_classes = (permissions.AllowAny,)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    26
    
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    27
    def get(self, request):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    28
        print ("here ?")
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    29
        user = get_user_model().objects.all()
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    30
        serializer = UserSerializer(user, many=True)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    31
        return Response(serializer.data)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    32
        
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    33
    def post(self, request):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    34
        VALID_USER_FIELDS = [f.name for f in get_user_model()._meta.fields]
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    35
        DEFAULTS = {
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    36
            "groups":"",
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    37
            "user_permissions":""
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    38
        }
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    39
        request.data.update(DEFAULTS)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    40
        serialized = UserSerializer(data=request.data)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    41
        
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    42
        if serialized.is_valid():
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    43
            user_data = {field: data for (field, data) in request.DATA.items() if field in VALID_USER_FIELDS}
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    44
            
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    45
            params = {'o': 'user', 'f': 'get', 'mail': user_data['email']}
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    46
            r = requests.get(URL_JAMESPOT, params=params)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    47
            infoUser = json.loads(r.content.decode('utf-8'))
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    48
            
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    49
            user_data.update({"idUser":infoUser['VAL']['idUser']})
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    50
            
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    51
            user = get_user_model().objects.create_user(
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    52
                **user_data
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    53
            )
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    54
            return Response(UserSerializer(instance=user).data, status=status.HTTP_201_CREATED)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    55
        else:
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    56
            return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    57
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    58
class AuthView(APIView):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    59
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    60
    Authentication is needed for this methods
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    61
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    62
    authentication_classes = (TokenAuthentication,)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    63
    permission_classes = (IsAuthenticated,)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    64
 
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    65
    def get(self, request, format=None):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    66
        return Response({'detail': "I suppose you are authenticated"})