forked from kmehant/cts_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
301 lines (263 loc) · 12.8 KB
/
Main.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
from flask import request, jsonify, Response
import json
from flask_mail import Message
from utils import otp, present_date, executeSQL, rvalidate, svalidate, tvalidate
from initializer import cts, mail, cache
import re
@cts.route('/')
def check():
data = executeSQL('show tables',False,)
return Response(response=json.dumps(data),status=200)
@cts.route('/login/t')
def tlogin():
email = request.args.get('email')
onepass = request.args.get('pin')
try:
pos = email.index('@')
except ValueError as e:
return Response(response='Invalid', status=401)
pass
domain = email[pos:]
print(domain)
if domain != "@nitandhra.ac.in":
return Response(response='Failed', status=401)
sd = executeSQL('select tid from teachers where temail="%s"', True, email)
if sd is None:
executeSQL('insert into teachers (temail) values("%s")', True, email)
if onepass is not None:
p = executeSQL('select tid from teachers where temail="%s" and tpin="%s"', True, email, onepass)
if p is not None:
return Response(response='Success', status=200)
return Response(response='Failed', status=401)
else:
key = otp()
with cts.app_context():
msg = Message(subject="Single use pin for NIT Andhra Pradesh CTS login",
sender=cts.config.get("MAIL_USERNAME"),
recipients=[email],
body='Single use pin: "%s" \n \n \n This is an auto generated mail. \n Please do not reply to this message or on this email address. \n For any query, please contact at [email protected] \n Do not disclose any confidential information to anyone.' % key)
mail.send(msg)
executeSQL('update teachers set tpin="%s" where temail="%s"', True, key, email)
return Response(response='Success', status=200)
@cts.route('/login/s')
def slogin():
email = request.args.get('email')
onepass = request.args.get('pin')
try:
pos = email.index('@')
except ValueError as e:
return Response(response='Invalid', status=401)
pass
domain = email[pos:]
if domain != "@student.nitandhra.ac.in":
return Response(response='Failed', status=401)
sd = executeSQL('select sid from students where Semail="%s"', True, email)
print(sd)
if sd is None:
executeSQL('insert into students (Semail) values("%s")', True, email)
if onepass is not None:
p = executeSQL('select sid from students where Semail="%s" and spin="%s"', True, email, onepass)
if p is not None:
return Response(response='Success', status=200)
return Response(response='Failed', status=401)
else:
key = otp()
with cts.app_context():
msg = Message(subject="Single use pin for NIT Andhra Pradesh CTS login",
sender=cts.config.get("MAIL_USERNAME"),
recipients=[email],
body='Single use pin: "%s" \n \n \n This is an auto generated mail. \n Please do not reply to this message or on this email address. \n For any query, please contact at [email protected] \n Do not disclose any confidential information to anyone.' % key)
mail.send(msg)
executeSQL('update students set spin="%s" where Semail="%s"', True, key, email)
return Response(response='Success', status=200)
@cts.route('/signup')
def signup():
email = request.args.get('email')
onepass = request.args.get('pin')
try:
pos = email.index('@')
except ValueError as e:
return Response(response='Invalid', status=401)
pass
domain = email[pos:]
if domain != "@student.nitandhra.ac.in" or domain != "@nitandhra.ac.in":
return Response(response='Failed', status=401)
if domain == "@student.nitandhra.ac.in":
i = executeSQL('select sid from students where Semail="%s"', True, email)
else:
i = executeSQL('select tid from teachers where temail="%s"', True, email)
if domain == "@student.nitandhra.ac.in" and i is not None:
executeSQL('insert into students (Semail) values("%s")', True, email)
elif domain == "@nitandhra.ac.in" and i is not None:
executeSQL('insert into teachers (temail) values("%s")', True, email)
if onepass != "":
if domain == "@student.nitandhra.ac.in":
p = executeSQL('select spin from students where Semail="%s"', True, email)
else:
p = executeSQL('select tpin from teachers where temail="%s"', True, email)
if p[0] == onepass:
return Response(response='Success', status=200)
return Response(response='Failed', status=401)
else:
key = otp()
with cts.app_context():
msg = Message(subject="Single use pin for NIT Andhra Pradesh CTS login",
sender=cts.config.get("MAIL_USERNAME"),
recipients=[email],
body='Single use pin: "%s" \n \n \n This is an auto generated mail. \n Please do not reply to this message or on this email address. \n For any query, please contact at [email protected] \n Do not disclose any confidential information to anyone.' % key)
mail.send(msg)
if domain == "@student.nitandhra.ac.in":
executeSQL('update students set spin="%s" where Semail="%s"', True, key, email)
else:
executeSQL('update teachers set tpin="%s" where temail="%s"', True, key, email)
return Response(response='Success', status=200)
@cts.route('/login/r')
def rlogin():
email = request.args.get('email')
onepass = request.args.get('pin')
if onepass is not None:
p = executeSQL('select rid from resolvers where remail="%s" and rpin="%s"', True, email, onepass)
if p is not None:
return Response(response='Success', status=200)
return Response(response='Failed', status=401)
else:
key = otp()
with cts.app_context():
msg = Message(subject="Single use resolver's pin for NIT Andhra Pradesh CTS login",
sender=cts.config.get("MAIL_USERNAME"),
recipients=[email],
body='Single use pin: "%s" \n \n \n This is an auto generated mail. \n Please do not reply to this message or on this email address. \n For any query, please contact at [email protected] \n Do not disclose any confidential information to anyone.' % key)
mail.send(msg)
executeSQL('update resolvers set rpin="%s" where remail="%s"', True, key, email)
return Response(response='Success', status=200)
@cts.route('/tfiles/<token>')
def tfiles(token):
vdata = tvalidate(token)
data = request.headers['data']
tags = request.headers['tags']
if vdata is not None:
cid = executeSQL('select cid from complaints where cdata="%s" and tags="%s"', True, data, tags)
if cid is None:
executeSQL('insert into complaints(cdata,tags) values ("%s","%s")', True, data, tags)
cid = executeSQL('select cid from complaints where cdata="%s" and tags="%s"', True, data, tags)
time_now = present_date()
executeSQL('insert into tfiles(tid,cid,ftime) values (%d,%d, "%s")', True, vdata[0], cid[0], time_now)
return Response(response='Success', status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/sfiles/<token>')
def sfiles(token):
vdata = svalidate(token)
data = request.headers['data']
tags = request.headers['tags']
if vdata is not None:
cid = executeSQL('select cid from complaints where cdata="%s" and tags="%s"', True, data, tags)
if cid is None:
executeSQL('insert into complaints(cdata,tags) values ("%s","%s")', True, data, tags)
cid = executeSQL('select cid from complaints where cdata="%s" and tags="%s"', True, data, tags)
time_now = present_date()
print(present_date())
print(vdata[0])
print(cid[0])
executeSQL('insert into sfiles(sid,cid,ftime) values (%s,%s, "%s")', True, vdata[0], cid[0], time_now)
return Response(response='Success', status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/myscomplaints/<token>')
@cache.cached(timeout=15)
def myscomplaints(token):
vdata = svalidate(token)
if vdata is not None:
data = executeSQL('select * from students,complaints, sfiles where students.sid=sfiles.sid and sfiles.cid=complaints.cid and students.sid = %d and sfiles.cid not in (select cid from resolves)', False, vdata[0])
print(data)
return Response(response=json.dumps(data, indent=4, sort_keys=True, default=str), status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/mytcomplaints/<token>')
@cache.cached(timeout=15)
def mytcomplaints(token):
vdata = tvalidate(token)
if vdata is not None:
data = executeSQL('select * from teachers,complaints, tfiles where teachers.tid=tfiles.tid and tfiles.cid=complaints.cid and teachers.tid = %d and tfiles.cid not in (select cid from resolves)', False, vdata[0])
return Response(response=json.dumps(data, indent=4, sort_keys=True, default=str), status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/mytcomplaints/r/<token>')
@cache.cached(timeout=15)
def mytcomplaintsr(token):
vdata = tvalidate(token)
if vdata is not None:
data = executeSQL('select * from teachers,complaints, tfiles, resolves, resolvers where teachers.tid=tfiles.tid and tfiles.cid=complaints.cid and resolves.cid = complaints.cid and resolves.rid = resolvers.rid and teachers.tid = %d', False, vdata[0])
return Response(response=json.dumps(data, indent=4, sort_keys=True, default=str), status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/myscomplaints/r/<token>')
@cache.cached(timeout=15)
def myscomplaintsr(token):
vdata = svalidate(token)
if vdata is not None:
data = executeSQL('select * from students,complaints, sfiles, resolves, resolvers where students.sid=sfiles.sid and sfiles.cid=complaints.cid and resolves.cid = complaints.cid and resolves.rid = resolvers.rid and students.sid = %d', False, vdata[0])
return Response(response=json.dumps(data, indent=4, sort_keys=True, default=str), status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/scomplaints/<token>')
def scomplaints(token):
vdata = rvalidate(token)
if vdata is not None:
data = executeSQL('select * from students,complaints, sfiles where students.sid=sfiles.sid and sfiles.cid=complaints.cid and sfiles.cid not in (select cid from resolves)', False)
return Response(response=json.dumps(data, indent=4, sort_keys=True, default=str), status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/tcomplaints/<token>')
def tcomplaints(token):
vdata = rvalidate(token)
if vdata is not None:
data = executeSQL('select * from teachers,complaints, tfiles where teachers.tid=tfiles.tid and tfiles.cid=complaints.cid and tfiles.cid not in (select cid from resolves)', False)
return Response(response=json.dumps(data, indent=4, sort_keys=True, default=str), status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/complaints/u/<cid>/<token>')
def complaints(cid, token):
vdata = rvalidate(token)
exp = request.headers['exp']
is_resolved = request.headers['is_resolved'] # 0/1
is_valid = request.headers['is_valid'] # 0/1
if vdata is not None:
executeSQL('insert into resolves values(%d, %s, %s, %s,"%s")', False, vdata[0], cid,is_valid, is_resolved, exp)
return Response(response=json.dumps("success", indent=4, sort_keys=True, default=str), status=200)
else:
return Response(response='Failed', status=401)
@cts.route('/search/<cardinal>')
def search(cardinal):
cardinal = int(cardinal)
data = str(request.headers['data'])
search_term = request.headers['search_term']
#search_term+="*"
print(data)
ans = []
ans2 = []
addIt = False
tempArray = []
count = 0
data = data.split(',')
for a in data:
if count == cardinal:
count = 0
ans.append(tempArray)
tempArray = []
count = count + 1
tempArray.append(a)
ans.append(tempArray)
print(ans)
for i in ans:
addIT = False
for j in i:
if re.search(search_term, j):
addIt = True
break
if addIt is True:
ans2.append(i)
print(ans2)
return Response(response=json.dumps(ans2, indent=4, sort_keys=True, default=str), status=200)
if __name__ == '__main__':
cts.run(threaded=True)