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