-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
407 lines (346 loc) · 13.3 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
import os
import validators as vd
from UserBalance import UserBalance
from cs50 import SQL
from dotenv import load_dotenv
from flask import Flask, flash, redirect, render_template, url_for, request, session, jsonify
from flask_session import Session
from dotenv import load_dotenv
# Configure app
app = Flask(__name__)
# Custom Filter for Numeric Values
app.jinja_env.filters["php"] = vd.formatToPHP
# Load secret key from .env and configure secret key to use Sessions
load_dotenv()
app.secret_key = os.getenv('SECRET_KEY')
# Configure session to use filesystem (instead of signed cookies) - we will be possibly handling massive amounts of data
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
load_dotenv()
db = SQL(os.getenv('DB_URL'))
entryCategory = {
'buttonID': [1, 2, 3],
'type': ['Savings', 'Spendings','Allowance']
}
curr_user_bal = None
# Adapted from CS50x Finance: https://cs50.harvard.edu/x/2024/psets/9/finance
# Ensure that the data is always updated when the app is running
@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("/")
@vd.login_required
def dashboard():
""" Handle user finances and transactions when logged in """
userFinancesData = getUserFinances()
if userFinancesData:
curr_user_bal = UserBalance(userFinancesData[0]['savings'], userFinancesData[0]['spendings'], userFinancesData[0]['allowance'])
transactionHistory = db.execute(
"SELECT * FROM transactions WHERE user_id = ? ORDER BY transaction_date DESC LIMIT 10", # Limit 10 transactions only
(session["user_id"],)
)
if transactionHistory:
for transaction in transactionHistory:
transacTypeNum = transaction['transaction_type']
transaction['category'] = vd.toCategory(transacTypeNum, entryCategory)
return render_template(
"index.html",
userSavings=curr_user_bal.savings,
userSpendings=curr_user_bal.spendings,
userAllowance=curr_user_bal.allowance,
userTransactions=transactionHistory
)
@app.route("/profile")
@vd.login_required
def profile():
""" Load information for profile page """
counts = db.execute(
"SELECT COUNT(*) FROM goal_counts WHERE user_id = ?",
(session['user_id'],)
)
user = getLoginInfo(session['user_id'], True)
goalData = getUserGoals()
if goalData:
for goal in goalData:
goal['progress'] = vd.getPercent(goal['curr_dep'], goal['total_amt'])
return render_template("profile.html", userName=user[0]['username'], goalCounts=counts[0]['COUNT(*)'], goals=goalData)
@app.route("/submit", methods=['POST'])
@vd.login_required
def getFormInput():
""" Process dashboard form submission/s """
request_userEntry = request.get_json()
rq_datetime = vd.getCurrTime()
rq_btnId = int(request_userEntry['btn_id'])
rq_amount = float(request_userEntry['amt'])
# Queries to update [Finances] table
if rq_btnId == 1:
updateSavings(rq_amount)
if rq_btnId == 2:
if not curr_user_bal.check_allowance(rq_amount):
return jsonify({'result': 'error', 'message': 'Insufficient Allowance!'}), 400
updateSpendings(rq_amount)
if rq_btnId == 3:
if not curr_user_bal.check_allowance(rq_amount):
return jsonify({'result': 'error', 'message': 'Insufficient Allowance!'}), 400
updateAllowance(rq_amount)
# Query to log transactions into [Transactions] table
db.execute(
"INSERT INTO transactions(user_id, transaction_type, transaction_date, amt) VALUES(?, ?, ?, ?)",
session['user_id'], rq_btnId, rq_datetime, rq_amount
)
# Queries to retrieve updated entries to [Transactions] and [Finances] table
latest_transaction = getLatestEntry()
updated_finances = getUserFinances()
# Merge data into a single JSON-like format and send to JS
merged_data = vd.mergeEntries(latest_transaction, updated_finances)
return jsonify(merged_data), 200
@app.route("/delete", methods=['POST'])
@vd.login_required
def deleteUserData():
""" Delete all user data from all tables in the DB """
delete_request = request.get_json()
username = delete_request['username']
db.execute(
"DELETE FROM finances WHERE user_id = ?",
(session['user_id'],)
)
db.execute(
"DELETE FROM transactions WHERE user_id = ?",
(session['user_id'],)
)
db.execute(
"DELETE FROM users WHERE username = ?",
(username,)
)
db.execute(
"DELETE FROM goals_info WHERE id IN (SELECT goals_info.id FROM goal_counts JOIN goals_info ON goal_counts.goal_ID = goals_info.id WHERE goal_counts.user_ID = ?)",
(session['user_id'],)
)
db.execute(
"DELETE FROM goal_counts WHERE user_ID = ?",
(session['user_id'],)
)
return jsonify({'result': 'success'}), 200
@app.route('/change', methods=['POST'])
@vd.login_required
def editUserPassword():
""" Change user passwords """
newPass = request.get_json()['new_password']
newSalt = vd.generatePasswordSalt()
conv_newSalt = newSalt.hex()
newHash = vd.generateHash(newPass, conv_newSalt)
db.execute(
"UPDATE users SET hash = ?, salt = ? WHERE id = ?",
newHash, conv_newSalt, session['user_id']
)
return jsonify({'result': 'success'}), 200
@app.route('/create_new', methods=['POST'])
@vd.login_required
def createGoal():
""" Add new user goals """
goalInfo = request.get_json()
curr_dep = 0.0
goalName = goalInfo['goal_name']
goalDesc = goalInfo['goal_desc']
goalTotal = float(goalInfo['goal_amt'])
db.execute(
"INSERT INTO goals_info(name, desc, total_amt, curr_dep) VALUES(?, ?, ?, ?)",
goalName, goalDesc, goalTotal, curr_dep
)
latestGoalID = getGoalID()
db.execute(
"INSERT INTO goal_counts(user_ID, goal_ID) VALUES(?, ?)",
session['user_id'], latestGoalID
)
uGoals, latestGoal = getUserGoals(), None
if uGoals:
for goal in uGoals:
if goal['id'] == latestGoalID:
latestGoal = goal
break
latestGoal['progress'] = vd.getPercent(latestGoal['curr_dep'], latestGoal['total_amt'])
return jsonify(latestGoal), 200
@app.route('/edit_goal', methods=['POST'])
def editUserGoal():
""" Edit user goals. """
editedGoal = request.get_json()
updatedGoal = None
try:
db.execute(
"UPDATE goals_info SET name = ?, desc = ?, total_amt = ? WHERE id = ?",
editedGoal['name'], editedGoal['desc'], editedGoal['total_amt'], editedGoal['id']
)
updatedGoal = db.execute(
"SELECT * FROM goals_info WHERE id = ?",
editedGoal['id']
)
except Exception as e:
return jsonify({"result":"error", "msg": str(e)}), 500
return jsonify(updatedGoal[0]), 200
@app.route('/remove', methods=['POST'])
def removeGoal():
""" Remove user goals from DB. """
deleteInfo = request.get_json()
try:
db.execute(
"DELETE FROM goal_counts WHERE goal_ID = ? AND user_id = ?",
deleteInfo['id'], session['user_id']
)
db.execute(
"DELETE FROM goals_info WHERE name = ? AND id = ?",
deleteInfo['name'], deleteInfo['id']
)
return jsonify({'result': 'success'}), 200
except Exception as e:
return jsonify({'result' : 'error', 'message': str(e)}), 500
@app.route('/update', methods=['POST'])
def updateGoalProgress():
""" TODO: DEBUG | Update curr_dep FROM goals_info table and recalculate progress. """
goalToUpdate = request.get_json()
try:
db.execute(
"UPDATE goals_info SET curr_dep = curr_dep + ? WHERE id = ? AND name = ?",
goalToUpdate['amt'], goalToUpdate['id'], goalToUpdate['name']
)
updatedGoal = db.execute(
"SELECT * FROM goals_info WHERE id = ? AND name = ?",
goalToUpdate['id'], goalToUpdate['name']
)
goal = updatedGoal[0]
goal['progress'] = vd.getPercent(goal['curr_dep'], goal['total_amt'])
return jsonify(goal), 200
except Exception as e:
return jsonify({'result':'error', 'message': str(e)}), 500
@app.route('/goal_data')
def sendGoals():
""" Send user's goal data as JSON to the client """
return jsonify(getUserGoals()), 200
def getGoalID():
goalID = db.execute(
"SELECT id FROM goals_info ORDER BY ROWID DESC LIMIT 1"
)
return goalID[0]['id'] if goalID else None
def getUserGoals():
""" Extract user goals from the Goals_Info DB """
goals = db.execute(
"SELECT goals_info.* FROM goal_counts JOIN goals_info ON goal_counts.goal_ID = goals_info.id WHERE goal_counts.user_ID = ?",
session["user_id"]
)
return goals
def getUserFinances():
""" Get user's finances from Finance DB """
financeData = db.execute(
"SELECT * FROM finances WHERE user_id = ?",
(session["user_id"],)
)
return financeData
def getLatestEntry():
""" Get latest transaction log from Transaction DB """
transactionData = db.execute(
"SELECT * FROM transactions WHERE user_id = ? ORDER BY ROWID DESC LIMIT 1",
(session['user_id'],)
)
if transactionData:
nType = transactionData[0]['transaction_type']
transactionData[0]['transaction_type'] = vd.toCategory(nType, entryCategory)
return transactionData
def updateSavings(amount):
""" Update savings from Finance DB """
db.execute(
"UPDATE finances SET savings = savings + ? WHERE user_id = ?",
amount, session['user_id']
)
db.execute(
"UPDATE finances SET allowance = allowance - ? WHERE user_id = ?",
amount, session['user_id']
)
return
def updateSpendings(amount):
""" Update spendings from Finance DB """
db.execute(
"UPDATE finances SET spendings = spendings + ? WHERE user_id = ?",
amount, session['user_id']
)
db.execute(
"UPDATE finances SET allowance = allowance - ? WHERE user_id = ?",
amount, session['user_id']
)
return
def updateAllowance(amount):
""" Update allowance from Finance DB """
db.execute(
"UPDATE finances SET allowance = allowance + ? WHERE user_id = ?",
amount, session['user_id']
)
return
def getLoginInfo(user, isUID):
if not isUID:
return db.execute(
"SELECT * FROM users WHERE username = ?",
(user,)
)
return db.execute(
"SELECT * FROM users WHERE id = ?",
(user,)
)
@app.route("/register", methods=["GET", "POST"])
def register():
""" Add more user/s """
fSavings, fSpendings, fAllowance = 0.0, 0.0, 0.0
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
confirmedPassword = request.form.get("confirm")
userDB = getLoginInfo(username, False)
if len(userDB) != 0:
return vd.apology("Username already exists.")
if not username or not password or not confirmedPassword:
return vd.apology("Input Field/s are empty.")
if not password == confirmedPassword:
return vd.apology("Passwords does not match with each other!")
if not vd.validatePasswordStructure(password):
return vd.apology("Password should contain at least: 1 symbol, 1 number and length of 8 or more.")
passwordSalt = vd.generatePasswordSalt()
# Convert the salt from a byte object to a string to store in DB
convertedSalt = passwordSalt.hex()
hashedPassword = vd.generateHash(password, convertedSalt)
db.execute(
"INSERT INTO users(username, hash, salt) VALUES(?, ?, ?)",
username, hashedPassword, convertedSalt
)
db.execute(
"INSERT INTO finances(user_id, savings, spendings, allowance) VALUES(?, ?, ?, ?)",
session['user_id'], fSavings, fSpendings, fAllowance
)
id = getLoginInfo(username, False)
session["user_id"] = id[0]["id"]
return redirect("/")
return render_template("register.html")
@app.route("/login", methods=["GET", "POST"])
def login():
""" Log user/s in to the app """
session.clear()
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
if not username or not password:
return vd.apology("Input field/s are empty.")
rowUserData = getLoginInfo(username, False)
if len(rowUserData) != 1 or not vd.validatePassword(password, rowUserData[0]["salt"], rowUserData[0]["hash"]):
return vd.apology("Invalid Username/Password!")
session["user_id"] = rowUserData[0]["id"]
return redirect("/")
return render_template("login.html")
@app.route("/logout")
def logout():
""" Log user/s out from the app """
# Forget any user_id and redirect to login form
session.clear()
return redirect("/")
if __name__ == "__main__":
app.run(debug=True,use_reloader=True)