@@ -8,12 +8,19 @@ def loadClubs():
8
8
listOfClubs = json .load (c )['clubs' ]
9
9
return listOfClubs
10
10
11
-
12
11
def loadCompetitions ():
13
12
with open ('competitions.json' ) as comps :
14
13
listOfCompetitions = json .load (comps )['competitions' ]
15
14
return listOfCompetitions
16
15
16
+ def saveClubs (clubs_data ):
17
+ with open ('clubs.json' , 'w' ) as c :
18
+ json .dump ({"clubs" : clubs_data }, c , indent = 4 )
19
+
20
+ def saveCompetitions (competitions_data ):
21
+ with open ('competitions.json' , 'w' ) as comps :
22
+ json .dump ({"competitions" : competitions_data }, comps , indent = 4 )
23
+
17
24
18
25
app = Flask (__name__ )
19
26
app .secret_key = 'something_special'
@@ -28,50 +35,64 @@ def index():
28
35
@app .route ('/showSummary' ,methods = ['POST' ])
29
36
def showSummary ():
30
37
found_clubs = [club for club in clubs if club ['email' ] == request .form ['email' ]]
31
- if found_clubs :
38
+
39
+ if found_clubs :
32
40
club = found_clubs [0 ]
33
41
return render_template ('welcome.html' ,club = club ,competitions = competitions , current_date_str = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
34
42
else :
35
43
flash ("Sorry, that email was not found." )
36
44
return redirect (url_for ('index' ))
37
45
46
+
38
47
@app .route ('/book/<competition>/<club>' )
39
48
def book (competition ,club ):
40
49
foundClub = [c for c in clubs if c ['name' ] == club ][0 ]
41
50
foundCompetition = [c for c in competitions if c ['name' ] == competition ][0 ]
51
+
42
52
competition_date = datetime .strptime (foundCompetition ['date' ], '%Y-%m-%d %H:%M:%S' )
43
53
if competition_date < datetime .now ():
44
54
flash ("This competition has already passed. Booking is not allowed." )
45
55
return render_template ('welcome.html' , club = foundClub , competitions = competitions , current_date_str = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
56
+
46
57
if foundClub and foundCompetition :
47
58
return render_template ('booking.html' ,club = foundClub ,competition = foundCompetition )
48
59
else :
49
60
flash ("Something went wrong-please try again" )
50
61
return render_template ('welcome.html' , club = foundClub , competitions = competitions , current_date_str = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
51
62
63
+
52
64
@app .route ('/purchasePlaces' ,methods = ['POST' ])
53
65
def purchasePlaces ():
54
66
competition = [c for c in competitions if c ['name' ] == request .form ['competition' ]][0 ]
55
67
club = [c for c in clubs if c ['name' ] == request .form ['club' ]][0 ]
56
68
placesRequired = int (request .form ['places' ])
69
+
57
70
competition_date = datetime .strptime (competition ['date' ], '%Y-%m-%d %H:%M:%S' )
58
71
if competition_date < datetime .now ():
59
72
flash ("Booking for past competitions is not allowed." )
60
73
return render_template ('welcome.html' , club = club , competitions = competitions , current_date_str = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
74
+
61
75
if placesRequired > 12 :
62
76
flash ("You cannot book more than 12 places per competition." )
63
77
return render_template ('welcome.html' , club = club , competitions = competitions , current_date_str = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
78
+
64
79
if int (club ['points' ]) >= placesRequired :
65
- competition ['numberOfPlaces' ] = int (competition ['numberOfPlaces' ])- placesRequired
66
- club ['points' ] = str (int (club ['points' ]) - placesRequired )
67
- flash ('Great-booking complete!' )
80
+ if int (competition ['numberOfPlaces' ]) >= placesRequired :
81
+ competition ['numberOfPlaces' ] = int (competition ['numberOfPlaces' ]) - placesRequired
82
+ club ['points' ] = str (int (club ['points' ]) - placesRequired )
83
+
84
+ saveClubs (clubs )
85
+ saveCompetitions (competitions )
86
+
87
+ flash ('Great-booking complete!' )
88
+ else :
89
+ flash (f"Not enough places available in this competition. Only { competition ['numberOfPlaces' ]} places left." )
68
90
else :
69
91
flash (f"You do not have enough points to book { placesRequired } places. You currently have { club ['points' ]} points." )
70
92
71
93
return render_template ('welcome.html' , club = club , competitions = competitions , current_date_str = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' ))
72
94
73
95
74
-
75
96
# TODO: Add route for points display
76
97
77
98
0 commit comments