server/ammicosrv/authentication/views.py
author rougeronj
Wed, 10 Jun 2015 10:57:27 +0200
changeset 149 8e117699857a
parent 143 server/src/authentication/views.py@ea633c8f9bfa
parent 135 server/src/authentication/views.py@27065f8a19d3
child 150 78e82bf8ff89
permissions -rw-r--r--
Merge with 925337e6983dc5e5a011681dafd37ce986e07a1a
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
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     8
from rest_framework.response import Response
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
     9
from rest_framework.views import APIView
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    10
143
ea633c8f9bfa add exposition table and update the views to include the new foreign key to the exposition table
rougeronj
parents: 117
diff changeset
    11
from config import URL_JAMESPOT
135
27065f8a19d3 correct rename of src
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    12
from ammicosrv.ammico.views import populateVisit
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    13
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    14
143
ea633c8f9bfa add exposition table and update the views to include the new foreign key to the exposition table
rougeronj
parents: 117
diff changeset
    15
#from ammico.views import populateVisit
51
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
        user = get_user_model().objects.all()
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    28
        serializer = UserSerializer(user, many=True)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    29
        return Response(serializer.data)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    30
        
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    31
    def post(self, request):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    32
        VALID_USER_FIELDS = [f.name for f in get_user_model()._meta.fields]
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    33
        serialized = UserSerializer(data=request.data)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    34
        
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    35
        if serialized.is_valid():
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    36
            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
    37
            
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    38
            params = {'o': 'user', 'f': 'get', 'mail': user_data['email']}
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    39
            r = requests.get(URL_JAMESPOT, params=params)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    40
            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
    41
            if ('idUser' in infoUser['VAL']):
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    42
                user_data.update({"idUser":infoUser['VAL']['idUser']})
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    43
            user = get_user_model().objects.create_user(
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    44
                **user_data
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    45
            )
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    46
            return Response(UserSerializer(instance=user).data, status=status.HTTP_201_CREATED)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    47
        else:
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    48
            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
    49
        
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    50
class ObtainAuthToken(APIView):
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    51
    throttle_classes = ()
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    52
    permission_classes = ()
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    53
    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
    54
    renderer_classes = (renderers.JSONRenderer,)
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    55
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    56
    def post(self, request):
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 87
diff changeset
    57
        user = get_user_model().objects.get(email = request.data['email'], password = request.data['password'])
143
ea633c8f9bfa add exposition table and update the views to include the new foreign key to the exposition table
rougeronj
parents: 117
diff changeset
    58
        if (user.idUser and 'idExpo' in request.GET):
ea633c8f9bfa add exposition table and update the views to include the new foreign key to the exposition table
rougeronj
parents: 117
diff changeset
    59
            populateVisit(user, request.GET['idExpo']);
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 87
diff changeset
    60
        token, _ = Token.objects.get_or_create(user=user)
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    61
        return Response({'token': token.key})