server/authentication/views.py
author rougeronj
Wed, 08 Apr 2015 00:39:23 +0200
changeset 79 1be5cc51e359
parent 72 ed2ee692ff6f
child 87 7b43de480a11
permissions -rw-r--r--
Add url to access main page of the project and gulp "copy-server" to copy the ammico app to the server
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
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
     6
from rest_framework import serializers, status, permissions, parsers, renderers
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     7
from rest_framework.authtoken.models import Token
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
     8
from rest_framework.authtoken.serializers import AuthTokenSerializer
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     9
from rest_framework.response import Response
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    10
from rest_framework.views import APIView
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    11
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    12
from config import URL_JAMESPOT
72
ed2ee692ff6f Update PopulateVisit function to get only the new visits from Jamespot.
rougeronj
parents: 61
diff changeset
    13
from ammico.views import populateVisit
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    14
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    15
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    16
class UserSerializer(serializers.ModelSerializer):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    17
    class Meta:
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    18
        model = get_user_model()
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    19
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    20
class User(APIView):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    21
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    22
    get list user or add user
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    23
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    24
    permission_classes = (permissions.AllowAny,)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    25
    
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    26
    def get(self, request):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    27
        print ("here ?")
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    28
        user = get_user_model().objects.all()
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    29
        serializer = UserSerializer(user, many=True)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    30
        return Response(serializer.data)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    31
        
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    32
    def post(self, request):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    33
        VALID_USER_FIELDS = [f.name for f in get_user_model()._meta.fields]
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    34
        DEFAULTS = {
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    35
            "groups":"",
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    36
            "user_permissions":""
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    37
        }
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    38
        request.data.update(DEFAULTS)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    39
        serialized = UserSerializer(data=request.data)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    40
        
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    41
        if serialized.is_valid():
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    42
            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
    43
            
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    44
            params = {'o': 'user', 'f': 'get', 'mail': user_data['email']}
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    45
            r = requests.get(URL_JAMESPOT, params=params)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    46
            infoUser = json.loads(r.content.decode('utf-8'))
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    47
            if ('idUser' in infoUser['VAL']):
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    48
                user_data.update({"idUser":infoUser['VAL']['idUser']})
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    49
            user = get_user_model().objects.create_user(
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    50
                **user_data
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    51
            )
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    52
            return Response(UserSerializer(instance=user).data, status=status.HTTP_201_CREATED)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    53
        else:
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    54
            return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    55
        
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    56
class ObtainAuthToken(APIView):
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    57
    throttle_classes = ()
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    58
    permission_classes = ()
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    59
    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    60
    renderer_classes = (renderers.JSONRenderer,)
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    61
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    62
    def post(self, request):
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    63
        serializer = AuthTokenSerializer(data=request.data)
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    64
        serializer.is_valid(raise_exception=True)
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    65
        user = serializer.validated_data['user']
72
ed2ee692ff6f Update PopulateVisit function to get only the new visits from Jamespot.
rougeronj
parents: 61
diff changeset
    66
        populateVisit(user)
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    67
        token, created = Token.objects.get_or_create(user=user)
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    68
        return Response({'token': token.key})