-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1409 lines (1232 loc) · 48.8 KB
/
app.py
File metadata and controls
1409 lines (1232 loc) · 48.8 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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# sqlite3 library
import sqlite3
# flask modules
from flask import (
Flask,
render_template,
request,
redirect,
abort,
url_for,
session,
g
)
# session from flask session
from flask_session import Session
# my helper file functions
from helper import (
login_required,
verify_date,
is_time_on_or_before,
get_reservation_time_end,
get_reservation_time_start,
get_current_date,
check_thirty_minutes,
get_next_day,
verify_registration_times
)
# regex library
import re
# library to verify passwords
from werkzeug.security import (
check_password_hash,
generate_password_hash
)
# app declaration
app = Flask(__name__)
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# setup db
def get_db():
if 'db' not in g:
g.db = sqlite3.connect("trimtime.db")
g.db.row_factory = sqlite3.Row # Allows accessing columns by name
return g.db
@app.teardown_appcontext
def close_db(error):
db = g.pop('db', None)
if db is not None:
db.close()
def query_db(query, args=(), one=False):
"""Wrapper to make queries look like CS50's query_db"""
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
get_db().commit() # Auto-commit changes
return (rv[0] if rv else None) if one else rv
# Ensure responses aren't cached
@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
# landing page - pick which you're logging in as
@app.route("/", methods=["GET", "POST"])
def landing():
# redirect to login page when user picks an option
if request.method == "POST":
user = request.form.get("user")
if user == "customer" or user == "salon":
return redirect(url_for('login', user=user))
abort(404, description="User not found")
else:
return render_template("landing.html")
# index page - customized for user. need to be logged in
@app.route("/<user>", methods=["GET", "POST"])
@login_required
def index(user):
# index page for salon
if user == "salon":
# reservations made with that salon
salon_reservations = query_db(
"SELECT * \
FROM reservations \
WHERE salon_id = ? \
AND status = ?",
(
session["user_id"],
"Pending"
)
)
# custom list made for salon for display
salon_reservations_list = [
{
"id": reservation["id"],
"username": query_db(
"SELECT username \
FROM users \
WHERE id = ?",
(reservation["user_id"],)
)[0]["username"],
"haircut": query_db(
"SELECT name \
FROM haircuts \
WHERE id = ?",
(reservation["haircut_id"],)
),
"specialized_description": reservation["specialized_description"],
"estimated_time": reservation["estimated_time"],
"reservation_date": reservation["reservation_date"],
"reservation_time_start": reservation["reservation_time_start"],
"reservation_time_end": reservation["reservation_time_end"],
} for reservation in salon_reservations
]
# total reservations made with that salon
total_reservations = len(salon_reservations_list)
return render_template(
"index.html",
user=user,
salon_reservations_list=salon_reservations_list,
total_reservations=total_reservations
)
# user is customer
else:
# if you click on an available salon in the index page
if request.method == "POST":
# get salon_id from form
salon_id = request.form.get("salon")
# verify if that salon id is valid
if not salon_id:
abort(400, description="Missing parameters")
# get required tables from database
salon_name = query_db(
"SELECT salon_name \
FROM salons \
WHERE salon_id = ?",
(salon_id,)
)[0]["salon_name"]
reservations = query_db(
"SELECT * \
FROM reservations \
WHERE user_id = ? \
AND salon_id = ? \
AND status = ?",
(
session["user_id"],
salon_id,
"Pending"
)
)
# if you've have a pending reservation before
if reservations:
# check if the reservation that you have pending is more than one
if len(reservations) > 1:
abort(400, "Error in retrieving reservation: Multiple reservations")
return redirect(url_for("reserved", salon_name=salon_name))
else:
return redirect(url_for("reservation", salon_name=salon_name))
# GET method
else:
# get list of salons
salons = query_db("SELECT * FROM salons")
# check if you have a reservation due in some time
salon_due = query_db(
"SELECT salon_name \
FROM salons \
WHERE salon_id = ?",
(alert(),)
)
# get if you have a fulfilled reservation for that day
salon_fulfilled = query_db(
"SELECT salon_name \
FROM salons \
WHERE salon_id = \
(\
SELECT salon_id \
FROM reservations \
WHERE reservation_date = ? \
AND status = ? \
)",
(
get_current_date(),
"Fulfilled"
)
)
return render_template(
"index.html",
user=session["user_type"],
salons=salons,
salon_due=salon_due,
salon_fulfilled=salon_fulfilled
)
# profile page
@app.route("/<user>/profile", methods=["GET"])
@login_required
def profile(user):
# user is customer
if user == "customer":
# get profile details from database
details = query_db(
"SELECT * \
FROM users \
WHERE id = ?",
(session["user_id"],)
)
# get the total number of reservations you've made with the app till date
total_number_of_reservations = query_db(
"SELECT COUNT(id) \
FROM reservations \
WHERE user_id = ?",
(session["user_id"],)
)[0]["COUNT(id)"]
# user is salon
else:
# custom table for salon details
details = [
{
"id": session["user_id"],
"salon_name": query_db(
"SELECT username \
FROM users \
WHERE id = ?",
(session["user_id"],)
)[0]["username"],
"email": query_db(
"SELECT email \
FROM users \
WHERE id = ?",
(session["user_id"],)
)[0]["email"],
"barber_number": query_db(
"SELECT barber_number \
FROM salons \
WHERE salon_id = ?",
(session["user_id"],)
)[0]["barber_number"],
"open_time": query_db(
"SELECT open_time \
FROM salons \
WHERE salon_id = ?",
(session["user_id"],)
)[0]["open_time"],
"close_time": query_db(
"SELECT close_time \
FROM salons \
WHERE salon_id = ?",
(session["user_id"],)
)[0]["close_time"],
}
]
# get total number of reservations made with salon till date
total_number_of_reservations = query_db(
"SELECT COUNT(id) \
FROM reservations \
WHERE salon_id = ?",
(session["user_id"],)
)[0]["COUNT(id)"]
return render_template(
"profile.html",
details=details,
user=user,
total_number_of_reservations=total_number_of_reservations
)
# edit your profile details
@app.route("/<user>/edit_profile", methods=["POST"])
@login_required
def edit_profile(user):
# you clicked on update details form
if request.method == "POST":
# it was a customer that clicked on their customer page
if user == "customer":
# get what fields the customer must have supplied
new_username = request.form.get("username")
new_email = request.form.get("email")
# update database depending on which fields you supplied
if new_username:
query_db(
"UPDATE users \
SET username = ? \
WHERE id = ?",
(
new_username,
session["user_id"]
)
)
if new_email:
query_db(
"UPDATE users \
SET email = ? \
WHERE id = ?",
(
new_email,
session["user_id"]
)
)
return redirect(url_for("profile", user=user))
# salon that clicked the update
else:
# get what fields the salon supplied
new_salon_name = request.form.get("salon_name")
new_email = request.form.get("email")
new_barber_number = request.form.get("barber_number")
new_open_time = request.form.get("open_time")
new_close_time = request.form.get("close_time")
# update the database depending on the fields the salon supplied
if new_salon_name:
query_db(
"UPDATE users \
SET username = ? \
WHERE id = ?",
(
new_salon_name,
session["user_id"]
)
)
if new_email:
query_db(
"UPDATE users \
SET email = ? \
WHERE id = ?",
(
new_email,
session["user_id"]
)
)
if new_barber_number:
try:
new_barber_number = int(new_barber_number)
except ValueError:
abort(400, "Your barber number must be an integer")
if new_barber_number <= 0:
abort(400, "Barber Number cannot be zero.")
query_db(
"UPDATE salons \
SET barber_number = ? \
WHERE salon_id = ?",
(
new_barber_number,
session["user_id"]
)
)
if new_open_time:
if not verify_registration_times(new_open_time):
abort(400, description="Open Time does not match required pattern")
query_db(
"UPDATE salons \
SET open_time = ? \
WHERE salon_id = ?",
(
new_open_time,
session["user_id"]
)
)
if new_close_time:
if not verify_registration_times(new_close_time):
abort(400, description="Close Time does not match required pattern")
query_db(
"UPDATE salons \
SET close_time = ? \
WHERE salon_id = ?",
(
new_close_time,
session["user_id"]
)
)
return redirect(url_for("profile", user=user))
# if one way or the other, a user got to this url through the get method: should not even be possible
abort(404, description="No get method for edit_profile")
# delete your trimtime account
@app.route("/<user>/delete_account", methods=["POST"])
@login_required
def delete_account(user):
# user clicked on the delete account form
if request.method == "POST":
# the user was a customer
if user == "customer":
query_db(
"DELETE FROM users \
WHERE id = ?",
(session["user_id"],)
)
# the user was a salon
else:
query_db(
"DELETE FROM users \
WHERE id = ?",
(session["user_id"],)
)
query_db(
"DELETE FROM salons \
WHERE salon_id = ?",
(session["user_id"],)
)
return redirect(url_for("landing"))
# user gets to this url through the get method: should not be possible
abort(400, description="There is no GET method for delete_account")
# login page - login as whichever user you picked in landing page
@app.route("/login/<user>", methods=["GET", "POST"])
def login(user):
"""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 user == "customer":
if not request.form.get("username"):
abort(400, description="Invalid username")
else:
if not request.form.get("salon_name"):
abort(400, description="Invalid Salon name")
# Ensure email and password was submitted
if not request.form.get("password") or not request.form.get("email"):
abort(400, description="Missing fields")
# Query database for username
if user == "customer":
rows = query_db(
"SELECT * \
FROM users \
WHERE username = ?",
(request.form.get("username"),)
)
else:
rows = query_db(
"SELECT * \
FROM users \
WHERE username = ?",
(request.form.get("salon_name"),)
)
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(
rows[0]["password_hash"], request.form.get("password")
):
abort(400, description="Invalid password")
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
session["user_type"] = user
# Redirect user to home page
return redirect(url_for('index', user=user))
else:
return render_template("login.html", user=user)
# log user out
@app.route("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
# register page - register as whichever you picked in landing page
@app.route("/register/<user>", methods=["GET", "POST"])
def register(user):
if request.method == "POST":
is_salon = "False"
# the user is a customer
if user == "customer":
username = request.form.get("username")
# Common fields
email = request.form.get("email")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
# Basic validation depending on which you are
if not username or not email or not password or not confirmation:
abort(400, description="Missing fields")
# Email validation
# regex gotten from chatgpt
if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
abort(400, description="Invalid email")
# Password validation
if password != confirmation:
abort(400, description="Passwords do not match")
# Generate password hash
password_hash = generate_password_hash(password, method="scrypt", salt_length=16)
# verify if that username already exists
usernames = [user["username"] for user in query_db(
"SELECT * \
FROM users"
)]
if username in usernames:
abort(400, description="Username already exists")
# Add to database
query_db(
"INSERT INTO users \
(username, email, password_hash, is_salon) \
VALUES(?, ?, ?, ?)",
(
username,
email,
password_hash,
is_salon
)
)
else:
# get form fields
salon_name = request.form.get("salon_name")
is_salon = "True"
email = request.form.get("email")
barber_number = request.form.get("barber_number")
open_time = request.form.get("open_time")
close_time = request.form.get("close_time")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
# verify if any of the fields is missing
if not salon_name or not email or not barber_number or not open_time or not close_time or not password or not confirmation:
abort(400, description="Missing Fields")
# verify if barber number supplied is an integer
try:
barber_number = int(barber_number)
except ValueError:
abort(400, "Your barber number must be an integer")
# verify if barber number is less than 1
if barber_number < 1:
abort(400, description="Your Barber Number must be more than zero")
# verify if registration times follow correct format
if not verify_registration_times(open_time) or not verify_registration_times(close_time):
abort(400, description="Open or Close Times do not match required pattern")
# verify if password and confirmation passwords match
if password != confirmation:
abort(400, description="Passwords do not match")
# Generate password hash
password_hash = generate_password_hash(password, method="scrypt", salt_length=16)
# verify if that username already exists
usernames = [user["username"] for user in query_db(
"SELECT * \
FROM users"
)]
if salon_name in usernames:
abort(400, description="Username already exists")
# Add to database
salon_id = query_db(
"INSERT INTO users \
(username, email, password_hash, is_salon) \
VALUES(?, ?, ?, ?)",
(
salon_name,
email,
password_hash,
is_salon
)
)
# verify if that username already exists
salon_names = [salon["salon_name"] for salon in query_db(
"SELECT * \
FROM salons"
)]
if salon_name in salon_names:
abort(400, description="Salon name already exists")
# add into salons if that salon id doesnt already exist
query_db(
"INSERT INTO salons \
(salon_id, salon_name, barber_number, open_time, close_time) \
VALUES(?, ?, ?, ?, ?)",
(
salon_id,
salon_name,
barber_number,
open_time,
close_time
)
)
# Redirect to login page
return redirect(url_for('login', user=user))
else:
return render_template("register.html", user=user)
# reservation page - for customer
@app.route("/customer/reservation/<salon_name>", methods=["GET", "POST"])
@login_required
def reservation(salon_name):
# you've clicked on the reserve button
if request.method == "POST":
haircut = request.form.get("haircut")
date = request.form.get("date") # year - month - date
# validate if inputs are given
if not date:
abort(400, description="Missing date")
# either a user picks a haircut from options or decides to type one into the text field (specialized)
if haircut == "specialized":
text = request.form.get("specialized_description")
estimated_time = request.form.get("estimated_time")
# validate if user gave an input
if not text or not estimated_time:
abort(400, "Missing fields")
# validate if estimated time given by user is an integer
try:
estimated_time = int(estimated_time)
except ValueError:
abort(400, "Your estimated time must be an integer (in minutes)")
close_time = query_db(
"SELECT close_time \
FROM salons \
WHERE salon_name = ?",
(salon_name,)
)[0]["close_time"]
# check if its thirty minutes to close time; you should work with the next day then
if check_thirty_minutes(close_time) and verify_date(date):
date = get_next_day(date)
# reservation times for the date you're working with
reservation_time_start, reservation_time_end = get_reservation_times(salon_name, date, estimated_time)
# ensure you've not made a reservation before
reservations = query_db(
"SELECT * \
FROM reservations \
WHERE user_id = ? \
AND status = ?",
(
session["user_id"],
"Pending"
)
)
if reservations:
abort(400, "Error in retrieving reservation: Multiple reservations.")
# get id of salon you are making a reservation with
salon_id = query_db(
"SELECT salon_id \
FROM salons \
WHERE salon_name = ?",
(salon_name,)
)[0]["salon_id"]
# insert reservation into database
query_db(
"INSERT INTO reservations \
(\
user_id, \
salon_id, \
specialized_description, \
estimated_time, \
reservation_date, \
reservation_time_start, \
reservation_time_end, \
status \
) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(
session["user_id"],
salon_id,
text,
estimated_time,
date,
reservation_time_start,
reservation_time_end,
"Pending"
)
)
return redirect(url_for("reserved", salon_name=salon_name))
# haircut is not specialized
else:
# haircut id gotten from haircut picked in form
try:
haircut_id = int(request.form.get("haircut"))
except ValueError:
abort(400, "Haircut does not exist")
# validate if inputs are given
if not haircut_id:
abort(400, description="Missing Haircut")
# check if haircut picked is in database of haircuts allowed for that salon
haircut_ids = [haircut["id"] for haircut in query_db(
"SELECT * \
FROM haircuts \
WHERE salon_id = \
(\
SELECT salon_id \
FROM salons \
WHERE salon_name = ?\
)",
(salon_name,)
)]
if haircut_id not in haircut_ids:
abort(400, description="Haircut does not exist")
# get parameters needed to add to reservations table
salon_id = query_db(
"SELECT salon_id \
FROM salons \
WHERE salon_name = ?",
(salon_name,)
)[0]["salon_id"]
estimated_time = query_db(
"SELECT estimated_time \
FROM haircuts \
WHERE id = ?",
(haircut_id,)
)[0]["estimated_time"]
close_time = query_db(
"SELECT close_time \
FROM salons \
WHERE salon_name = ?",
(salon_name,)
)[0]["close_time"]
# if its thirty minutes to closing time, you need to work with the next day
if check_thirty_minutes(close_time) and verify_date(date):
date = get_next_day(date)
# get reservation times
reservation_time_start, reservation_time_end = get_reservation_times(salon_name, date, estimated_time)
# check if you've made a reservation before
reservations = query_db(
"SELECT * \
FROM reservations \
WHERE user_id = ? \
AND status = ?",
(session["user_id"],
"Pending",)
)
if reservations:
abort(400, "Error in retrieving reservation: Multiple reservations.")
# add to reservations table
query_db(
"INSERT INTO reservations \
(\
user_id, \
salon_id, \
haircut_id, \
estimated_time, \
reservation_date, \
reservation_time_start, \
reservation_time_end, \
status \
) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(
session["user_id"],
salon_id,
haircut_id,
estimated_time,
date,
reservation_time_start,
reservation_time_end,
"Pending"
)
)
return redirect(url_for("reserved", salon_name=salon_name))
# you want to make a reservation
else:
haircuts = query_db(
"SELECT * \
FROM haircuts \
WHERE salon_id = \
(\
SELECT salon_id \
FROM salons \
WHERE salon_name = ?\
) \
ORDER BY estimated_time",
(salon_name,)
)
# get open and close times for the salon you want to make a reservation with
open_time = query_db(
"SELECT open_time \
FROM salons \
WHERE salon_name = ?",
(salon_name,)
)[0]["open_time"]
close_time = query_db(
"SELECT close_time \
FROM salons \
WHERE salon_name = ?",
(salon_name,)
)[0]["close_time"]
# get the current date
today_date = str(get_current_date())
# get available reservation times for that date and the next two days
endtime_last_allocated_today = get_endtime_last_allocated(today_date, salon_name)
available_time_today = get_available_times(endtime_last_allocated_today, open_time, today_date)
next_day = get_next_day(str(today_date))
endtime_last_allocated_tomorrow = get_endtime_last_allocated(next_day, salon_name)
available_time_tomorrow = get_available_times(endtime_last_allocated_tomorrow, open_time, next_day)
next_two_days = get_next_day(get_next_day(str(today_date)))
endtime_last_allocated_next_tomorrow = get_endtime_last_allocated(next_two_days, salon_name)
available_time_next_tomorrow = get_available_times(endtime_last_allocated_next_tomorrow, open_time, next_two_days)
# check if time is thirty minutes to closing time
is_closing_time = check_thirty_minutes(close_time)
return render_template(
"reservation.html",
haircuts=haircuts,
salon_name=salon_name,
open_time=open_time,
close_time=close_time,
available_time_today=available_time_today,
available_time_tomorrow=available_time_tomorrow,
available_time_next_tomorrow=available_time_next_tomorrow,
is_closing_time=is_closing_time
)
# page for when you just made a reservation: list reservation details
@app.route("/<salon_name>/reserved")
@login_required
def reserved(salon_name):
# get your pending reservation
reservations = query_db(
"SELECT * \
FROM reservations \
WHERE user_id = ? \
AND status = ?",
(
session["user_id"],
"Pending"
)
)
# redirect you to index page if you dont have a reservation pending
if not reservations:
return redirect(url_for("index", user=session["user_type"]))
# get reservation details for that salon
for reservation in reservations:
haircut_id = reservation["haircut_id"]
haircut = reservation["specialized_description"]
estimated_time = reservation["estimated_time"]
reservation_time_start = reservation["reservation_time_start"]
reservation_time_end = reservation["reservation_time_end"]
reservation_date = reservation["reservation_date"]
# haircut is originally set to specialized description, set it to an haircut in haircuts table if not specialized haircut
if not haircut:
haircut = query_db(
"SELECT name \
FROM haircuts \
WHERE id = ?",
(haircut_id,)
)[0]["name"]
# query reservations table again just to get the total number of reservations made with that salon
reservations = query_db(
"SELECT * \
FROM reservations \
WHERE salon_id = \
(\
SELECT salon_id \
FROM salons \
WHERE salon_name = ?\
) \
AND reservation_date = ? \
AND status = ?",
(
salon_name,
reservation_date,
"Pending"
)
)
total_reservations = len(reservations)
# get reservation id for that reservation you made to autofulfil it after the time elapses
reservation_id = query_db(
"SELECT id\
FROM reservations \
WHERE user_id = ? \
AND status = ?",
(
session["user_id"],
"Pending"
)
)[0]["id"]
# check if reservation time has elapsed
compare_dates = verify_date(reservation_date)
compare_times = is_time_on_or_before(reservation_time_end)
# autofulfil reservation after reservation time elapses: updaate database
is_time = False
if compare_dates and compare_times:
is_time = True
query_db(
"UPDATE reservations \
SET status = ? \
WHERE id = ?",
(
"Fulfilled",
reservation_id
)
)
return redirect(url_for("index", user=session["user_type"]))
return render_template("reserved.html",
salon_name=salon_name,
haircut=haircut,
estimated_time=estimated_time,
reservation_date=reservation_date,
reservation_time_start=reservation_time_start,
reservation_time_end=reservation_time_end,
total_reservations=total_reservations,
reservation_id=reservation_id,
is_time=is_time)
# the reservation you made