forked from Omkar0231/django-login-and-signup-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonCodePart2.txt
More file actions
281 lines (214 loc) · 9.61 KB
/
Copy pathPythonCodePart2.txt
File metadata and controls
281 lines (214 loc) · 9.61 KB
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
<------------------------ serializer.py ------------------------>
from rest_framework import serializers
from django.contrib.auth import get_user_model, authenticate
User = get_user_model()
class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username','email','phone','password')
extra_kwargs = {'password': {'write_only' : True},}
def create(self, validated_data):
user = User.objects.create(**validated_data)
return user
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'phone',)
<------------------------ serializer.py ------------------------>
<------------------------ views.py ------------------------>
from django.shortcuts import render
from django.http import HttpResponse
import http.client
# Create your views here.
import json
import requests
import ast
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, status, generics
# from forms import UserForm
from .models import User, PhoneOTP
from django.shortcuts import get_object_or_404, redirect
import random
from .serializer import CreateUserSerializer, LoginSerializer
from knox.views import LoginView as KnoxLoginView
from knox.auth import TokenAuthentication
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from django.contrib.auth import login,logout
conn = http.client.HTTPConnection("2factor.in")
class ValidatePhoneSendOTP(APIView):
def post(self, request, *args, **kwargs):
phone_number = request.data.get('phone')
password = request.data.get('password', False)
username = request.data.get('username', False)
email = request.data.get('email', False)
if phone_number:
phone = str(phone_number)
user = User.objects.filter(phone__iexact = phone)
if user.exists():
return Response({
'status' : False,
'detail' : 'Phone number already exists'
})
else:
key = send_otp(phone)
if key:
old = PhoneOTP.objects.filter(phone__iexact = phone)
if old.exists():
old = old.first()
count = old.count
if count > 10:
return Response({
'status' : False,
'detail' : 'Sending otp error. Limit Exceeded. Please Contact Customer support'
})
old.count = count +1
old.save()
print('Count Increase', count)
conn.request("GET", "https://2factor.in/API/R1/?module=SMS_OTP&apikey=1028fcd9-3158-11ea-9fa5-0200cd936042&to="+phone+"&otpvalue="+str(key)+"&templatename=WomenMark1")
res = conn.getresponse()
data = res.read()
data=data.decode("utf-8")
data=ast.literal_eval(data)
if data["Status"] == 'Success':
old.otp_session_id = data["Details"]
old.save()
print('In validate phone :'+old.otp_session_id)
return Response({
'status' : True,
'detail' : 'OTP sent successfully'
})
else:
return Response({
'status' : False,
'detail' : 'OTP sending Failed'
})
else:
obj=PhoneOTP.objects.create(
phone=phone,
otp = key,
email=email,
username=username,
password=password,
)
conn.request("GET", "https://2factor.in/API/R1/?module=SMS_OTP&apikey=1028fcd9-3158-11ea-9fa5-0200cd936042&to="+phone+"&otpvalue="+str(key)+"&templatename=WomenMark1")
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
data=data.decode("utf-8")
data=ast.literal_eval(data)
if data["Status"] == 'Success':
obj.otp_session_id = data["Details"]
obj.save()
print('In validate phone :'+obj.otp_session_id)
return Response({
'status' : True,
'detail' : 'OTP sent successfully'
})
else:
return Response({
'status' : False,
'detail' : 'OTP sending Failed'
})
else:
return Response({
'status' : False,
'detail' : 'Sending otp error'
})
else:
return Response({
'status' : False,
'detail' : 'Phone number is not given in post request'
})
def send_otp(phone):
if phone:
key = random.randint(999,9999)
print(key)
return key
else:
return False
class ValidateOTP(APIView):
def post(self, request, *args, **kwargs):
phone = request.data.get('phone', False)
otp_sent = request.data.get('otp', False)
if phone and otp_sent:
old = PhoneOTP.objects.filter(phone__iexact = phone)
if old.exists():
old = old.first()
otp_session_id = old.otp_session_id
print("In validate otp"+otp_session_id)
conn.request("GET", "https://2factor.in/API/V1/1028fcd9-3158-11ea-9fa5-0200cd936042/SMS/VERIFY/"+otp_session_id+"/"+otp_sent)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
data=data.decode("utf-8")
data=ast.literal_eval(data)
if data["Status"] == 'Success':
old.validated = True
old.save()
return Response({
'status' : True,
'detail' : 'OTP MATCHED. Please proceed for registration.'
})
else:
return Response({
'status' : False,
'detail' : 'OTP INCORRECT'
})
else:
return Response({
'status' : False,
'detail' : 'First Proceed via sending otp request'
})
else:
return Response({
'status' : False,
'detail' : 'Please provide both phone and otp for Validation'
})
class Register(APIView):
def post(self, request, *args, **kwargs):
phone = request.data.get('phone', False)
password = request.data.get('password', False)
username = request.data.get('username', False)
email = request.data.get('email', False)
if phone and password and username and email :
old = PhoneOTP.objects.filter(phone__iexact = phone)
if old.exists():
old = old.first()
validated = old.validated
if validated:
temp_data = {
'username' : old.username,
'email' : old.email,
'phone' : old.phone,
'password' : old.password,
}
serializer = CreateUserSerializer(data = temp_data)
serializer.is_valid(raise_exception = True)
user = serializer.save()
user.set_password(old.password)
user.save()
print(user)
print(old.password)
old.delete()
return Response({
'status' : True,
'detail' : 'Account Created Successfully'
})
else:
return Response({
'status' : False,
'detail' : 'OTP havent Verified. First do that Step.'
})
else:
return Response({
'status' : False,
'detail' : 'Please verify Phone First'
})
else:
return Response({
'status' : False,
'detail' : 'Both username, email, phone, password are not sent'
})
<------------------------ views.py ------------------------>