-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
614 lines (539 loc) · 22 KB
/
server.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/usr/bin/env python
import json
from flask import Flask, render_template, redirect, url_for, request, send_from_directory, session
from flask_mysqldb import MySQL
import hashlib
from base64 import b64encode
from flask import jsonify
from flask_socketio import SocketIO, send, emit
from datetime import datetime
SESSION_TYPE = 'memcache'
app = Flask(__name__)
app.secret_key = 'ourSpace'
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'toor'
app.config['MYSQL_DB'] = 'myspace'
mysql = MySQL(app)
# create table login_credentials(
# email varchar(50) primary key not null,
# password varchar(32) not null
# );
# create table user_profile(
# email varchar(50) primary key not null references login_credentails(email),
# username varchar(50) not null,
# gender varchar(10) not null,
# dob varchar(10) not null,
# photo mediumblob
# );
# create table friend_list(
# email1 varchar(50) not null references login_credentials(email),
# email2 varchar(50) not null references login_credentials(email),
# constraint pk_constraint primary key(email1, email2)
# );
# create table friend_request(
# email1 varchar(50) not null references login_credentials(email),
# email2 varchar(50) not null references login_credentials(email),
# constraint pk_constraint primary key(email1, email2)
# );
# create table post(
# email varchar(50) not null references login_credentials(email),
# post_id int not null,
# image mediumblob,
# text varchar(1000) not null, #### either image or text has to be not null, or just make text as not null?
# timestamp bigint not null,
# likes int not null,
# constraint pk_constraint primary key(email, post_id)
# );
# create table likes(
# poster_email varchar(50) not null references login_credentials(email),
# post_id int not null references post(post_id),
# liker_email varchar(50) not null references login_credentials(email),
# constraint pk_constraint primary key(poster_email, post_id, liker_email)
# );
# create table chat(
# email1 varchar(50) not null references login_credentials(email),
# email2 varchar(50) not null references login_credentials(email),
# message varchar(2000) not null, #### need to handle empty messages
# timestamp bigint not null
# );
# create table admin_credentials(
# email varchar(50) primary key not null,
# password varchar(32) not null
# );
# create table phone_numbers(
# email varchar(50),
# phone_no varchar(12),
# constraint pk_constraint primary key(email, phone_no)
# );
# create table schools(
# email varchar(50),
# school varchar(30),
# constraint pk_constraint primary key(email, school)
# );
#
# delimiter $$
# create procedure get_unix_timestamp()
# begin
# select unix_timestamp();
# end $$
# delimiter ;
socketio = SocketIO(app)
users = {}
@app.route('/')
def identify():
return render_template('identify.html')
@app.route('/check_user', methods=['GET', 'POST'])
def check_user():
email = request.form['email']
cur = mysql.connection.cursor()
cur.execute('select * from login_credentials where email=%s', [email])
result = cur.fetchall()
cur.close()
if len(result) != 0:
return render_template('login.html', email=email)
return render_template('signup.html', email=email)
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/register_user', methods=['GET', 'POST'])
def register_user():
if request.method == 'POST':
school = []
school_ctr = 0
while True:
if 'school' + str(school_ctr) in request.form:
school.append(request.form['school' + str(school_ctr)])
school_ctr = school_ctr + 1
else:
break
phone = []
phone_ctr = 0
while True:
if 'phone_no' + str(phone_ctr) in request.form:
phone.append(request.form['phone_no' + str(phone_ctr)])
phone_ctr = phone_ctr + 1
else:
break
email = request.form['email']
password = request.form['pass']
passwordhash = hashlib.md5(password.encode()).hexdigest()
username = request.form['username']
uploadedImage = request.files['profilePhoto']
gender = request.form['gender']
dob = request.form['dob']
imageBytes = uploadedImage.read()
cur = mysql.connection.cursor()
try:
cur.execute('insert into login_credentials values(%s, %s)',
[email, passwordhash])
mysql.connection.commit()
except:
print("in except")
return '<script>alert("Email already exists");window.location=\'/signup\'</script>'
if imageBytes != '': # needs to be checked
image = b64encode(imageBytes).decode('utf-8')
cur.execute('insert into user_profile values(%s, %s, %s, %s, %s)', [
email, username, gender, dob, image]) # obj or image?
mysql.connection.commit()
for item in school:
cur.execute('insert into schools values(%s, %s)', [email, item])
mysql.connection.commit()
for item in phone:
cur.execute('insert into phone_numbers values(%s, %s)',
[email, item])
mysql.connection.commit()
cur.close()
return redirect(url_for('login'))
@app.route('/login', methods=['GET', 'POST'])
def login():
return render_template('login1.html')
@app.route('/verify_user', methods=['GET', 'POST'])
def verify_user():
if request.method == 'POST':
email = request.form['email']
password = request.form['pass']
passwordhash = hashlib.md5(password.encode()).hexdigest()
cur = mysql.connection.cursor()
cur.execute('select * from login_credentials where email=%s', [email])
result = cur.fetchall() # fetch all entries which satisfied the query
cur.close()
dbpassword = result[0][1]
if passwordhash == dbpassword:
session['email'] = email
return redirect(url_for('news_feed'))
else:
return render_template('login.html', email=email)
return
@app.route('/myProfile')
def myProfile():
email = session['email']
cur = mysql.connection.cursor()
cur.execute(
'select photo, username, gender, dob from user_profile where email=%s', [email])
result = cur.fetchall()
posts = []
cur.execute(
'select username, image, text, likes, email, post_id,timestamp from post natural join user_profile where email = %s order by timestamp', [email])
results = cur.fetchall()
for post in results:
temp_post = {}
temp_post['username'] = post[0]
temp_post['image'] = post[1].decode('utf-8')
temp_post['text'] = post[2]
temp_post['likes'] = post[3]
temp_post['email'] = post[4]
temp_post['post_id'] = post[5]
temp_post['timestamp'] = datetime.fromtimestamp(
post[6]).strftime('%d-%m-%Y %H:%M')
posts.append(temp_post)
phone = []
cur.execute('select phone_no from phone_numbers where email=%s', [email])
results = cur.fetchall()
print(results)
for item in results:
phone.append(item[0])
school = []
cur.execute('select school from schools where email=%s', [email])
results = cur.fetchall()
for item in results:
school.append(item[0])
cur.close()
image = result[0][0].decode("utf-8")
return render_template('myProfile.html', posts=posts, email=email, image=image, username=result[0][1], phone=phone, school=school, gender=result[0][2], dob=result[0][3])
@app.route('/logout')
def logout():
session['email'] = None
return redirect(url_for('identify'))
@app.route('/edit_profile', methods=['GET', 'POST'])
def edit_profile():
email = session['email']
cur = mysql.connection.cursor()
if request.method == 'POST':
username = request.form['Username']
uploadedImage = request.files['profilePhoto']
print(uploadedImage)
imageBytes = uploadedImage.read()
cur.execute('update user_profile set username=%s where email=%s', [
username, email])
mysql.connection.commit()
image = b64encode(imageBytes).decode('utf-8')
if image != '':
cur.execute(
'update user_profile set photo=%s where email=%s', [image, email])
mysql.connection.commit()
cur.execute(
'select photo, username from user_profile where email=%s', [email])
result = cur.fetchall()
cur.close()
image = result[0][0].decode("utf-8")
print("updated")
return redirect(url_for('myProfile'))
@app.route('/profile/<email>')
def profile(email):
myEmail = session['email']
print(email, myEmail)
cur = mysql.connection.cursor()
cur.execute(
'select photo, username, gender, dob from user_profile where email=%s', [email])
result = cur.fetchall()
cur.execute(
'select * from friend_request where email1=%s and email2=%s', [email, myEmail])
request1Status = cur.fetchall()
cur.execute(
'select * from friend_request where email1=%s and email2=%s', [myEmail, email])
request2Status = cur.fetchall()
cur.execute(
'select * from friend_list where email1=%s and email2=%s', [email, myEmail])
friendStatus = cur.fetchall()
image = result[0][0].decode("utf-8")
print(request1Status, request2Status, friendStatus)
phone = []
cur.execute('select phone_no from phone_numbers where email=%s', [email])
results = cur.fetchall()
print(results)
for item in results:
phone.append(item[0])
school = []
cur.execute('select school from schools where email=%s', [email])
results = cur.fetchall()
for item in results:
school.append(item[0])
posts = []
cur.execute(
'select username, image, text, likes, email, post_id,timestamp from post natural join user_profile where email = %s order by timestamp', [email])
results = cur.fetchall()
for post in results:
temp_post = {}
temp_post['username'] = post[0]
temp_post['image'] = post[1].decode('utf-8')
temp_post['text'] = post[2]
temp_post['likes'] = post[3]
temp_post['email'] = post[4]
temp_post['post_id'] = post[5]
print(post[6])
temp_post['timestamp'] = datetime.fromtimestamp(
post[6]).strftime('%d-%m-%Y %H:%M')
posts.append(temp_post)
cur.close()
return render_template('profile.html', myEmail=myEmail, email=email, image=image, username=result[0][1],
request1Status=len(request1Status), request2Status=len(request2Status), friendStatus=len(friendStatus),
phone=phone, school=school, posts=posts, gender=result[0][2], dob=result[0][3])
@app.route('/send_friend_request', methods=['GET', 'POST'])
def send_friend_request():
email1 = session['email']
print(email1)
email2 = request.form['email']
friendStatus = request.form['friendStatus']
request1Status = request.form['request1Status']
request2Status = request.form['request2Status']
print(email1, email2, friendStatus, request1Status, request2Status)
print(type(int(friendStatus)))
cur = mysql.connection.cursor()
if int(request1Status) == 0 and int(friendStatus) == 0 and int(request2Status) == 0:
cur.execute('insert into friend_request values(%s, %s)',
[email1, email2])
mysql.connection.commit()
elif int(request1Status) == 1:
cur.execute('delete from friend_request where email1=%s and email2=%s', [
email2, email1])
mysql.connection.commit()
cur.execute('insert into friend_list values(%s, %s)', [email1, email2])
mysql.connection.commit()
cur.execute('insert into friend_list values(%s, %s)', [email2, email1])
mysql.connection.commit()
elif int(request2Status) == 1:
cur.execute('delete from friend_request where email1=%s and email2=%s', [
email1, email2])
mysql.connection.commit()
cur.execute('insert into friend_list values(%s, %s)', [email1, email2])
mysql.connection.commit()
cur.execute('insert into friend_list values(%s, %s)', [email2, email1])
mysql.connection.commit()
cur.close()
return redirect(url_for('profile', email=email2))
@app.route('/news_feed')
def news_feed():
myEmail = session['email']
print(myEmail)
posts = []
cur = mysql.connection.cursor()
cur.execute(
'select username, image, text, likes, email, post_id,timestamp from post natural join user_profile where email in (select email2 from friend_list where email1=%s) order by timestamp', [myEmail])
results = cur.fetchall()
for post in results:
temp_post = {}
temp_post['username'] = post[0]
temp_post['image'] = post[1].decode('utf-8')
temp_post['text'] = post[2]
temp_post['likes'] = post[3]
temp_post['email'] = post[4]
temp_post['post_id'] = post[5]
temp_post['timestamp'] = datetime.fromtimestamp(
post[6]).strftime('%d-%m-%Y %H:%M')
posts.append(temp_post)
return render_template('news_feed.html', myEmail=myEmail, posts=posts)
@app.route('/update_likes', methods=['POST'])
def update_likes():
myEmail = session['email']
poster_email = request.form['poster_email']
post_id = request.form['post_id']
likes = request.form['likes']
cur = mysql.connection.cursor()
cur.execute('select * from likes where poster_email=%s and post_id=%s and liker_email=%s',
[poster_email, int(post_id), myEmail])
results = cur.fetchall()
print('results:', results, len(results))
if len(results) == 0:
cur.execute('update post set likes=%s where email=%s and post_id=%s', [
int(likes)+1, poster_email, post_id])
mysql.connection.commit()
cur.execute('insert into likes values(%s, %s, %s)',
[poster_email, post_id, myEmail])
mysql.connection.commit()
else:
cur.execute('update post set likes=%s where email=%s and post_id=%s', [
int(likes)-1, poster_email, post_id])
mysql.connection.commit()
cur.execute('delete from likes where poster_email=%s and post_id=%s and liker_email=%s', [
poster_email, post_id, myEmail])
mysql.connection.commit()
cur.close()
return redirect(url_for('news_feed'))
@app.route('/upload_post', methods=['POST', 'GET'])
def upload_post():
cur = mysql.connection.cursor()
print(request.form)
myEmail = session['email']
cur.execute('select max(post_id) from post where email=%s', [myEmail])
maxPostId = cur.fetchall()
newPostId = 0
if maxPostId[0][0] != None:
newPostId = maxPostId[0][0] + 1
image = request.files['image']
imageBytes = image.read()
text = request.form['text']
cur.execute('call get_unix_timestamp()')
current_timestamp = cur.fetchall()
likes = 0
if imageBytes != '':
image64 = b64encode(imageBytes).decode('utf-8')
cur.execute('insert into post values(%s, %s, %s, %s, %s, %s)', [
myEmail, newPostId, image64, text, current_timestamp, likes])
mysql.connection.commit()
else:
cur.execute('insert into post values(%s, %d, %s, %s, %d, %d)', [
myEmail, newPostId, None, text, current_timestamp, likes])
mysql.connection.commit()
cur.close()
return redirect(url_for('news_feed'))
@app.route('/search_profile', methods=['GET', 'POST'])
def search_profile():
cur = mysql.connection.cursor()
cur.execute('select email,username from user_profile')
results = cur.fetchall()
cur.close()
return jsonify(results)
@app.route('/search_results', methods=['GET', 'POST'])
def search_results():
myEmail = session['email']
print(myEmail)
username = request.form['username']
cur = mysql.connection.cursor()
cur.execute('select email,username,photo from user_profile where username like concat(\"%%\",%s,\"%%\")', [username])
results = cur.fetchall()
cur.close()
user_list = []
for user in results:
profile = {}
profile['email'] = user[0]
profile['username'] = user[1]
profile['photo'] = user[2].decode("utf-8")
user_list.append(profile)
return render_template('display_profiles.html', myEmail=myEmail, profiles=user_list)
@app.route('/chat/<email>', methods=['POST'])
def chat(email):
myEmail = session['email']
print(myEmail + ' - ' + email)
cur = mysql.connection.cursor()
cur.execute(
'select username, photo from user_profile where email=%s', [email])
res = cur.fetchall()
username = res[0][0]
photo = res[0][1].decode('utf-8')
cur.execute(
'select username, photo from user_profile where email=%s', [myEmail])
res1 = cur.fetchall()
myPhoto = res1[0][1].decode('utf-8')
message_sent = request.form['message_to_send']
print('message:' + message_sent + ";")
cur.execute('call get_unix_timestamp()')
current_timestamp = cur.fetchall()
print(current_timestamp)
if message_sent != '':
cur.execute('insert into chat values(%s, %s, %s, %s)', [
myEmail, email, message_sent, current_timestamp[0][0]])
mysql.connection.commit()
cur.close()
cur.execute(
'select email2, username, photo from friend_list inner join user_profile on email2=email where email1=%s', [myEmail])
res = cur.fetchall()
friends = []
for item in res:
temp_friend = {}
temp_friend['email'] = item[0]
temp_friend['username'] = item[1]
temp_friend['photo'] = item[2].decode('utf-8')
friends.append(temp_friend)
# fetching messages for chat
messages = []
if email == myEmail:
pass
else:
cur.execute('select * from chat where (email1=%s and email2=%s) or (email1=%s and email2=%s) order by timestamp',
[myEmail, email, email, myEmail])
res = cur.fetchall()
for item in res:
temp_message = {}
temp_message['email1'] = item[0]
temp_message['email2'] = item[1]
temp_message['text'] = item[2]
temp_message['timestamp'] = datetime.fromtimestamp(
item[3]).strftime('%Y-%m-%d %H:%M:%S')
messages.append(temp_message)
print(friends)
return render_template('chat.html', myPhoto=myPhoto, myEmail=myEmail, friends=friends, messages=messages, email=email, username=username, photo=photo)
@socketio.on('email', namespace='/private')
def receive_username(email):
users[email] = request.sid
print('email added!')
@socketio.on('private_message', namespace='/private')
def private_message(payload):
recipient_session_id = users[payload['email']]
message = payload['message']
print(recipient_session_id, message)
cur = mysql.connection.cursor()
myEmail = session['email']
cur.execute('call get_unix_timestamp()')
current_timestamp = cur.fetchall()
cur.execute('insert into chat values(%s,%s,%s,%s)', [
myEmail, payload['email'], message, current_timestamp[0][0]])
mysql.connection.commit()
cur.close()
emit('new_private_message', message,
room=recipient_session_id, include_self=True)
@app.route('/admin_login')
def admin_login():
return render_template('admin_login.html')
@app.route('/verify_admin', methods=['GET', 'POST'])
def verify_admin():
if request.method == 'POST':
email = request.form['email']
password = request.form['pass']
passwordhash = hashlib.md5(password.encode()).hexdigest()
cur = mysql.connection.cursor()
cur.execute('select * from admin_credentials where email=%s', [email])
result = cur.fetchall() # fetch all entries which satisfied the query
cur.close()
session['email'] = email
return redirect(url_for('admin_feed'))
return
@app.route('/admin_feed')
def admin_feed():
myEmail = session['email']
print(myEmail)
posts = []
cur = mysql.connection.cursor()
cur.execute(
'select username, image, text, likes, email, post_id, timestamp from post natural join user_profile order by timestamp')
results = cur.fetchall()
for post in results:
temp_post = {}
temp_post['username'] = post[0]
temp_post['image'] = post[1].decode('utf-8')
temp_post['text'] = post[2]
temp_post['likes'] = post[3]
temp_post['email'] = post[4]
temp_post['post_id'] = post[5]
temp_post['timestamp'] = datetime.fromtimestamp(
post[6]).strftime('%d-%m-%Y %H:%M')
posts.append(temp_post)
print(posts)
return render_template('admin_feed.html', myEmail=myEmail, posts=posts)
@app.route('/delete_posts', methods=['POST'])
def delete_posts():
post_id = request.form['post_id']
email = request.form['poster_email']
cur = mysql.connection.cursor()
cur.execute('delete from post where email=%s and post_id=%s',
[email, post_id])
mysql.connection.commit()
cur.execute('delete from likes where poster_email=%s and post_id=%s', [
email, post_id])
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_feed'))
if __name__ == '__main__':
app.secret_key = 'acquaintance'
app.config['SESSION_TYPE'] = 'filesystem'
socketio.run(app, debug=True)