Skip to content

Commit

Permalink
add ui form contraints + test issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeammet committed Dec 10, 2021
1 parent 1fe6a15 commit 286e0d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
38 changes: 26 additions & 12 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@


def loadClubs(clubs_json):
with open('clubs.json') as c:
listOfClubs = json.load(c)['clubs']
return listOfClubs
with open(clubs_json) as c:
listOfClubs = json.load(c)['clubs']
return listOfClubs


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


def create_app(config):
def create_app(config={}):
app = Flask(__name__)
app.config.update(config)
app.secret_key = 'something_special'
app.config.from_object(config)

competitions = loadCompetitions('competitions.json')
clubs = loadClubs('clubs.json')
competitions_json = 'competitions.json'
clubs_json = 'clubs.json'
if app.config["TESTING"] == True:
competitions_json = 'tests/test_dataset.json'
clubs_json = 'tests/test_dataset.json'

competitions = loadCompetitions(competitions_json)
clubs = loadClubs(clubs_json)

@app.route('/')
def index():
Expand Down Expand Up @@ -48,10 +54,18 @@ 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['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)

placesRemaining = int(competition['numberOfPlaces'])
if placesRequired > placesRemaining:
flash(f"Cannot book more places than remaining ({placesRemaining}).")
return render_template('booking.html',club=club, competition=competition)
elif placesRequired > int(club['points']):
flash(f"Cannot book more places than points you have ({club['points']}).")
return render_template('booking.html',club=club, competition=competition)
else:
flash('Great-booking complete!')
club['points'] = int(club['points']) - placesRequired
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
return render_template('welcome.html', club=club, competitions=competitions)

# TODO: Add route for points display

Expand Down
14 changes: 12 additions & 2 deletions templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
</head>
<body>
<h2>{{competition['name']}}</h2>
Places available: {{competition['numberOfPlaces']}}
{% with messages = get_flashed_messages()%}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}
{% endwith %}
<p>Places available: {{competition['numberOfPlaces']}}</p>
<p>Club points: {{club['points']}}</p>
<form action="/purchasePlaces" method="post">
<input type="hidden" name="club" value="{{club['name']}}">
<input type="hidden" name="competition" value="{{competition['name']}}">
<label for="places">How many places?</label><input type="number" name="places" id=""/>
<label for="places">How many places?</label><input type="number" name="places" id="" max="{{club['points']}}" min="1"/>
<button type="submit">Book</button>
</form>
</body>
Expand Down

0 comments on commit 286e0d3

Please sign in to comment.