1
1
import json
2
2
import datetime
3
- from flask import Flask ,render_template ,request ,redirect ,flash ,url_for , abort
3
+ from flask import Flask , render_template , request , redirect , flash , url_for
4
4
5
5
6
6
# ============================================ FLASK APP INITIALIZATION
7
7
8
8
app = Flask (__name__ )
9
9
app .secret_key = 'something_special'
10
10
11
+
11
12
def page_not_found (e ):
12
13
return render_template ('404.html' ), 404
13
14
15
+
14
16
app .register_error_handler (404 , page_not_found )
15
17
16
18
@@ -20,7 +22,7 @@ def page_not_found(e):
20
22
PATH_JSON_CLUBS = 'clubs.json'
21
23
PATH_JSON_COMPETITIONS = 'competitions.json'
22
24
23
- # ============================================ LOAD JSON DATA
25
+ # ============================================ LOAD JSON DATA
24
26
25
27
26
28
def loadClubs ():
@@ -30,7 +32,7 @@ def loadClubs():
30
32
listOfClubs = json .load (c )['clubs' ]
31
33
if listOfClubs and len (listOfClubs ) > 0 :
32
34
return listOfClubs
33
- else :
35
+ else :
34
36
raise Exception ("This file is empty, or key not found" )
35
37
36
38
except Exception as error :
@@ -42,14 +44,16 @@ def loadCompetitions():
42
44
if PATH_JSON_COMPETITIONS :
43
45
with open (PATH_JSON_COMPETITIONS ) as c :
44
46
listOfCompetitions = json .load (c )['competitions' ]
47
+ listOfCompetitions .sort (key = lambda x : x ["date" ], reverse = True )
45
48
if listOfCompetitions and len (listOfCompetitions ) > 0 :
46
49
return listOfCompetitions
47
- else :
50
+ else :
48
51
raise Exception ("This file is empty, or key not found" )
49
52
50
53
except Exception as error :
51
54
return str (error )
52
55
56
+
53
57
competitions = loadCompetitions ()
54
58
clubs = loadClubs ()
55
59
now = datetime .datetime .now ()
@@ -86,7 +90,7 @@ def show_summary():
86
90
"""
87
91
try :
88
92
club = [club for club in clubs if club ['email' ] == request .form ['email' ]][0 ]
89
-
93
+
90
94
if club :
91
95
return render_template ('welcome.html' , club = club , competitions = competitions , current_date = current_date )
92
96
@@ -99,7 +103,7 @@ def show_summary():
99
103
100
104
101
105
@app .route ('/book/<competition>/<club>' )
102
- def book (competition ,club ):
106
+ def book (competition , club ):
103
107
"""
104
108
Displays :
105
109
Competition's name and places available
@@ -108,7 +112,7 @@ def book(competition,club):
108
112
try :
109
113
foundClub = [c for c in clubs if c ['name' ] == club ][0 ]
110
114
foundCompetition = [c for c in competitions if c ['name' ] == competition ][0 ]
111
- return render_template ('booking.html' ,club = foundClub ,competition = foundCompetition )
115
+ return render_template ('booking.html' , club = foundClub , competition = foundCompetition )
112
116
113
117
except IndexError :
114
118
flash ("Sorry, this club or competition wasn't found !!" )
@@ -123,7 +127,7 @@ def book(competition,club):
123
127
}
124
128
125
129
126
- @app .route ('/purchasePlaces' ,methods = ['POST' ])
130
+ @app .route ('/purchasePlaces' , methods = ['POST' ])
127
131
@app .errorhandler (400 )
128
132
def purchase_places ():
129
133
"""
@@ -146,13 +150,13 @@ def purchase_places():
146
150
elif placesRequired > club_points :
147
151
flash ("Sorry, your club doesn't have enough points !" )
148
152
return render_template ('booking.html' , club = club , competition = competition , current_date = current_date ), 400
149
-
150
- elif placesRequired > MAX_PLACES :
153
+
154
+ elif placesRequired > MAX_PLACES :
151
155
flash (f"Sorry, you can't book more than { MAX_PLACES } places !" )
152
156
return render_template ('booking.html' , club = club , competition = competition , current_date = current_date ), 400
153
-
154
- elif placesRequired + current_cart > MAX_PLACES :
155
- flash (f"""Sorry, you have already booked places, for this competition
157
+
158
+ elif placesRequired + current_cart > MAX_PLACES :
159
+ flash (f"""Sorry, you have already booked places, for this competition
156
160
and now you have exceeded the limit of { MAX_PLACES } places !""" )
157
161
return render_template ('booking.html' , club = club , competition = competition , current_date = current_date ), 400
158
162
@@ -163,13 +167,16 @@ def purchase_places():
163
167
else :
164
168
competition ['numberOfPlaces' ] = int (competition ['numberOfPlaces' ])- placesRequired
165
169
166
- club ['points' ] = int (club ['points' ])- placesRequired
170
+ club ['points' ] = int (club ['points' ]) - placesRequired
167
171
168
172
cart [competition ["name" ]][club ["name" ]] += placesRequired
169
173
170
174
flash (f"Great you have booked { placesRequired } places! for { competition ['name' ]} " )
171
175
172
- return render_template ('welcome.html' , club = club , competitions = competitions , current_date = current_date , add_to_cart = cart [competition ["name" ]][club ["name" ]])
176
+ return render_template ('welcome.html' ,
177
+ club = club , competitions = competitions ,
178
+ current_date = current_date ,
179
+ add_to_cart = cart [competition ["name" ]][club ["name" ]])
173
180
174
181
# ================================================= ROUTE FOR BOARD DISPLAY
175
182
0 commit comments