1
1
import json
2
- from flask import Flask ,render_template ,request ,redirect ,flash ,url_for
2
+ from flask import Flask , render_template , request , redirect , flash , url_for
3
3
4
4
5
5
app = Flask (__name__ )
6
- app .secret_key = 'something_special'
7
- app .config ['CLUBS_FILE' ] = 'clubs.json'
6
+ app .secret_key = "something_special"
7
+ app .config ["CLUBS_FILE" ] = "clubs.json"
8
+
8
9
9
10
def loadClubs ():
10
- with open (app .config [' CLUBS_FILE' ]) as c :
11
- listOfClubs = json .load (c )[' clubs' ]
11
+ with open (app .config [" CLUBS_FILE" ]) as c :
12
+ listOfClubs = json .load (c )[" clubs" ]
12
13
return listOfClubs
13
14
14
15
15
-
16
16
def loadCompetitions ():
17
- with open (' competitions.json' ) as comps :
18
- listOfCompetitions = json .load (comps )[' competitions' ]
19
- return listOfCompetitions
17
+ with open (" competitions.json" ) as comps :
18
+ listOfCompetitions = json .load (comps )[" competitions" ]
19
+ return listOfCompetitions
20
20
21
21
22
22
competitions = loadCompetitions ()
23
23
clubs = loadClubs ()
24
24
25
25
26
- @app .route ('/' )
26
+ @app .route ("/" )
27
27
def index ():
28
- return render_template ('index.html' )
28
+ return render_template ("index.html" )
29
+
29
30
30
- @app .route (' /showSummary' , methods = [' POST' ])
31
+ @app .route (" /showSummary" , methods = [" POST" ])
31
32
def showSummary ():
32
- club = [club for club in clubs if club [' email' ] == request .form [' email' ]][0 ]
33
- return render_template (' welcome.html' , club = club ,competitions = competitions )
33
+ club = [club for club in clubs if club [" email" ] == request .form [" email" ]][0 ]
34
+ return render_template (" welcome.html" , club = club , competitions = competitions )
34
35
35
36
36
- @app .route (' /book/<competition>/<club>' )
37
- def book (competition ,club ):
38
- foundClub = [c for c in clubs if c [' name' ] == club ][0 ]
39
- foundCompetition = [c for c in competitions if c [' name' ] == competition ][0 ]
37
+ @app .route (" /book/<competition>/<club>" )
38
+ def book (competition , club ):
39
+ foundClub = [c for c in clubs if c [" name" ] == club ][0 ]
40
+ foundCompetition = [c for c in competitions if c [" name" ] == competition ][0 ]
40
41
if foundClub and foundCompetition :
41
- return render_template ('booking.html' ,club = foundClub ,competition = foundCompetition )
42
+ return render_template (
43
+ "booking.html" , club = foundClub , competition = foundCompetition
44
+ )
42
45
else :
43
46
flash ("Something went wrong-please try again" )
44
- return render_template ('welcome.html' , club = club , competitions = competitions )
47
+ return render_template (
48
+ "welcome.html" , club = club , competitions = competitions
49
+ )
45
50
46
51
47
- @app .route (' /purchasePlaces' , methods = [' POST' ])
52
+ @app .route (" /purchasePlaces" , methods = [" POST" ])
48
53
def purchasePlaces ():
49
54
dict_clubs = {}
50
- competition = [c for c in competitions if c ['name' ] == request .form ['competition' ]][0 ]
51
- club , index_club = [(c , clubs .index (c )) for c in clubs if c ['name' ] == request .form ['club' ]][0 ]
52
- placesRequired = int (request .form ['places' ])
53
- competition ['numberOfPlaces' ] = int (competition ['numberOfPlaces' ])- placesRequired
54
- club ['points' ] = int (club ['points' ]) - placesRequired
55
- clubs [index_club ]['points' ] = str (club ['points' ])
55
+ competition = [
56
+ c for c in competitions if c ["name" ] == request .form ["competition" ]
57
+ ][0 ]
58
+ club , index_club = [
59
+ (c , clubs .index (c )) for c in clubs if c ["name" ] == request .form ["club" ]
60
+ ][0 ]
61
+ placesRequired = int (request .form ["places" ])
62
+ competition ["numberOfPlaces" ] = (
63
+ int (competition ["numberOfPlaces" ]) - placesRequired
64
+ )
65
+ club ["points" ] = int (club ["points" ]) - placesRequired
66
+ clubs [index_club ]["points" ] = str (club ["points" ])
56
67
dict_clubs ["clubs" ] = clubs
57
68
try :
58
- with open (app .config [' CLUBS_FILE' ], 'w' ) as file_club :
69
+ with open (app .config [" CLUBS_FILE" ], "w" ) as file_club :
59
70
json .dump (dict_clubs , file_club , indent = 4 )
60
71
except FileNotFoundError :
61
- flash (' File clubs not found' )
62
- return redirect (url_for (' book' ), competition , club )
63
- flash (' Great-booking complete!' )
64
- return render_template (' welcome.html' , club = club , competitions = competitions )
72
+ flash (" File clubs not found" )
73
+ return redirect (url_for (" book" ), competition , club )
74
+ flash (" Great-booking complete!" )
75
+ return render_template (" welcome.html" , club = club , competitions = competitions )
65
76
66
77
67
78
# TODO: Add route for points display
68
79
69
80
70
- @app .route (' /logout' )
81
+ @app .route (" /logout" )
71
82
def logout ():
72
- return redirect (url_for (' index' ))
83
+ return redirect (url_for (" index" ))
0 commit comments