-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
75 lines (55 loc) · 2.31 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
import json
from django.utils.translation import gettext_lazy as _
from django.http import Http404
from django.core.exceptions import ValidationError
from django.core.exceptions import ObjectDoesNotExist
from rest_framework.response import Response
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.generics import RetrieveUpdateAPIView
from users.renderers import APIJSONRenderer
from users.models import User
# from users.utilities.email import send_email_to_user
from users.serializers import (
UserRegisterSerializer,
LoginSerializer,
UserSerializer,
)
class UserLoginAPIView(APIView):
permission_classes = [AllowAny]
serializer_class = LoginSerializer
# renderer_classes = APIJSONRenderer
def post(self, request):
user = {
"email": request.data.get('email'),
"password": request.data.get('password')
}
try:
serializer = self.serializer_class(data=user)
serializer.is_valid(raise_exception=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Http404:
return Response({'error': 'User does not exist.'}, status=status.HTTP_404_NOT_FOUND)
except ValidationError:
return Response({'error': 'Invalid credentials.'}, status=status.HTTP_401_UNAUTHORIZED)
class UserRegisterAPIView(APIView):
permission_classes = [AllowAny]
serializer_class = UserRegisterSerializer
# renderer_classes = APIJSONRenderer
def post(self, request):
user = {
"email": request.data.get('email'),
"username": request.data.get('email'),
"password": request.data.get('password'),
"role": request.data.get('role')
}
serializer = self.serializer_class(data=user)
serializer.is_valid(raise_exception=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
print(serializer.errors)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)