Commit fda086f06b33e2aac913d638d43cd644f4ba0488
1 parent
3c58e75f
Exists in
master
and in
2 other branches
API login function modifications
Showing
1 changed file
with
21 additions
and
7 deletions
Show diff stats
api/views.py
| ... | ... | @@ -4,7 +4,7 @@ from django.contrib.auth import authenticate |
| 4 | 4 | from django.views.decorators.csrf import csrf_exempt |
| 5 | 5 | from rest_framework import viewsets |
| 6 | 6 | from rest_framework.response import Response |
| 7 | -from rest_framework.decorators import detail_route | |
| 7 | +from rest_framework.decorators import detail_route, list_route | |
| 8 | 8 | from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly |
| 9 | 9 | |
| 10 | 10 | from security.models import Security |
| ... | ... | @@ -18,18 +18,32 @@ from django.http import HttpResponse |
| 18 | 18 | |
| 19 | 19 | class LoginViewset(viewsets.ReadOnlyModelViewSet): |
| 20 | 20 | queryset = User.objects.all() |
| 21 | - permissions_classes = (IsAuthenticatedOrReadOnly,) | |
| 21 | + permissions_classes = (IsAuthenticated,) | |
| 22 | 22 | |
| 23 | 23 | @csrf_exempt |
| 24 | - @detail_route(methods = ['post']) | |
| 24 | + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) | |
| 25 | 25 | def login(self, request): |
| 26 | - username = request.DATA['email'] | |
| 26 | + username = request.data['email'] | |
| 27 | 27 | |
| 28 | - user = get_object_or_404(self.queryset, email = username) | |
| 28 | + user = self.queryset.get(email = username) | |
| 29 | 29 | |
| 30 | - serializer = UserSerializer(user) | |
| 30 | + if not user is None: | |
| 31 | + serializer = UserSerializer(user) | |
| 32 | + | |
| 33 | + json_r = json.dumps(serializer.data) | |
| 34 | + json_r = json.loads(json_r) | |
| 35 | + print(type(serializer.data)) | |
| 36 | + | |
| 37 | + json_r["message"] = "" | |
| 38 | + json_r["type"] = "" | |
| 39 | + json_r["title"] = "" | |
| 40 | + json_r["success"] = True | |
| 41 | + json_r["number"] = 1 | |
| 42 | + json_r['extra'] = 0 | |
| 43 | + | |
| 44 | + response = json.dumps(json_r) | |
| 31 | 45 | |
| 32 | - return Response(serializer.data) | |
| 46 | + return HttpResponse(response) | |
| 33 | 47 | |
| 34 | 48 | @csrf_exempt |
| 35 | 49 | def getToken(request): | ... | ... |