Skip to content

Bug/prevent overbooking places #301

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ lib
.Python
tests/
.envrc
__pycache__
__pycache__
venv/
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": "6"
},
{
"name": "Iron Temple",
"email": "[email protected]",
"points": "1"
},
{
"name": "She Lifts",
"email": "[email protected]",
"points": "12"
}
]
}
5 changes: 5 additions & 0 deletions competitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"name": "Fall Classic",
"date": "2020-10-22 13:30:00",
"numberOfPlaces": "13"
},
{
"name": "Summer 2025",
"date": "2025-07-22 13:30:00",
"numberOfPlaces": 0
}
]
}
128 changes: 111 additions & 17 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,153 @@
import json
from flask import Flask,render_template,request,redirect,flash,url_for
from flask import Flask,render_template,request,redirect,flash,url_for,session
from datetime import datetime


def loadClubs():
with open('clubs.json') as c:
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'

competitions = loadCompetitions()
clubs = loadClubs()


@app.route('/')
def index():
if 'club_email' in session:
club_email = session['club_email']
found_clubs = [c for c in clubs if c['email'] == club_email]
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:
session.pop('club_email', None)
flash("Your club's email was not found. Please log in again.")
return render_template('index.html')
return render_template('index.html')


@app.route('/showSummary',methods=['POST'])
def showSummary():
club = [club for club in clubs if club['email'] == request.form['email']][0]
return render_template('welcome.html',club=club,competitions=competitions)


@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]
user_email = request.form['email']
found_clubs = [club for club in clubs if club['email'] == user_email]

if found_clubs:
club = found_clubs[0]
session['club_email'] = club['email']
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.")
session.pop('club_email', None)
return redirect(url_for('index'))


@app.route('/book/<competition_name>/<club_name>')
def book(competition_name,club_name):
if 'club_email' not in session:
flash("You need to be logged in to book places.")
return redirect(url_for('index'))

logged_in_club_email = session['club_email']
foundClub = [c for c in clubs if c['email'] == logged_in_club_email][0]

foundCompetition = [c for c in competitions if c['name'] == competition_name][0]

if foundClub['name'] != club_name:
flash("Attempted to book for a different club. Action blocked.")
return redirect(url_for('index'))

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 redirect(url_for('index'))

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=club, competitions=competitions)
return redirect(url_for('index'))


@app.route('/purchasePlaces',methods=['POST'])
def purchasePlaces():
if 'club_email' not in session:
flash("You need to be logged in to purchase places.")
return redirect(url_for('index'))

logged_in_club_email = session['club_email']
club = [c for c in clubs if c['email'] == logged_in_club_email][0]

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['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)

if club['name'] != request.form['club']:
flash("Attempted to purchase for a different club. Action blocked.")
return redirect(url_for('index'))

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 redirect(url_for('index'))

if placesRequired > 12:
flash("You cannot book more than 12 places per competition.")
return redirect(url_for('index'))

# --- BUG Fix: Prevent overbooking and incorrect point deduction ---
# Check if club has enough points AND if competition has enough places
if int(club['points']) >= placesRequired:
if int(competition['numberOfPlaces']) >= placesRequired:
# ONLY proceed with updates if both conditions are met
competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired
club['points'] = str(int(club['points']) - placesRequired)

saveClubs(clubs)
saveCompetitions(competitions)

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

return redirect(url_for('index'))


# TODO: Add route for points display
@app.route('/pointsDisplay')
def pointsDisplay():
if 'club_email' not in session:
flash("You need to be logged in to view club points.")
return redirect(url_for('index'))

sorted_clubs = sorted(clubs, key=lambda c: int(c['points']), reverse=True)
return render_template('points.html', clubs=sorted_clubs)


@app.route('/logout')
def logout():
return redirect(url_for('index'))
session.pop('club_email', None)
flash("You have been logged out.")
return redirect(url_for('index'))
12 changes: 12 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
</head>
<body>
<h1>Welcome to the GUDLFT Registration Portal!</h1>

{# Add this block to display flashed messages #}
{% with messages = get_flashed_messages()%}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}
{%endwith%}

Please enter your secretary email to continue:
<form action="showSummary" method="post">
<label for="email">Email:</label>
Expand Down
33 changes: 33 additions & 0 deletions templates/points.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Points Display Board | GUDLFT Registration</title>
</head>
<body>
<h2>Club Points Leaderboard</h2>
<p><a href="{{url_for('index')}}">Back to Dashboard</a></p>

{% if clubs %}
<table border="1" style="width:50%; text-align: left;">
<thead>
<tr>
<th>Club Name</th>
<th>Points</th>
</tr>
</thead>
<tbody>
{% for club_item in clubs %}
<tr>
<td>{{ club_item['name'] }}</td>
<td>{{ club_item['points'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No clubs found to display points.</p>
{% endif %}

</body>
</html>
18 changes: 14 additions & 4 deletions templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<title>Summary | GUDLFT Registration</title>
</head>
<body>
<h2>Welcome, {{club['email']}} </h2><a href="{{url_for('logout')}}">Logout</a>
<h2>Welcome, {{club['email']}} </h2>
<a href="{{url_for('logout')}}">Logout</a> |
<a href="{{url_for('pointsDisplay')}}">View All Club Points</a>

{% with messages = get_flashed_messages()%}
{% if messages %}
Expand All @@ -23,9 +25,17 @@ <h3>Competitions:</h3>
{{comp['name']}}<br />
Date: {{comp['date']}}</br>
Number of Places: {{comp['numberOfPlaces']}}
{%if comp['numberOfPlaces']|int >0%}
<a href="{{ url_for('book',competition=comp['name'],club=club['name']) }}">Book Places</a>
{%endif%}
{# Compare competition date string with current_date_str #}
{% if comp['date'] > current_date_str and comp['numberOfPlaces']|int > 0 %}
{# --- CHANGE THIS LINE --- #}
<a href="{{ url_for('book',competition_name=comp['name'],club_name=club['name']) }}">Book Places</a>
{% else %}
{% if comp['date'] <= current_date_str %}
<span style="color: gray;">(Competition passed)</span>
{% else %}
<span style="color: gray;">(No places left)</span>
{% endif %}
{% endif %}
</li>
<hr />
{% endfor %}
Expand Down