-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·255 lines (235 loc) · 6.85 KB
/
app.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
from flask import Flask, jsonify, request
from pymongo import MongoClient
from config import db
from config import gcmKey
from config import secretKey
import time
from config import parseAppId,parseRestAPIKey
from gcm import GCM
import json,httplib
import imaplib
import hashlib
app = Flask(__name__)
IMAP_SERVER='webmail.nitt.edu'
def hashify(string):
hash_object = hashlib.sha1(string+secretKey+str(time.time()))
hex_dig = hash_object.hexdigest()
return hex_dig
def verifyKey(username,key):
usersinfo = db.users.find_one({
"rollnumber" : username,
"secret" : key
})
if userInfo == None:
return False
else:
return True
@app.route('/' ,methods=['GET'])
def index():
return "hello"
@app.route('/addCR',methods=['POST'])
def addCR():
data=request.json
username=data['username']
password=data['password']
a={
"username": username ,
"password": password
}
db.crlogin.insert(a)
return "Success"
@app.route('/crlogin', methods=['POST'])
def crlogin():
data=request.json
username=data['username']
password=data['password']
check=db.crlogin.find_one({
"username": username ,
"password": password
})
if check is None :
return jsonify(({"Signed Up" : 0}))
else:
secret = hashify(username)
ab={
"rollnumber" : username,
"secret" : secret
}
try:
db.usersInfo.remove({"rollnumber" :username})
db.usersInfo.insert(ab)
except:
print "****cant add*****"
return jsonify(({"Signed Up" : 1 , "secret" : secret}))
@app.route('/login', methods=['POST'])
def login():
if request.method == "POST":
data=request.json
username = data['username']
password = data['password']
try:
imap = imaplib.IMAP4(IMAP_SERVER)
login = imap.login(username, password)
success = login[0]
if(success=='OK'):
print 'ok\n\n'
return jsonify({'logged_in':1})
except:
print 'not ok\n\n\n'
return jsonify({'logged_in':0})
@app.route('/register', methods=['POST'])
def add_user():
if request.method == "POST":
data=request.json
rollno = data['rollnumber']
reg_id = data['regno']
print request.form
batch_det = rollno[:-3]
db.users.insert({
"rollnumber": rollno,
"registration_id": reg_id,
"batch": batch_det
})
return jsonify( ( { "Signed Up" : 1 } ) )
@app.route('/setTimeTable', methods=['POST'])
def setTimeTable():
if request.method == "POST" :
data = request.json
rno = data['username']
secret = data['secret']
chk = db.usersInfo.find({
"rollnumber" : rno
})
for i in chk:
if(i["secret"]!=secret):
return jsonify(({"Success" : 0 , "message" : "Nice Try :P"}))
break
batch = data["batch"]
b={
"batch": batch
}
check = db.fullTT.find_one(b)
a ={
"tt" : data["data"],
"batch" : batch
}
if check is None :
try:
db.fullTT.insert(a)
except:
return jsonify(({"Error" : 1}))
else:
try:
db.fullTT.remove(b)
db.fullTT.insert(a)
except:
return jsonify(({"Error" : 1}))
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/push', json.dumps({
"channels": [
'nlr'+data['batch']
],
"data": {"type": "tt"}
}), {
"X-Parse-Application-Id": parseAppId,
"X-Parse-REST-API-Key": parseRestAPIKey,
"Content-Type": "application/json"
})
result = json.loads(connection.getresponse().read())
print result
return jsonify(({"Success": 1}))
#getTimetable with batch
@app.route('/getTimetable/<batch>' , methods=['POST','GET'])
def getTimetable(batch):
data = db.fullTT.find({
"batch" : batch
})
if data is None :
return jsonify(({"Success" : 0}))
for i in data :
return json.dumps(i['tt'])
#parse sending code
@app.route('/updateTT', methods=['POST'])
def update_timetable():
if request.method == "POST":
data = request.json
rno = data['username']
secret = data['secret']
chk = db.usersInfo.find({
"rollnumber" : rno
})
for i in chk:
if(i["secret"]!=secret):
return jsonify(({"Success" : 0 , "message" : "Nice Try :P"}))
break
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/push', json.dumps({
"channels": [
'nlr'+data['batch']
],
"data": data
}), {
"X-Parse-Application-Id": parseAppId,
"X-Parse-REST-API-Key": parseRestAPIKey,
"Content-Type": "application/json"
})
result = json.loads(connection.getresponse().read())
print result
return "YOLO"+"\n"
# Route is for backing up attendance
@app.route('/backup', methods= ['POST'] )
def backup():
if request.method == "POST":
data_array = request.json
# rno = data['username']
# secret = data['secret']
# chk = db.usersInfo.find({
# "rollnumber" : rno
# })
# for i in chk:
# if(i["secret"]!=secret):
# return jsonify(({"Success" : 0 , "message" : "Nice Try :P"}))
# break
print data_array
for data in data_array:
attendance = db.attendance.find_one( {
"rollno" : data['rollno'],
"date-time" : data['date-time'],
"subject" : data['subject']
})
if attendance is None:
try:
db.attendance.insert(data)
except:
return jsonify( ( { "BackedUp": 0 } ) )
else:
try:
db.attendance.remove(attendance)
db.attendance.insert(data)
except:
return jsonify( ( { "BackedUp": 0 } ) )
return jsonify( ( { "BackedUp": 1 } ) )
# To get attendance of a particular user
@app.route('/attendance', methods = ['POST'])
def get_attendance():
if request.method == "POST":
rollno = request.json['rollno']
try:
attendance = db.attendance.find( { "rollno" : rollno } )
a = []
for i in attendance:
temp = {
"rollno" : i['rollno'],
"subject" : i['subject'],
"date-time" : i['date-time'],
"present" : i['present']
}
a.append(temp)
a = json.dumps(a)
return a
except:
return jsonify ( ( { "Error": 1 } ) )
if __name__ =="__main__":
app.run(debug=True)