-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
90 lines (78 loc) · 2.78 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
This view module is used to specify the User Details
"""
from user.models import User
from user.serializers import UserSerializer
from user.serializers import UserDetailSerializer
from rest_framework import viewsets
from django.contrib.auth.views import logout
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import detail_route
from django.contrib.auth import authenticate, login
from rest_framework.permissions import AllowAny
import json
from rest_framework import status, views
from django.views.decorators.csrf import csrf_exempt
class UserDetailViewSet(viewsets.ModelViewSet):
# pylint: disable = too-many-ancestors
"""
A viewset for viewing and editing user instances.
"""
print("this is user detail top most")
permission_classes = (AllowAny,)
serializer_class = UserDetailSerializer
queryset = User.objects.all()
class AuthUserViewSet(APIView):
"""
AuthUserViewSet
"""
permission_classes = (AllowAny,)
queryset = User.objects.all()
serializer_class = UserSerializer
@detail_route(methods=['delete'], url_path='id')
def delete(self, request):
"""
this model delete will log the user out
"""
# user = self.get_object(request.user)
logout(request)
return Response(status=status.HTTP_204_NO_CONTENT)
print("in auth user model????????")
def post(self, request):
"""
this model will log the authenticated user in
"""
print("in auth user...post method")
email = request.data['email']
password = request.data['password']
account = authenticate(email=email, password=password)
print(account)
if account is not None:
if account.is_active:
login(request, account)
serialized = UserDetailSerializer(account)
return Response(serialized.data)
else:
return Response({
'status': 'Unauthorized',
'message': 'User Not active'
},status=status.HTTP_401_NO_CONTENT)
else:
return Response({
'status': 'Unauthorized',
'message': 'User not Authenticated'
},status=status.HTTP_401_NO_CONTENT)
class MeViewSet(APIView):
"""
this model will show thw details of user logged in "
"""
serializer_class = UserDetailSerializer
def get(self, request, email):
"""
this method will return logged in user details
"""
account = User.objects.get(email=email)
serializer = UserDetailSerializer(account)
return Response(serializer.data)