-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
398 lines (335 loc) · 12.1 KB
/
app.py
File metadata and controls
398 lines (335 loc) · 12.1 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from flask import Flask, render_template, request, redirect, session, jsonify, flash
from flask_mail import Mail
from flask_socketio import SocketIO, join_room, leave_room
from werkzeug.security import generate_password_hash, check_password_hash
import json, re, string, random
from activation import *
from dbRequests import *
import config
app = Flask(__name__)
app.config.from_object('config')
mail = Mail(app)
listID = 0
##### Front End Routes #####
@app.route("/")
def index():
if hasattr(session, 'logged_in'):
if session['logged_in'] == True:
return redirect('/dashboard')
else:
return render_template('index.html')
else:
return render_template('index.html')
@app.route("/test")
def test():
return "test123"
@app.route("/dashboard")
def dashboard():
print(session)
if session['logged_in'] == True:
flash("Logged In Successfully! ")
return render_template('dashboard.html')
else:
return redirect('/')
@app.route("/TandC")
def terms():
return render_template('TandC.html')
@app.route("/Contact")
def conts():
return render_template('Contact.html')
@app.route("/Documentation")
def docs():
return render_template('documentation.html')
##### Web Service Routes #####
### DEMO WEB SERVICE ROUTE ###
@app.route('/request/submitTestForm', methods=['POST'])
def request_submitTestForm():
print(request.form)
return redirect('/dashboard')
### Authentication ###
# signup
@app.route("/auth/signup", methods=['POST'])
def auth_signup():
email = request.form['signupemail']
# displayname = request.form['displayname']
password = request.form['signuppassword']
# Validate email
regex = re.compile('(^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(case)\.edu$)')
regexResult = regex.match(email)
# Validate Password
pwregex = re.compile('(?=^.{8,}$)(?=.*\d)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$')
pwResult = pwregex.search(password)
if regexResult != None:
existingUserId = lookupUserIdFromEmail((email,))
if existingUserId == -1:
if pwResult != None:
addUser((email, generate_password_hash(password), email,))
activationToken = make_email_token(email)
sendActivationEmail(email, activationToken)
flash("Almost done! Check your email to activate your account.")
# return 'Almost done! Check your email to activate your account.'
else:
flash("Password did not meet strength requirements.")
# return "Password did not meet strength requirements."
else:
flash("That email is already taken!")
# return "That email is already taken!"
else:
flash("Invalid email address. Please use a valid @case.edu address.")
# return "Invalid email address. Please use a valid @case.edu address."
return render_template('index.html')
# activate
@app.route("/auth/activate/<token>", methods=['GET'])
def auth_activate(token):
plaintextEmail = confirm_email_token(token)
if plaintextEmail == False:
flash("Invalid email activation token.")
# return "Invalid email activation token."
else:
didConfirm = confirmUser((plaintextEmail,))
if didConfirm == True:
flash("Account successfully activated! Go log in.")
# return "Account successfully activated! Go log in."
else:
flash("Unable to confirm user. Maybe your account is already activated, or your activation token expired.")
# return "Unable to confirm user. Maybe your account is already activated, or your activation token expired."
return render_template('index.html')
# forgotPassword
@app.route("/auth/forgotpassword", methods=['POST'])
def auth_forgotpassword():
email = request.form['email']
userid = getUserIdFromEmail((email,))
if userid is None:
flash("There is no account associated with that email address!")
# return "There is no account associated with that email address!"
else:
newpassword = ""
while len(newpassword) < 8:
newpassword = newpassword + random.choice(string.ascii_letters + string.digits)
changeUserPassword((generate_password_hash(newpassword), email,))
flash("Your password was reset! Check your email for your new password.")
return render_template('index.html')
# return "Your password was reset! Check your email for your new password."
# Login
@app.route('/auth/login', methods=['POST'])
def login():
# print(request.form)
# userId = validateUserData((request.form['loginemail'], request.form['loginpassword'],))
hashedPassword = getHashedPassword((request.form['loginemail'],))
print(hashedPassword)
print(request.form['loginemail'])
if hashedPassword is None:
# flash("There is no account associated with that email address.")
return "There is no account associated with that email address."
matches = check_password_hash(hashedPassword, request.form['loginpassword'])
if matches:
session['user_name'] = request.form['loginemail']
session['pwd'] = hashedPassword
session['logged_in'] = True
if request.form['loginemail'].lower() == 'qtadmin@case.edu':
session['admin'] = True
else:
session['user_name'] = -1
session['admin'] = False
session['logged_in'] = False
print(session)
flash("Login information was not valid.")
return render_template('index.html')
# return "Login information was not valid."
return render_template('dashboard.html')
@app.route('/auth/logout', methods=['POST'])
def logout():
# del socketIODict[request.cookies.get('session')]
session.pop('user_name', None)
session['logged_in'] = False
session['admin'] = False
return redirect('/')
@app.route('/request/getUserInfo', methods=['GET'])
def request_getUserInfo():
if request.method == 'GET':
sampleJsonData = '[{"userid": 1, "email": "test@case.edu", "password": "password", "displayname": "TestUser"}]'
return sampleJsonData
### Main Activity Page / Listings ###
# getlistings
@app.route("/listings/getall", methods=['GET'])
def listings_getall():
listings = getAllListings()
ret = []
for item in listings:
new = {}
new['listingid'] = item[0]
new['userid'] = item[1]
new['classid'] = item[2]
new['classname'] = getClassStringName((item[2],))
new['shortDescription'] = item[3]
new['topic'] = item[4]
new['location'] = item[5]
new['timestamp'] = item[6]
ret.append(new)
return json.dumps(ret)
# createlistings
@app.route("/listings/create", methods=['POST'])
def listings_create():
className = request.form['newRequestClass']
topic = request.form['newRequestTopic']
location = request.form['newRequestLocation']
description = request.form['newRequestDescription']
classId = getClassIdFromName(className)
addListing((session['user_name'], classId, description, topic, location,))
message = {}
message['className'] = className
message['topic'] = topic
message['location'] = location
message['description'] = description
message['userid'] = getUserIdFromEmail((session['user_name'],))
socketio.emit('new listing', message, broadcast=True)
flash("New request made successfully!")
return render_template('dashboard.html') # i got an error when flashing w/o a return and this return is no content. actually might be because i'm not using a flask form when I make the request, i'm making a JQuery request
### Profile ###
# get a profile
@app.route("/profile/get", methods=['GET'])
def profile_get():
if request.method == 'GET':
if 'user_name' in session:
ret = {}
ret['email'] = session['user_name']
ret['screenname'] = getUsernameFromUserEmail((session['user_name'],))
userClassIds = getClassesForUser((session['user_name'],))
if userClassIds is None:
ret['classes'] = []
else:
ret['classes'] = []
for classid in userClassIds:
newclass = {}
newclass['classid'] = classid[0]
newclass['stringName'] = getClassStringName(classid)
ret['classes'].append(newclass)
return json.dumps(ret)
else:
flash("User did not exist")
return "User did not exist"
else:
flash("Invalid protocol for this route, use GET")
# return "Invalid protocol for this route, use GET"
return render_template('dashboard.html')
# ChangeScreenName
@app.route("/profile/changescreenname", methods=['POST'])
def profile_changescreenname():
newName = request.form['newName']
changeUserScreenname((newName, session['user_name']))
flash("Changed screennname!")
return render_template('dashboard.html')
# return "Changed screennname!"
# AddClass
@app.route("/profile/addclass", methods=['POST'])
def profile_addclass():
classDept = request.form['classDept']
classNum = request.form['classNum']
addRuserClass((session['user_name'], classDept, classNum,))
flash("Added your class!")
return render_template('dashboard.html')
# return "Added your class!"
# RemoveClass
@app.route("/profile/removeclass", methods=['POST'])
def profile_removeclass():
classDept = request.form['classDept']
classNum = request.form['classNum']
deleteRuserClass((session['user_name'], classDept, classNum,))
flash("Removed your class!")
return render_template('dashboard.html')
# return "Removed your class!"
@app.route("/profile/edit", methods=['POST'])
def profile_edit():
addclasses = request.form['addclasses']
deleteclasses = request.form['deleteclasses']
screenname = request.form['screenname']
for c in addclasses:
addRuserclass((session['user_name'], c.split(' ')[0], c.split(' '[1])))
for c in deleteclasses:
deleteRuserClass((session['user_name'], c.split(' ')[0], c.split(' ')[0]))
flash("Updated your profile!")
return render_template('dashboard.html')
@app.route("/listings/respond", methods=['POST'])
def listings_respond():
email = session['user_name']
listingId = request.form['listingId']
requestingUserId = getUserIdFromListingId((listingId,))
socketio.emit('tutor accepted', {'recipientUserId': requestingUserId})
return ('', 204)
# SocketIO
socketio = SocketIO(app)
listOfUsers = {}
socketIODict = {}
@socketio.on('initConnection')
def init_connection(json):
# sessionID = request.cookies.get('session')
socketID = request.sid
userID = getUserIdFromEmail((session['user_name'],))[0]
print("INITING USER ID:", userID, session['user_name'])
socketIODict[userID] = socketID;
@socketio.on('chat msg send')
def chat_msg_send(json):
# sessionID = request.cookies.get('session')
print("ROOM:", json['sioRoom'])
sendMessageToRecipient(json['sioRoom'], json['message'])
def sendMessageToRecipient(recipientUserId, message):
#connectedUserKeys = socketIODict.keys()
#for key in connectedUserKeys:
# if sessionID != key:
# recvSessionID = key
socketio.emit('chat msg recv', {'data': message}, room=recipientUserId)
@socketio.on('listing new')
def listing_new(json):
print(json)
userid = getUserIdFromEmail((session['user_name'],))
print(userid)
json['uid'] = userid[0]
global listID
json['lid'] = listID
socketio.emit('listing broadcast', json, broadcast=True)
listID += 1
@socketio.on('tutor accepted')
def tutor_accepted(json):
#print('socketio on tutor accepted was executed')
tutorid = getUserIdFromEmail((session['user_name'],))[0]
userid = json['uid']
print("USERID:", json['uid'], " TUTORID:", tutorid)
sioTRoom = socketIODict[int(tutorid)]
sioURoom = socketIODict[int(userid)]
socketio.emit('chat msg init', {'roomid': sioTRoom}, room=sioURoom)
socketio.emit('chat msg init', {'roomid': sioURoom}, room=sioTRoom)
# DELETE LISTING USING GENERAL BROADCAST
# METHOD FOR TUTOR CHATBOX HANDSHAKE
# After user accepts tutor request, this code runs
'''
Chatbox is opened up on user with send location = tutor sessionID
Chatbox is opened upon tutor with send location = user sessionID
Messages are sent using same method, with destination = send location
'''
@app.route("/chatbox")
def chatbox():
return render_template('chatbox.html')
@socketio.on('set client')
def set_client(json):
print(str(json))
server_response(json['clientID'], request.sid)
return 'one', 2
@socketio.on('my event')
def handle_custom_event(json):
print('Received JSON: ' + str(json))
#server_response(json['clientID'], request.sid)
return 'one', 2
@socketio.on('to user message')
def send_user_msg(json):
print(str(json))
socketio.emit('user message', {'data': json['msg']}, room=listOfUsers[json['uid']])
def server_response(uid, sid):
if int(uid) > 0:
listOfUsers[uid] = sid
print(listOfUsers)
socketio.emit('server_response', {'data': uid}, room=sid)
if __name__ == "__main__":
app.secret_key = "3sAmVAtdh!GNTSKuZJJn4^5wve"
addDefaultAccounts()
socketio.run(app, host='0.0.0.0')
# http://flask.pocoo.org/docs/0.12/quickstart/ #