Skip to content

Commit 75ecf05

Browse files
authored
Merge pull request #5 from devogs/bug/persist-points-and-places
Fix: Persist club points and competition places after booking
2 parents d000b12 + 7bba7af commit 75ecf05

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

clubs.json

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
{"clubs":[
2-
{
3-
"name":"Simply Lift",
4-
"email":"[email protected]",
5-
"points":"13"
6-
},
7-
{
8-
"name":"Iron Temple",
9-
"email": "[email protected]",
10-
"points":"4"
11-
},
12-
{ "name":"She Lifts",
13-
"email": "[email protected]",
14-
"points":"12"
15-
}
16-
]}
1+
{
2+
"clubs": [
3+
{
4+
"name": "Simply Lift",
5+
"email": "[email protected]",
6+
"points": "10"
7+
},
8+
{
9+
"name": "Iron Temple",
10+
"email": "[email protected]",
11+
"points": "4"
12+
},
13+
{
14+
"name": "She Lifts",
15+
"email": "[email protected]",
16+
"points": "12"
17+
}
18+
]
19+
}

competitions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"name": "Summer 2025",
1515
"date": "2025-07-22 13:30:00",
16-
"numberOfPlaces": "10"
16+
"numberOfPlaces": 7
1717
}
1818
]
1919
}

server.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ def loadClubs():
88
listOfClubs = json.load(c)['clubs']
99
return listOfClubs
1010

11-
1211
def loadCompetitions():
1312
with open('competitions.json') as comps:
1413
listOfCompetitions = json.load(comps)['competitions']
1514
return listOfCompetitions
1615

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+
1724

1825
app = Flask(__name__)
1926
app.secret_key = 'something_special'
@@ -28,50 +35,64 @@ def index():
2835
@app.route('/showSummary',methods=['POST'])
2936
def showSummary():
3037
found_clubs = [club for club in clubs if club['email'] == request.form['email']]
31-
if found_clubs:
38+
39+
if found_clubs:
3240
club = found_clubs[0]
3341
return render_template('welcome.html',club=club,competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
3442
else:
3543
flash("Sorry, that email was not found.")
3644
return redirect(url_for('index'))
3745

46+
3847
@app.route('/book/<competition>/<club>')
3948
def book(competition,club):
4049
foundClub = [c for c in clubs if c['name'] == club][0]
4150
foundCompetition = [c for c in competitions if c['name'] == competition][0]
51+
4252
competition_date = datetime.strptime(foundCompetition['date'], '%Y-%m-%d %H:%M:%S')
4353
if competition_date < datetime.now():
4454
flash("This competition has already passed. Booking is not allowed.")
4555
return render_template('welcome.html', club=foundClub, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
56+
4657
if foundClub and foundCompetition:
4758
return render_template('booking.html',club=foundClub,competition=foundCompetition)
4859
else:
4960
flash("Something went wrong-please try again")
5061
return render_template('welcome.html', club=foundClub, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
5162

63+
5264
@app.route('/purchasePlaces',methods=['POST'])
5365
def purchasePlaces():
5466
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
5567
club = [c for c in clubs if c['name'] == request.form['club']][0]
5668
placesRequired = int(request.form['places'])
69+
5770
competition_date = datetime.strptime(competition['date'], '%Y-%m-%d %H:%M:%S')
5871
if competition_date < datetime.now():
5972
flash("Booking for past competitions is not allowed.")
6073
return render_template('welcome.html', club=club, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
74+
6175
if placesRequired > 12:
6276
flash("You cannot book more than 12 places per competition.")
6377
return render_template('welcome.html', club=club, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
78+
6479
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.")
6890
else:
6991
flash(f"You do not have enough points to book {placesRequired} places. You currently have {club['points']} points.")
7092

7193
return render_template('welcome.html', club=club, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
7294

7395

74-
7596
# TODO: Add route for points display
7697

7798

0 commit comments

Comments
 (0)