Skip to content

Fix: Persist club points and competition places after booking #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions clubs.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{"clubs":[
{
"name":"Simply Lift",
"email":"[email protected]",
"points":"13"
},
{
"name":"Iron Temple",
"email": "[email protected]",
"points":"4"
},
{ "name":"She Lifts",
"email": "[email protected]",
"points":"12"
}
]}
{
"clubs": [
{
"name": "Simply Lift",
"email": "[email protected]",
"points": "10"
},
{
"name": "Iron Temple",
"email": "[email protected]",
"points": "4"
},
{
"name": "She Lifts",
"email": "[email protected]",
"points": "12"
}
]
}
2 changes: 1 addition & 1 deletion competitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"name": "Summer 2025",
"date": "2025-07-22 13:30:00",
"numberOfPlaces": "10"
"numberOfPlaces": 7
}
]
}
33 changes: 27 additions & 6 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ def loadClubs():
listOfClubs = json.load(c)['clubs']
return listOfClubs


def loadCompetitions():
with open('competitions.json') as comps:
listOfCompetitions = json.load(comps)['competitions']
return listOfCompetitions

def saveClubs(clubs_data):
with open('clubs.json', 'w') as c:
json.dump({"clubs": clubs_data}, c, indent=4)

def saveCompetitions(competitions_data):
with open('competitions.json', 'w') as comps:
json.dump({"competitions": competitions_data}, comps, indent=4)


app = Flask(__name__)
app.secret_key = 'something_special'
Expand All @@ -28,50 +35,64 @@ def index():
@app.route('/showSummary',methods=['POST'])
def showSummary():
found_clubs = [club for club in clubs if club['email'] == request.form['email']]
if found_clubs:

if found_clubs:
club = found_clubs[0]
return render_template('welcome.html',club=club,competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
else:
flash("Sorry, that email was not found.")
return redirect(url_for('index'))


@app.route('/book/<competition>/<club>')
def book(competition,club):
foundClub = [c for c in clubs if c['name'] == club][0]
foundCompetition = [c for c in competitions if c['name'] == competition][0]

competition_date = datetime.strptime(foundCompetition['date'], '%Y-%m-%d %H:%M:%S')
if competition_date < datetime.now():
flash("This competition has already passed. Booking is not allowed.")
return render_template('welcome.html', club=foundClub, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

if foundClub and foundCompetition:
return render_template('booking.html',club=foundClub,competition=foundCompetition)
else:
flash("Something went wrong-please try again")
return render_template('welcome.html', club=foundClub, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))


@app.route('/purchasePlaces',methods=['POST'])
def purchasePlaces():
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
club = [c for c in clubs if c['name'] == request.form['club']][0]
placesRequired = int(request.form['places'])

competition_date = datetime.strptime(competition['date'], '%Y-%m-%d %H:%M:%S')
if competition_date < datetime.now():
flash("Booking for past competitions is not allowed.")
return render_template('welcome.html', club=club, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

if placesRequired > 12:
flash("You cannot book more than 12 places per competition.")
return render_template('welcome.html', club=club, competitions=competitions, current_date_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

if int(club['points']) >= placesRequired:
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
club['points'] = str(int(club['points']) - placesRequired)
flash('Great-booking complete!')
if int(competition['numberOfPlaces']) >= placesRequired:
competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired
club['points'] = str(int(club['points']) - placesRequired)

saveClubs(clubs)
saveCompetitions(competitions)

flash('Great-booking complete!')
else:
flash(f"Not enough places available in this competition. Only {competition['numberOfPlaces']} places left.")
else:
flash(f"You do not have enough points to book {placesRequired} places. You currently have {club['points']} points.")

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



# TODO: Add route for points display


Expand Down