-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
602 lines (426 loc) · 21.1 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
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
from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash
from helpers import error, login_required, errorhandler
# Configure application
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Ensure responses aren't cached
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///NoteCloud.db")
@app.route("/", methods=["GET", "POST"])
@login_required
def index():
"""Show index of all of user's notes"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# User wants to post a note
if request.form.get("submit") == "submit":
# Ensure text was submitted
if not request.form.get("textbox"):
return error("must provide text", 400)
# Call submit function
submitStatus = submit(request.form.get("textbox"))
if submitStatus[1] != 200:
return error(submitStatus[0], submitStatus[1])
# User wants to share their note with other profiles
elif request.form.get("share"):
# Call share function
shareStatus = share(request.form.get("share"), submitStatus[0])
if shareStatus[1] != 200:
return error(shareStatus[0], shareStatus[1])
# Notify user how many profiles note was shared with
flash(f'Note shared with {shareStatus[0]} other profiles')
# Redirect user to homepage
return redirect("/")
else:
# Ensure usernames was submitted
if not request.form.get("share"):
return error("must provide usernames", 400)
# Ensure note ID was submitted
if not request.form.get("submit"):
return error("must provide note ID", 400)
# Call share function to share note
shareStatus = share(request.form.get("share"), request.form.get("submit"))
if shareStatus[1] != 200:
return error(shareStatus[0], shareStatus[1])
# Notify user how many profiles note was shared with
flash(f'Note shared with {shareStatus[0]} other profiles')
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
# Query database for user's notes
rows = db.execute("SELECT id, author, text, timestamp FROM notes WHERE id IN (SELECT note_id FROM participants WHERE user_id = :id) ORDER BY timestamp DESC",
id=session["user_id"])
# Call share_data function
share_data = shareData(rows)
# Render page with user's notes
return render_template("index.html", rows=rows, share_data=share_data, id="id", usernames="usernames", shares="shares")
@app.route("/owned", methods=["GET", "POST"])
@login_required
def owned():
"""Show index of user's owned notes"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# User wants to post a note
if request.form.get("submit") == "submit":
# Ensure text was submitted
if not request.form.get("textbox"):
return error("must provide text", 400)
# Call submit function
submitStatus = submit(request.form.get("textbox"))
if submitStatus[1] != 200:
return error(submitStatus[0], submitStatus[1])
# User wants to share their note with other profiles
elif request.form.get("share"):
# Call share function
shareStatus = share(request.form.get("share"), submitStatus[0])
if shareStatus[1] != 200:
return error(shareStatus[0], shareStatus[1])
# Notify user how many profiles note was shared with
flash(f'Note shared with {shareStatus[0]} other profiles')
# Redirect user to homepage
return redirect("/owned")
else:
# Ensure usernames was submitted
if not request.form.get("share"):
return error("must provide usernames", 400)
# Ensure note ID was submitted
if not request.form.get("submit"):
return error("must provide note ID", 400)
# Call share function to share note
shareStatus = share(request.form.get("share"), request.form.get("submit"))
if shareStatus[1] != 200:
return error(shareStatus[0], shareStatus[1])
# Notify user how many profiles note was shared with
flash(f'Note shared with {shareStatus[0]} other profiles')
return redirect("/owned")
else:
# Query database for user's owned notes
rows = db.execute("SELECT id, author, text, timestamp FROM notes WHERE author = :user_name ORDER BY timestamp DESC",
user_name=session["user_name"])
# Call share_data function
share_data = shareData(rows)
# Render page with user's notes
return render_template("owned.html", rows=rows, share_data=share_data, id="id", usernames="usernames", shares="shares")
@app.route("/shared", methods=["GET", "POST"])
@login_required
def shared():
"""Show index of notes shared with user"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# User wants to post a note
if request.form.get("submit") == "submit":
# Ensure text was submitted
if not request.form.get("textbox"):
return error("must provide text", 400)
# Call submit function
submitStatus = submit(request.form.get("textbox"))
if submitStatus[1] != 200:
return error(submitStatus[0], submitStatus[1])
# User wants to share their note with other profiles
elif request.form.get("share"):
# Call share function
shareStatus = share(request.form.get("share"), submitStatus[0])
if shareStatus[1] != 200:
return error(shareStatus[0], shareStatus[1])
# Notify user how many profiles note was shared with
flash(f'Note shared with {shareStatus[0]} other profiles')
# Redirect user to homepage
return redirect("/shared")
else:
# Ensure usernames was submitted
if not request.form.get("share"):
return error("must provide usernames", 400)
# Ensure note ID was submitted
if not request.form.get("submit"):
return error("must provide note ID", 400)
# Call share function to share note
shareStatus = share(request.form.get("share"), request.form.get("submit"))
if shareStatus[1] != 200:
return error(shareStatus[0], shareStatus[1])
# Notify user how many profiles note was shared with
flash(f'Note shared with {shareStatus[0]} other profiles')
return redirect("/shared")
else:
# Query database for user's shared notes
rows = db.execute("SELECT id, author, text, timestamp FROM notes WHERE id IN (SELECT note_id FROM participants WHERE user_id = :id) AND author != :user_name ORDER BY timestamp DESC",
id=session["user_id"],user_name=session["user_name"])
# Call share_data function
share_data = shareData(rows)
# Render page with user's notes
return render_template("shared.html", rows=rows, share_data=share_data, id="id", usernames="usernames", shares="shares")
@app.route("/deletenote")
@login_required
def deletenote():
"""Delete note"""
# Ensure note ID was submitted
if not request.args.get("note_id"):
return error("must provide note ID", 400)
# If redirect argument wasn't submitted, redirect to home route
if not request.args.get("redirect"):
origin = "/"
# If it was submitted ensure it is valid and save it
else:
if request.args.get("redirect") not in ["owned", "shared"]:
return error("invalid redirect", 400)
origin = "/" + request.args.get("redirect")
note_id = int(request.args.get("note_id"))
# Ensure note exists
rows = db.execute("SELECT user_id, author FROM participants JOIN notes ON participants.note_id = notes.id WHERE note_id = :note_id",
note_id=note_id)
if len(rows) == 0:
return error("note not found", 404)
# Ensure user has access to note
hasAccess = False
for row in rows:
if row["user_id"] == session["user_id"]:
hasAccess = True
break
if hasAccess == False:
return error("cannot delete note without having access to it", 403)
# If user is author of note, delete note completely
if rows[0]["author"] == session["user_name"]:
# Delete all access to note
if db.execute("DELETE FROM participants WHERE note_id = :note_id",
note_id=note_id) == 0:
return error("failed to delete access to note", 503)
# Delete note
if db.execute("DELETE FROM notes WHERE id = :note_id",
note_id=note_id) == 0:
return error("failed to delete note", 503)
# Else just delete user's access to note
else:
if db.execute("DELETE FROM participants WHERE note_id = :note_id AND user_id = :user_id",
note_id=note_id, user_id=session["user_id"]) == 0:
return error("failed to delete access to note", 503)
# Redirect user to original route
return redirect(origin)
@app.route("/signup", methods=["GET", "POST"])
def signup():
"""Sign up user"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return error("must provide username", 400)
# Ensure username is not too short or too long
if len(request.form.get("username")) < 4 or len(request.form.get("username")) > 40:
return error("username length invalid", 403)
# Ensure password was submitted
if not request.form.get("password"):
return error("must provide password", 400)
# Ensure confirmation password matches password
elif request.form.get("password") != request.form.get("confirmation"):
return error("confirmation password wrong", 403)
# Ensure that username is not already taken
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
if len(rows) != 0:
return error("username already taken", 409)
# Insert username and hashed password into database
if not db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
username=request.form.get("username"),
hash=generate_password_hash(request.form.get("password"), method='pbkdf2:sha256', salt_length=8)):
return error("failed to save profile to database", 503)
# Ensure user is in database
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
session["user_name"] = rows[0]["username"]
# Redirect user to home page
flash(f'User "{request.form.get("username")}" successfully registered')
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("signup.html")
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in"""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return error("must provide username", 400)
# Ensure password was submitted
elif not request.form.get("password"):
return error("must provide password", 400)
# Query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
return error("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
session["user_name"] = rows[0]["username"]
# Redirect user to home page
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
"""Manage user's profile"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# User wants to change their password
if request.form.get("submit") == "change":
# Ensure old password was submitted
if not request.form.get("oldPassword"):
return error("must provide old password", 400)
# Ensure new password was submitted
if not request.form.get("newPassword"):
return error("must provide new password", 400)
# Ensure confirmation password matches new password
if request.form.get("newPassword") != request.form.get("confirmation"):
return error("confirmation password wrong", 403)
# Ensure new password is not the same as old password
if request.form.get("oldPassword") == request.form.get("newPassword"):
return error("new password must be different from old password", 409)
# Query database for user
rows = db.execute("SELECT hash FROM users WHERE id = :id",
id=session["user_id"])
# Ensure old password is correct
if not check_password_hash(rows[0]["hash"], request.form.get("oldPassword")):
return error("invalid password", 403)
# Update password
if db.execute("UPDATE users SET hash = :hash WHERE id = :id",
hash=generate_password_hash(request.form.get("newPassword"), method='pbkdf2:sha256', salt_length=8),
id=session["user_id"]) == 0:
return error("failed to update password", 503)
# Redirect user to home page
flash('Password successfully changed')
return redirect("/")
# User wants to delete their profile
else:
# Ensure user confirmed deletion
if not request.form.get("confirm"):
return error("must confirm deletion", 403)
# Redirect user to deletion page
return redirect("/delete")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("profile.html")
@app.route("/delete", methods=["GET", "POST"])
@login_required
def delete():
"""Delete user's profile from database"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# User is sure they want to delete their profile
if request.form.get("confirm") == "yes":
# Delete access to notes authored by profile
db.execute("DELETE FROM participants WHERE note_id IN (SELECT id FROM notes WHERE author = :user_name)",
user_name=session["user_name"])
# Delete user's access to all other notes
db.execute("DELETE FROM participants WHERE user_id = :user_id",
user_id=session["user_id"])
# Delete notes authored by profile
db.execute("DELETE FROM notes WHERE author = :user_name",
user_name=session["user_name"])
# Delete profile
if db.execute("DELETE FROM users WHERE id = :id",
id=session["user_id"]) == 0:
return error("failed to delete profile", 503)
# Log out user
return redirect("/logout")
# User doesn't want to delete their profile
else:
# Redirect user to homepage
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("delete.html")
@app.route("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
def submit(text):
"""Submit notes to database"""
# Ensure text isn't too short
if len(text) - text.count("\r") > 1000:
return "text length invalid", 403
# Insert note into database
note_id = db.execute("INSERT INTO notes (author, text, timestamp) VALUES(:user_name, :text, datetime('now', '+2 hours'))",
user_name=session["user_name"], text=text)
# Ensure note was submitted
if not note_id:
return "failed to save note to database", 503
# Give access to user
if not db.execute("INSERT INTO participants (note_id, user_id) VALUES (:note_id, :user_id)",
note_id=note_id, user_id=session["user_id"]):
return "failed to save access to note", 503
return note_id, 200
def share(usernames, note_id):
"""Share note with other users"""
# Iterate over usernames
count = 0
for username in usernames.split():
# Ensure username has valid length
if len(username) < 4 or len(username) > 40:
continue
# Ensure username is not current user's username
if username == session["user_name"]:
continue
# Ensure profile actually exists
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=username)
if len(rows) != 1:
continue
# Save user ID
user_id = rows[0]["id"]
# Ensure profile doesn't already have access to note
rows = db.execute("SELECT * FROM participants WHERE note_id = :note_id AND user_id = :user_id",
note_id=note_id, user_id=user_id)
if len(rows) != 0:
continue
# Share note with profile
if not db.execute("INSERT INTO participants (note_id, user_id) VALUES (:note_id, :user_id)",
note_id=note_id, user_id=user_id):
return "failed to share note with user", 503
# Count how many profiles note was shared with
count += 1
# Return that count
return count, 200
def shareData(rows):
"""Get share data for user's notes"""
# Make list of data about sharing
share_data = list()
# For each note, save number of profiles note is shared with and usernames of profiles note is shared with
for row in rows:
shares = db.execute("SELECT username FROM users WHERE id in (SELECT user_id FROM participants WHERE note_id = :note_id)",
note_id=row["id"])
usernames = ""
for sharerow in shares:
if sharerow["username"] != session["user_name"]:
if len(usernames) != 0:
usernames += ", "
usernames += sharerow["username"]
share_data.append({"shares":len(shares), "usernames":usernames})
return share_data
# Check for errors
for code in default_exceptions:
app.errorhandler(code)(errorhandler)