-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
528 lines (360 loc) · 14.7 KB
/
app.py
File metadata and controls
528 lines (360 loc) · 14.7 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
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
import sqlite3
from cachelib.file import FileSystemCache
from datetime import datetime, timezone
from dateutil.relativedelta import relativedelta
from flask import Flask, redirect, render_template, request, session, jsonify, url_for, flash
from flask_session import Session
from helpers import createDB, dict_factory, unix_to_date, login_required, update_member_status, days_remaining
from werkzeug.security import check_password_hash, generate_password_hash
app = Flask(__name__)
# ONLY FOR DEVELOPMENT ->
#---
app.config['DEBUG'] = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
#---
""" Configuration of session """
app.config['SESSION_TYPE'] = "cachelib"
app.config['SESSION_PERMANENT'] = True
SESSION_CACHELIB = FileSystemCache(
cache_dir="./sessions",
threshold=500, # A maximum of cached sessions.
default_timeout=10 # Amount of seconds a session will last.
)
app.config.from_object(__name__)
Session(app)
app.jinja_env.filters["convert_date"] = unix_to_date # 'env.filters' is a dict
""" DB creation """
# Could change to another DB in the future but there's no need to get complicated right now.
connection = sqlite3.connect("gyms.db") # Connection.
createDB() # Creates a DB with 'gyms', 'members' and 'routines' tables.
connection.close()
""" WEBPAGES: """
@app.route("/")
def index():
""" The presentation to our service """
return render_template("index.html")
@app.route("/register", methods=["GET", "POST"])
def register():
""" Registration page """
if request.method == "POST":
gym_name = request.form.get("gym_name")
email_address = request.form.get("email_address")
password = request.form.get("password")
repeat_password = request.form.get("repeat_password")
# Check if all the data was input.
if not gym_name or not email_address or not password or not repeat_password:
return "PLEASE FILL ALL FIELDS" # Apology
connection = sqlite3.connect("gyms.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
# Check if email has been used.
cursor.execute("SELECT * " \
"FROM gyms " \
"WHERE gym_email IS ?", (email_address,))
row = cursor.fetchone()
# If there are rows with that email, notify the user.
if row != None:
return "EMAIL IN USE" # Apology
# Check if passwords match.
if password != repeat_password:
return "PASSWORDS MUST MATCH" # Apology
# Check if password is the right length.
if len(password) < 8:
return "PASSWORD MUST BE AT LEAST 8 CHARACTERS LONG" # Apology
# Hashing and salting password.
hashed_password = generate_password_hash(
password, method="scrypt", salt_length=32
)
""" INSERT NEW USER TO DB """
cursor.execute("INSERT INTO " \
"gyms " \
"(gym_Name, gym_email, password_hash) " \
"VALUES (?, ?, ?);",
(gym_name, email_address, hashed_password)
)
connection.commit()
cursor.close()
connection.close()
return redirect("/login")
else:
return render_template("register.html")
@app.route("/login", methods=["GET", "POST"])
def login():
""" Login page """
if request.method == "POST":
gym_email = request.form.get("email_address")
password = request.form.get("password")
# Check if all fields have been filled.
if not gym_email or not password:
flash("Please fill all fields", "error")
return render_template("login.html")
# Get gym data from DB.
query = ("SELECT * " \
"FROM gyms " \
"WHERE gym_email = ?" # Will return the row with all the gym data.
)
connection = sqlite3.connect("gyms.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
cursor.execute(query, (gym_email,)) # Must pass variables as a tuple, even if there is just a single one.
# Save the whole row as a dict. (Honestly, this is unnecesary here, but if it ain't broke don't fix it).
row = cursor.fetchone()
connection.close()
# Check if there are any users with that email and check the password.
if row is None or not check_password_hash(
row["password_hash"], password
):
flash("Invalid email or password", "error")
return render_template("login.html")
session["gym_id"] = row["gym_id"]
return redirect("/homepage")
return render_template("login.html")
@app.route("/logout")
@login_required
def logout():
""" Logout the user """
session.clear()
return redirect("/")
@app.route("/homepage", methods=["GET", "POST"])
@login_required
def homepage():
""" Gym owner's menu """
update_member_status(session["gym_id"])
connection = sqlite3.connect("gyms.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
search_query = ""
if request.method == "POST":
search_query = request.form.get("member_search")
query = "SELECT * " \
"FROM members " \
"WHERE gym_id = ? AND name LIKE ?;"
cursor.execute(query, (session["gym_id"], "%" + search_query + "%"))
members = cursor.fetchall()
return render_template("homepage.html", members=members)
else:
query = "SELECT * " \
"FROM members " \
"WHERE gym_id = ?;"
cursor.execute(query, (session["gym_id"],))
members = cursor.fetchall()
query = "SELECT * " \
"FROM members " \
"WHERE gym_id = ?;"
cursor.execute(query, (session["gym_id"],))
members = cursor.fetchall()
connection.close()
return render_template("homepage.html", members=members)
@app.route("/members/<int:member_id>", methods=["GET", "POST"])
@login_required
def member_detail(member_id):
""" Show member information """
connection = sqlite3.connect("gyms.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
query = "SELECT * " \
"FROM members " \
"WHERE member_id = ?;"
cursor.execute(query, (member_id,))
member_row = cursor.fetchone()
# Member data
name = member_row["name"]
gym_id = member_row["gym_id"]
joined_date = member_row["joined_date"]
end_date = member_row["end_date"]
status = member_row["status"]
last_visit = member_row["last_visit"]
days_left = days_remaining(member_row)
# Routine data
query = "SELECT * " \
"FROM routines " \
"WHERE member_id = ?;"
cursor.execute(query, (member_id,))
routine_row = cursor.fetchone()
cursor.close()
connection.close()
monday = routine_row["Monday"]
tuesday = routine_row["Tuesday"]
wednesday = routine_row["Wednesday"]
thursday = routine_row["Thursday"]
friday = routine_row["Friday"]
saturday = routine_row["Saturday"]
sunday = routine_row["Sunday"]
# Delete button
if request.method == "POST":
if request.form.get("delete_member"):
connection = sqlite3.connect("gyms.db")
cursor = connection.cursor()
query = "DELETE FROM members " \
"WHERE member_id = ? AND gym_id = ?;"
cursor.execute(query, (member_id, gym_id))
connection.commit()
query = "DELETE FROM routines " \
"WHERE member_id = ?;"
cursor.execute(query, (member_id,))
connection.commit()
connection.close()
return redirect("/homepage")
return render_template("member_details.html",
name=name,
gym_id=gym_id,
joined_date=joined_date,
end_date=end_date,
status=status,
last_visit=last_visit,
member_id=member_id,
days_left=days_left,
monday=monday,
tuesday=tuesday,
wednesday=wednesday,
thursday=thursday,
friday=friday,
saturday=saturday,
sunday=sunday)
@app.route("/edit_routine/<int:member_id>", methods=["GET", "POST"])
@login_required
def edit_routine(member_id):
""" Allows trainers to add and edit a member's routine """
connection = sqlite3.connect("gyms.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
query = "SELECT * " \
"FROM routines " \
"WHERE member_id = ?;"
cursor.execute(query, (member_id,))
current_routine = cursor.fetchone()
if request.method == "POST":
print("POSTED")
monday = request.form.get("monday")
tuesday = request.form.get("tuesday")
wednesday = request.form.get("wednesday")
thursday = request.form.get("thursday")
friday = request.form.get("friday")
saturday = request.form.get("saturday")
sunday = request.form.get("sunday")
print(monday)
query = "UPDATE routines " \
"SET " \
"Monday = ?, " \
"Tuesday = ?, " \
"Wednesday = ?, " \
"Thursday = ?, " \
"Friday = ?, " \
"Saturday = ?, " \
"Sunday = ? " \
"WHERE member_id = ?;"
cursor.execute(query, (monday,
tuesday,
wednesday,
thursday,
friday ,
saturday,
sunday,
member_id))
connection.commit()
return redirect(url_for("member_detail", member_id=member_id))
connection.close
return render_template("edit_routine.html",
routine=current_routine,
member_id=member_id)
@app.route("/new_member", methods=["GET", "POST"])
@login_required
def new_member():
""" INSERT A NEW MEMBER TO DB """
if request.method == "POST":
# Get data from form.
member_id = request.form.get("id")
name = request.form.get("first_name")
last_name = request.form.get("last_name")
subscription_months = int(request.form.get("subscription_months"))
# Implicit data from gym/date.
gym_id = session["gym_id"]
joined_date = datetime.now()
end_date = joined_date + relativedelta(months=subscription_months)
# ID validation.
if isinstance(member_id, int):
return "PLEASE INPUT A VALID ID" # Apology
if not member_id or not name or not last_name or not gym_id or not joined_date or not end_date:
return "Please fill all fields." # Apology
connection = sqlite3.connect("gyms.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
query = ("SELECT member_id "
"FROM members "
"WHERE member_id = ?;"
)
cursor.execute(query, (member_id,))
row = cursor.fetchone()
if row is not None:
return "Member is already registered" # Apology
# Insert member into members table.
query = ("INSERT INTO "
"members ("
"member_id, " \
"name, " \
"gym_id, " \
"joined_date, " \
"end_date)" \
"VALUES (?, ?, ?, ?, ?);"
)
joined_unix_timestamp = int(joined_date.timestamp())
end_unix_timestamp = int(end_date.timestamp())
cursor.execute(query,
(member_id, name + " " + last_name, gym_id, joined_unix_timestamp, end_unix_timestamp) # Awful design honestly
)
connection.commit()
# Insert member into routines table
query = "INSERT INTO routines (member_id) " \
"VALUES (?) ;"
cursor.execute(query, (member_id,))
connection.commit()
cursor.close()
connection.close()
return redirect("homepage")
return render_template("new_member.html")
@app.route("/reception")
@login_required
def reception():
""" Render reception """
update_member_status(session["gym_id"]) # Update members status. Not the best design.
return render_template("reception.html")
@app.route("/api/check_member", methods=["POST"]) # Handle the JS. Helps prevent page from reloading. Calling it /api... is convention.
def check_member_api():
""" Check if input id exists and use JS as an intermediary to modify HTML """
# Get JSON data sent from JavaScript.
id = request.get_json()
member_id = id.get("member_id") # This matches the key sent frm JS.
connection = sqlite3.connect("gyms.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
query = "SELECT * " \
"FROM members " \
"WHERE member_id = ? AND gym_id = ?;"
cursor.execute(query, (member_id, session["gym_id"]))
row = cursor.fetchone()
if row is None: # If no id has been found, return false.
# Return a JSON
connection.close()
return jsonify({"exists": False})
elif row["status"] == "Inactive":
connection.close()
return jsonify({"exists": True,
"status": row["status"]})
# Log the last visit of member
last_visit = int(datetime.now().timestamp())
query = "UPDATE members " \
"SET last_visit = ? " \
"WHERE member_id = ?;"
cursor.execute(query, (last_visit, member_id))
connection.commit()
query = "INSERT INTO " \
"access_logs (member_id, check_in_time) " \
"VALUES (?, ?);"
cursor.execute(query, (member_id, last_visit))
connection.commit()
connection.close()
return jsonify({
"exists": True,
"status": row["status"],
"name": row["name"] # Send the name to the JS.
})