33from lib .database_connection import get_flask_database_connection
44from lib .gig_repository import GigRepository
55from lib .booking_repository import BookingRepository
6+ from flask_login import (
7+ LoginManager ,
8+ UserMixin ,
9+ current_user ,
10+ login_required ,
11+ login_user ,
12+ logout_user
13+ )
14+ from werkzeug .security import generate_password_hash , check_password_hash
15+ from functools import wraps
16+ import datetime
617
718app = Flask (__name__ )
19+ app .config .update (
20+ SECRET_KEY = "adobgaiodbgaidgbiodgb" ,
21+ )
22+
23+ login_manager = LoginManager ()
24+ login_manager .init_app (app )
25+
26+ class User (UserMixin ):
27+ def signed_up (self , connection , username ):
28+ rows = connection .execute ('SELECT * FROM users WHERE username = %s' , [username ])
29+ return len (rows ) > 0
30+ def password_valid (self , connection , username , password_attempt ):
31+ if not self .signed_up (connection , username ):
32+ return False
33+ rows = connection .execute ('SELECT * FROM users WHERE username = %s' , [username ])
34+ return check_password_hash (rows [0 ]["hashed_password" ], password_attempt ) != username
35+ def get_user_database_id (self , connection , username ):
36+ rows = connection .execute ('SELECT * FROM users WHERE username = %s' , [username ])
37+ return rows [0 ]["id" ]
38+
39+ @login_manager .user_loader
40+ def user_loader (username : str ):
41+ connection = get_flask_database_connection (app )
42+ if User ().signed_up (connection , username ):
43+ user_model = User ()
44+ user_model .id = username
45+ return user_model
46+ return None
47+
48+
849
950@app .route ('/home' , methods = ['GET' ])
1051def get_home ():
@@ -14,30 +55,82 @@ def get_home():
1455def get_about ():
1556 return render_template ('about.html' )
1657
17- @app .route ('/gigs' , methods = ['GET' ])
58+ @app .route ('/gigs' , methods = ['GET' , 'POST' ])
1859def get_gigs ():
1960 connection = get_flask_database_connection (app )
20- repo = BookingRepository (connection )
21- gig_booked_ids = [booking .gig_id for booking in repo .get_bookings ()]
2261 repo = GigRepository (connection )
23- gigs = repo .all ()
24- gig_ids = [gig .id for gig in gigs ]
25- booked_percentage = int (len (set (gig_booked_ids )) / len (set (gig_ids )) * 100 )
26- return render_template ('gigs.html' , gigs = gigs , number_bookings = booked_percentage )
62+ locations = ["All" ]
63+ for gig in repo .all ():
64+ if gig .location not in locations :
65+ locations .append (gig .location )
66+ selected_location = "All"
67+ if "location" in request .form .keys ():
68+ selected_location = request .form ["location" ]
69+ date_from = "1900-01-01"
70+ if "date_from" in request .form .keys ():
71+ date_from = request .form ["date_from" ]
72+ date_to = "3000-01-01"
73+ if "date_to" in request .form .keys ():
74+ date_to = request .form ["date_to" ]
75+ gigs = repo .get_by_location_and_dates (selected_location , date_from , date_to )
76+ return render_template ('gigs.html' , gigs = gigs , locations = locations , selected_location = selected_location , date_from = date_from , date_to = date_to )
77+
78+ @app .route ('/gigs/<id>' , methods = ['GET' ])
79+ def get_gig_by_id (id ):
80+ connection = get_flask_database_connection (app )
81+ repo = GigRepository (connection )
82+ gig = repo .get_by_id (id )
83+ logged_in_as = str (current_user .id ) if current_user .__dict__ .get ("id" ) else None
84+ repo = BookingRepository (connection )
85+ if current_user .__dict__ != {}:
86+ already_booked_gig = gig .id in [booking .gig_id for booking in repo .get_bookings (1 )]
87+ else :
88+ already_booked_gig = False
89+ gig_in_past = gig .datetime < datetime .datetime .now ()
90+ return render_template ('gig.html' , gig = gig , logged_in_as = logged_in_as , already_booked_gig = already_booked_gig , gig_in_past = gig_in_past )
91+
92+ @app .route ("/book_gig/<gig_id>" , methods = ["POST" ])
93+ def post_book_gig (gig_id ):
94+ if int (request .form ["ticket_count" ]) > 8 :
95+ return "A user can't book more than 8 tickets for one gig"
96+ connection = get_flask_database_connection (app )
97+ repo = BookingRepository (connection )
98+ user_database_id = User ().get_user_database_id (connection , current_user .id )
99+ repo .make_booking (gig_id , user_database_id , request .form ["ticket_count" ])
100+ return redirect (url_for ('get_account' ))
101+
102+ @app .route ("/login" , methods = ["POST" ])
103+ def post_login ():
104+ username = request .form ["usernmae" ]
105+ password = request .form ["password" ]
106+ connection = get_flask_database_connection (app )
107+
108+ if User ().signed_up (connection , username ):
109+ if User ().password_valid (connection , username , password ):
110+ user_model = User ()
111+ user_model .id = username
112+ login_user (user_model )
113+ return redirect (url_for ('get_home' ))
114+ else :
115+ return "Wrong credentials"
116+ return "Unknown user"
27117
28118@app .route ('/login' , methods = ['GET' ])
29119def get_login ():
30120 return render_template ('login.html' )
31121
32122@app .route ('/logout' , methods = ['GET' ])
33123def get_logout ():
124+ logout_user ()
34125 return render_template ('logout.html' )
35126
36127@app .route ('/account' , methods = ['GET' ])
128+ @login_required
37129def get_account ():
38130 connection = get_flask_database_connection (app )
39131 repo = BookingRepository (connection )
40- bookings = repo .get_bookings ()
132+ user_database_id = User ().get_user_database_id (connection , current_user .id )
133+ bookings = repo .get_bookings (user_database_id )
41134 repo = GigRepository (connection )
42135 gigs = repo .all ()
43136 booking_details = []
@@ -49,9 +142,5 @@ def get_account():
49142 })
50143 return render_template ('account.html' , booking_details = booking_details )
51144
52- @app .route ('/tcs' , methods = ['GET' ])
53- def get_tcs ():
54- return render_template ('tcs.html' )
55-
56145if __name__ == '__main__' :
57146 app .run (debug = True , port = int (os .environ .get ('PORT' , 5001 )))
0 commit comments