-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
431 lines (336 loc) · 12.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
import os
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
from werkzeug.security import check_password_hash, generate_password_hash
from datetime import datetime
from helpers import login_required
# Configure application
app = Flask(__name__)
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///fridge.db")
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/", methods=["GET"])
@login_required
def index():
"""Show portfolio of stocks"""
names = db.execute(
"SELECT name FROM roommates WHERE user_id = ?",
session["user_id"],
)
# Gets person's space
userinfo = db.execute(
"SELECT space, hall, roomnum FROM users WHERE id = ?", session["user_id"]
)
space = userinfo[0]["space"]
dorm = userinfo[0]["hall"] + " " + userinfo[0]["roomnum"]
fridge = db.execute(
"SELECT * FROM storage WHERE user_id = ? ORDER BY timestamp ASC",
session["user_id"],
)
# Current date-time
now = datetime.now()
# Sets number of expired items
expired = 0
for item in fridge:
# If the user inputs an improper time just set the time to be 0-0-0
try:
timestamp = datetime.strptime(item["timestamp"], "%Y-%m-%d")
except ValueError:
timestamp = datetime.strptime("0-0-0", "%Y-%m-%d")
difference = timestamp - now
item["days_left"] = difference.days + 1
if item["days_left"] <= 0:
expired += 1
item["space"] = int(item["quantity"]) * int(item["size"])
# Renders the page
return render_template(
"index.html",
fridge=fridge,
space=space,
expired=expired,
names=names,
dorm=dorm,
)
@app.route("/remove", methods=["POST"])
def remove_item():
item_id = request.form.get("item_id")
user_id = session["user_id"]
current = db.execute(
"SELECT * FROM storage WHERE user_id = ? AND id = ?", user_id, item_id
)[0]
roommate_list = db.execute("SELECT name FROM roommates WHERE user_id = ?", user_id)
roommate_names = [person["name"] for person in roommate_list]
print(roommate_names)
roommate = request.form.get("dropdown")
# Checks to see that a roommate is selected and is chosen
if roommate not in roommate_names:
flash("Roommate needs to be selected")
return redirect("/")
# Updates user's fridge space
db.execute(
"UPDATE users SET space = space + ? WHERE id = ?",
(current["quantity"] * current["size"]),
user_id,
)
# Records the removal into transactions table
db.execute(
"INSERT INTO transactions (user_id, name, size, quantity, roommate, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
current["user_id"],
current["name"],
current["size"],
-current["quantity"],
roommate,
current["timestamp"],
)
# Removes item from storage
db.execute("DELETE FROM storage WHERE user_id = ? AND id = ?", user_id, item_id)
flash("Removed Item")
return redirect("/")
@app.route("/insert", methods=["GET", "POST"])
@login_required
def insert():
"""Insert items into the fridge"""
# Gets roommate names
names = db.execute(
"SELECT name FROM roommates WHERE user_id = ?",
session["user_id"],
)
# Post request
if request.method == "POST":
# Gets form data
user_id = session["user_id"]
item = request.form.get("item").capitalize()
quantity = request.form.get("quantity")
name = request.form.get("roommate")
date = request.form.get("expiration")
# Edge-case catching
if request.form.get("size") is None:
flash("Missing Size")
return redirect("/insert")
if name is None:
flash("Missing Roommate Name")
return redirect("/insert")
if date is None:
flash("Missing Expiration Date")
return redirect("/insert")
if not item:
flash("Missing Item")
return redirect("/insert")
if not quantity.isdigit():
flash("Invalid Quantity")
return redirect("/insert")
if int(quantity) < 1:
flash("Invalid Quantity")
return redirect("/insert")
# Dictionary to map selection to unit size
size_to_number = {
"extrasmall": 1,
"small": 2,
"medium": 4,
"large": 8,
"extralarge": 16,
}
size = size_to_number[request.form.get("size")]
space = db.execute("SELECT space FROM users WHERE id = ?", user_id)
if size * int(quantity) > space[0]["space"]:
flash("Item(s) Too Large")
return redirect("/insert")
# Checks to see if "transactions" table exists
try:
db.execute("SELECT * FROM transactions LIMIT 1")
except Exception:
db.execute(
"CREATE TABLE transactions (id INTEGER PRIMARY KEY, user_id INTEGER, name TEXT, size INTEGER, quantity INTEGER, roommate TEXT, timestamp DATETIME)"
)
# Checks to see if "storage" table exists
try:
db.execute("SELECT * FROM storage LIMIT 1")
except Exception:
db.execute(
"CREATE TABLE storage (id INTEGER PRIMARY KEY, user_id INTEGER, name TEXT, size INTEGER, quantity INTEGER, roommate TEXT, timestamp DATETIME)"
)
# Updates storage, transaction table and user's space
db.execute(
"INSERT INTO transactions (user_id, name, size, quantity, roommate, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
user_id,
item,
size,
int(quantity),
name,
date,
)
db.execute(
"INSERT INTO storage (user_id, name, size, quantity, roommate, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
user_id,
item,
size,
int(quantity),
name,
date,
)
db.execute(
"UPDATE users SET space = space - ? WHERE id = ?",
(int(quantity) * size),
user_id,
)
flash("Item Added")
# Redirect user to home page
return redirect("/")
else:
return render_template("insert.html", names=names)
@app.route("/history")
@login_required
def history():
"""Show history of transactions"""
# Gets the database of transactions
history = db.execute(
"SELECT * FROM transactions WHERE user_id = ?", session["user_id"]
)
# Renders the page
return render_template("history.html", history=history)
@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"):
flash("Must provide username")
return redirect("/login")
# Ensure password was submitted
elif not request.form.get("password"):
flash("Must provide password")
return redirect("/login")
# Query database for username
rows = db.execute(
"SELECT * FROM users WHERE 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")
):
flash("Invalid username and/or password")
return redirect("/login")
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# 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("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
@app.route("/roommate", methods=["GET", "POST"])
@login_required
def roommate():
# Gets roommate names
names = db.execute(
"SELECT name FROM roommates WHERE user_id = ?",
session["user_id"],
)
# Post method
if request.method == "POST":
# Gets data about the symbol and current status
name = request.form.get("roommate").capitalize()
user_id = session["user_id"]
rows = db.execute("SELECT * FROM roommates WHERE name = ?", name)
# If the symbol doesnt exist, throw show a flash message
if not name:
flash("No Name")
return redirect("/roommate")
# If roommate doesn't already exist
if len(rows) == 0:
db.execute(
"INSERT INTO roommates (user_id, name) VALUES (?, ?)", user_id, name
)
else:
db.execute(
"DELETE FROM roommates WHERE user_id = ? AND name = ?", user_id, name
)
# Redirect user to roommate page again
return redirect("/roommate")
else:
return render_template("roommate.html", names=names)
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Gets data from forms
username = request.form.get("username")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
hall = request.form.get("hall")
roomnum = request.form.get("number").upper()
# Query database for username
rows = db.execute("SELECT * FROM users WHERE username = ?", username)
# Ensure username was submitted
if not username:
flash("Must provide username")
return redirect("/register")
# Ensure username is unique
elif len(rows) != 0:
flash("Username already exists")
return redirect("/register")
# Ensure password was submitted
elif not password:
flash("Must provide password")
return redirect("/register")
# Ensure password was submitted
elif not confirmation:
flash("Must provide password (again)")
return redirect("/register")
# Ensure passwords match
elif confirmation != password:
flash("Passwords must match")
return redirect("/register")
elif not hall:
flash("Must provide hall name")
return redirect("/register")
elif not roomnum:
flash("Must provide room number")
return redirect("/register")
# Adds user into the database
db.execute(
"INSERT INTO users (username, hash, hall, roomnum) VALUES (?, ?, ?, ?)",
username,
generate_password_hash(password),
hall,
roomnum,
)
rows = db.execute("SELECT * FROM users WHERE username = ?", username)
session["user_id"] = rows[0]["id"]
# Creates roommates table
try:
db.execute("SELECT * FROM roommates LIMIT 1")
except Exception:
db.execute("CREATE TABLE roommates (user_id INTEGER, name TEXT)")
# Redirect user to home page
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")
@app.route("/info", methods=["GET"])
@login_required
def info():
return render_template("info.html")