-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
319 lines (284 loc) · 10.4 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from accounts.serializers import (
ResetSerializer,
UserSerailizer,
LoginSerializer,
ResetPasswordSeriliazer,
)
from .models import User
from rest_framework import status
from rest_framework_simplejwt.tokens import RefreshToken
from django.contrib.auth.hashers import check_password
from .tokens import account_activation_token
from django.core.mail import send_mail
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.utils.encoding import force_str, force_bytes
from django.contrib.auth import logout
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.permissions import IsAuthenticated
# Create your views here.
class Register(GenericAPIView):
serializer_class = UserSerailizer
def post(self, request, format="json"):
num_results = User.objects.filter(email=request.data["email"]).count()
if num_results > 0:
return Response(
{
"success": False,
"detail": "User already exists",
},
status=status.HTTP_400_BAD_REQUEST,
)
serializer = self.serializer_class(data=request.data)
if serializer.is_valid(raise_exception=True):
user = serializer.save()
user.is_active = True
user.save()
# if user:
# subject = "Activate your account"
# message = "Hi, " + user.first_name + " " + user.last_name + "!\n\n"
# message += "Please click the link below to activate your account:\n\n"
# message += (
# "http://localhost:8000/api/v1/auth/confirm-email/"
# + urlsafe_base64_encode(force_bytes(user.id))
# + "/"
# + account_activation_token.make_token(user)
# + "\n\n"
# )
# message += "Thank you for using our application!"
# from_email = "EMAIL_HOST_USER"
# to_email = [user.email]
# send_mail(subject, message, from_email, to_email)
return Response(
{
"success": True,
"detail": "User created successfully.",
},
status=status.HTTP_201_CREATED,
)
return Response(
{
"success": False,
"detail": "User already exists",
},
status=status.HTTP_400_BAD_REQUEST,
)
class Login(GenericAPIView):
serializer_class = LoginSerializer
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.data
try:
user = User.objects.get(email=data["email"])
except User.DoesNotExist:
return Response(
{
"success": False,
"detail": "Email or password is incorrect",
},
status=status.HTTP_401_UNAUTHORIZED,
)
current_password = user.password
check = check_password(data['password'], current_password)
if check:
if user.is_active:
refresh = RefreshToken.for_user(user)
return Response(
{
"success": True,
"detail": "Login Successful",
"data": {
"user": {
"user_id": user.pk,
"email": user.email,
},
"refresh": str(refresh),
"access": str(refresh.access_token),
},
}
)
else:
return Response(
{
"success": False,
"detail": "User is not active",
},
status=status.HTTP_401_UNAUTHORIZED,
)
else:
return Response(
{
"success": False,
"detail": "Email or password is not correct.",
},
status=status.HTTP_401_UNAUTHORIZED,
)
class ResendActivationLink(GenericAPIView):
serializer_class = ResetSerializer
def get(self, request, email):
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
return Response(
{
"success": False,
"detail": "User does not exist"
},
status=status.HTTP_400_BAD_REQUEST,
)
if user.is_active:
return Response(
{
"success": False,
"detail": "User is already active"
},
status=status.HTTP_400_BAD_REQUEST,
)
subject = "Activate your account"
message = "Hi, " + user.first_name + " " + user.last_name + "!\n\n"
message += "Please click the link below to activate your account:\n\n"
message += (
"http://localhost:8000/api/v1/auth/confirm-email/"
+ urlsafe_base64_encode(force_bytes(user.id))
+ "/"
+ account_activation_token.make_token(user)
+ "\n\n"
)
message += "Thank you for using our application!"
from_email = "EMAIL_HOST_USER"
to_email = [user.email]
send_mail(subject, message, from_email, to_email)
return Response(
{
"success": True,
"detail": "Email sent", "data": message
},
status=status.HTTP_200_OK,
)
class ActivateEmail(GenericAPIView):
def get(self, request, user_id, token):
try:
user_id = force_str(urlsafe_base64_decode(user_id))
user = User.objects.get(pk=user_id)
except User.DoesNotExist:
return Response(
{
"success": False,
"detail": "User does not exist"
},
status=status.HTTP_400_BAD_REQUEST,
)
if user and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
return Response(
{
"success": True,
"detail": "Email activated successfully"
},
status=status.HTTP_200_OK,
)
else:
return Response(
{
"success": False,
"detail": "Invalid token"
},
status=status.HTTP_400_BAD_REQUEST,
)
class ResetPassword(GenericAPIView):
serializer_class = ResetSerializer
def get(self, request, email):
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
return Response(
{
"success": False,
"detail": "User does not exist."
}, status=status.HTTP_401_UNAUTHORIZED
)
if user.is_active:
subject = "Reset Your Password"
message = "Hi, " + user.first_name + " " + user.last_name + "!\n\n"
message += "Please click the link below to activate your account:\n\n"
message += (
"http://localhost:8000/api/v1/auth/reset-password-confirm/"
+ urlsafe_base64_encode(force_bytes(user.id))
+ "/"
+ account_activation_token.make_token(user)
+ "\n\n"
)
message += "Thank you for using our application!"
from_email = "EMAIL_HOST_USER"
to_email = [user.email]
send_mail(subject, message, from_email, to_email)
return Response(
{
"success": True,
"detail": "Check your mail to reset your password",
"data": message
},
status=status.HTTP_200_OK,
)
else:
return Response(
{
"success": False,
"detail": "User is not active. Request for an activation link."
},
status=status.HTTP_400_BAD_REQUEST,
)
class ConfirmPassword(GenericAPIView):
serializer_class = ResetPasswordSeriliazer
def put(self, request, user_id, token):
try:
user_id = force_str(urlsafe_base64_decode(user_id))
user = User.objects.get(pk=user_id)
except User.DoesNotExist:
return Response(
{
"success": False,
"detail": "User does not exist"
},
status=status.HTTP_400_BAD_REQUEST,
)
if user and account_activation_token.check_token(user, token):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
user.set_password(request.data.get("password"))
user.save()
return Response(
{
"success": True,
"detail": "New password set."
}, status=status.HTTP_200_OK
)
else:
return Response(
{
"success": False,
"detail": serializer.errors}, status=status.HTTP_400_BAD_REQUEST
)
else:
return Response(
{
"success": False,
"detail": "Password Reset Failed!"},
status=status.HTTP_400_BAD_REQUEST,
)
class Logout(GenericAPIView):
permission_classes = [IsAuthenticated]
authentication_classes = [JWTAuthentication]
def get(self, request):
request.user.access_token.delete()
logout(request)
return Response(
{
"success": True,
"detail": "Logged out successfully"
},
status=status.HTTP_200_OK,
)