11import 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
33
44
55def 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
99
1010
1111def 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
1515
1616
1717app = Flask (__name__ )
18- app .secret_key = ' something_special'
18+ app .secret_key = " something_special"
1919
2020competitions = loadCompetitions ()
2121clubs = loadClubs ()
2222
23- @app .route ('/' )
23+
24+ @app .route ("/" )
2425def index ():
25- return render_template ('index.html' )
26+ return render_template ("index.html" )
27+
28+
29+ def get_club_from_email (email ):
30+ try :
31+ club = [
32+ club for club in clubs if club ["email" ] == email
33+ ][0 ]
34+ return club
35+ except IndexError :
36+ return None
2637
27- @app .route ('/showSummary' ,methods = ['POST' ])
38+
39+ @app .route ("/showSummary" , methods = ["POST" ])
2840def 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 )
41+ club = get_club_from_email (request .form ["email" ])
42+ if club :
43+ return render_template ("welcome.html" , club = club ,
44+ competitions = competitions )
45+ else :
46+ flash ("Sorry, that email wasn't found." )
47+ return redirect (url_for ("index" ))
3148
3249
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 ]
50+ @app .route (" /book/<competition>/<club>" )
51+ def book (competition , club ):
52+ foundClub = [c for c in clubs if c [" name" ] == club ][0 ]
53+ foundCompetition = [c for c in competitions if c [" name" ] == competition ][0 ]
3754 if foundClub and foundCompetition :
38- return render_template ('booking.html' ,club = foundClub ,competition = foundCompetition )
55+ return render_template (
56+ "booking.html" , club = foundClub , competition = foundCompetition
57+ )
3958 else :
4059 flash ("Something went wrong-please try again" )
41- return render_template ('welcome.html' , club = club , competitions = competitions )
60+ return render_template (
61+ "welcome.html" , club = club , competitions = competitions
62+ )
4263
4364
44- @app .route (' /purchasePlaces' , methods = [' POST' ])
65+ @app .route (" /purchasePlaces" , methods = [" POST" ])
4566def 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- placesRequired = int (request .form ['places' ])
49- competition ['numberOfPlaces' ] = int (competition ['numberOfPlaces' ])- placesRequired
50- flash ('Great-booking complete!' )
51- return render_template ('welcome.html' , club = club , competitions = competitions )
67+ competition = [
68+ c for c in competitions if c ["name" ] == request .form ["competition" ]
69+ ][0 ]
70+ club = [c for c in clubs if c ["name" ] == request .form ["club" ]][0 ]
71+ placesRequired = int (request .form ["places" ])
72+ competition ["numberOfPlaces" ] = (
73+ int (competition ["numberOfPlaces" ]) - placesRequired
74+ )
75+ flash ("Great-booking complete!" )
76+ return render_template ("welcome.html" , club = club , competitions = competitions )
5277
5378
5479# TODO: Add route for points display
5580
5681
57- @app .route (' /logout' )
82+ @app .route (" /logout" )
5883def logout ():
59- return redirect (url_for (' index' ))
84+ return redirect (url_for (" index" ))
0 commit comments