server/ammicosrv/authentication/views.py
author ymh <ymh.work@gmail.com>
Mon, 08 Jun 2015 09:36:41 +0200
changeset 135 27065f8a19d3
parent 134 server/ammicossrv/authentication/views.py@a84aa262847c
child 149 8e117699857a
permissions -rw-r--r--
correct rename of src
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
135
27065f8a19d3 correct rename of src
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    11
from ammicosrv.ammico.views import populateVisit
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    12
from django.conf import settings
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
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    15
class UserSerializer(serializers.ModelSerializer):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    16
    class Meta:
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    17
        model = get_user_model()
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    18
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    19
class User(APIView):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    20
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    21
    get list user or add user
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    22
    """
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    23
    permission_classes = (permissions.AllowAny,)
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    24
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    25
    def get(self, request):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    26
        user = get_user_model().objects.all()
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    27
        serializer = UserSerializer(user, many=True)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    28
        return Response(serializer.data)
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    29
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    30
    def post(self, request):
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    31
        VALID_USER_FIELDS = [f.name for f in get_user_model()._meta.fields]
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    32
        serialized = UserSerializer(data=request.data)
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    33
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    34
        if serialized.is_valid():
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    35
            user_data = {field: data for (field, data) in request.DATA.items() if field in VALID_USER_FIELDS}
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    36
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    37
            params = {'o': 'user', 'f': 'get', 'mail': user_data['email']}
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    38
            r = requests.get(settings.URL_JAMESPOT, params=params)
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    39
            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
    40
            if ('idUser' in infoUser['VAL']):
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    41
                user_data.update({"idUser":infoUser['VAL']['idUser']})
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    42
            user = get_user_model().objects.create_user(
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    43
                **user_data
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    44
            )
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    45
            return Response(UserSerializer(instance=user).data, status=status.HTTP_201_CREATED)
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    46
        else:
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    47
            return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    48
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    49
class ObtainAuthToken(APIView):
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    50
    throttle_classes = ()
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    51
    permission_classes = ()
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    52
    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
    53
    renderer_classes = (renderers.JSONRenderer,)
51
032280909e65 add authentication module using Token Auth
rougeronj
parents:
diff changeset
    54
61
5abd0a9eafbc custom ObtainToken function to add PopulateVisite function when a user connects
rougeronj
parents: 51
diff changeset
    55
    def post(self, request):
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 87
diff changeset
    56
        user = get_user_model().objects.get(email = request.data['email'], password = request.data['password'])
87
7b43de480a11 get the books and slides from Jamespot in case the user have a idUser (is a registered user in jamespot plateform)
rougeronj
parents: 72
diff changeset
    57
        if (user.idUser):
7b43de480a11 get the books and slides from Jamespot in case the user have a idUser (is a registered user in jamespot plateform)
rougeronj
parents: 72
diff changeset
    58
            populateVisit(user)
108
4a152f5f4a09 update user model to authenticate with the email as username
rougeronj
parents: 87
diff changeset
    59
        token, _ = Token.objects.get_or_create(user=user)
118
fea47f1054e2 prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    60
        return Response({'token': token.key})