Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Guyot Bourgeois committed Jun 14, 2022
1 parent 0671ffd commit 51735aa
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
33 changes: 26 additions & 7 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,47 @@ def show_summary():
@app.route('/book/<competition>/<club>')
def book(competition, club):
found_club = [c for c in clubs if c['name'] == club][0]
found_competition = [c for c in competitions if c['name'] == competition][0]
found_competition = [
c for c in competitions if c['name'] == competition
][0]
if found_club and found_competition:
return render_template('booking.html', club=found_club, competition=found_competition)
return render_template(
'booking.html',
club=found_club,
competition=found_competition
)
else:
flash("Something went wrong-please try again")
return render_template('welcome.html', club=club, competitions=competitions)
return render_template(
'welcome.html',
club=club,
competitions=competitions
)


@app.route('/purchase-places', methods=['POST'])
def purchase_places():
competition = [c for c in competitions if c['name'] == request.form['competition']][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]
places_required = int(request.form['places'])
place_allowed = int(club['points']) // PLACE_COST
places_allowed = int(club['points']) // PLACE_COST
current_datetime = f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
if places_required > place_allowed:
if places_required > places_allowed:
flash('You do not have enough points')
return render_template(
'booking.html',
club=club,
competition=competition
)
elif places_required > int(competition['places']):
flash('Not enough places available')
return render_template(
'booking.html',
club=club,
competition=competition
)
elif current_datetime > competition['date']:
flash('Competition over')
return render_template(
Expand All @@ -90,7 +109,7 @@ def purchase_places():
competition=competition
)
else:
club['points'] = place_allowed - places_required * PLACE_COST
club['points'] = places_allowed - places_required * PLACE_COST
competition['places'] = int(competition['places']) - places_required
flash('Great-booking complete!')
return render_template(
Expand Down
4 changes: 3 additions & 1 deletion src/templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ <h2>{{ competition['name'] }}</h2>
</ul>
{% endif %}
{% endwith %}
Places available: {{ competition['places'] }}

<p>Points available: {{ club['points'] }}</p>
<p>Places available: {{ competition['places'] }}</p>

<form action="/purchase-places" method="post">
<input type="hidden" name="club" value="{{ club['name'] }}">
Expand Down
7 changes: 5 additions & 2 deletions src/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ <h3>Competitions:</h3>
{{ comp['name'] }}<br />
Date: {{ comp['date'] }}</br>
Number of Places: {{ comp['places'] }}
{% if comp['places']|int > 0 and comp['date'] > current_datetime %}
<a href="{{ url_for('book', competition=comp['name'], club=club['name']) }}">Book Places</a>
{% if comp['places']|int <= 0 %}
</br>
No more places available
{% elif comp['date'] < current_datetime %}
</br>
Competition over
{% else %}
<a href="{{ url_for('book', competition=comp['name'], club=club['name']) }}">Book Places</a>
{% endif %}
</li>
<hr />
Expand Down
41 changes: 41 additions & 0 deletions src/tests/test_purchase_places.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ def test_purchase_places_with_valid_data(mocker, client, clubs, competitions):
assert 'Competition over' not in response.data.decode()


def test_purchase_more_places_than_available(
mocker, client, clubs, competitions
):
for competition in competitions:
competition['places'] = "1"
mocker.patch('server.clubs', clubs)
mocker.patch('server.competitions', competitions)
data = {
'club': clubs[0]['name'],
'competition': competitions[0]['name'],
'places': f"{int(competitions[0]['places']) + 1}",
}
response = client.post('purchase-places', data=data)
assert response.status_code == 200
assert 'Not enough places available' in response.data.decode()


def test_purchase_places_with_more_points_than_available(
mocker, client, clubs, competitions
):
Expand Down Expand Up @@ -59,6 +76,30 @@ def test_purchase_places_over_date_limit(mocker, client, clubs, competitions):
assert 'Competition over' in response.data.decode()


def test_link_to_purchase_page(
mocker, client, clubs, competitions
):
for competition in competitions:
competition['places'] = f"{int(clubs[0]['points']) + 5}"
mocker.patch('server.clubs', clubs)
mocker.patch('server.competitions', competitions)
response = client.post('show-summary', data=clubs[0])
assert response.status_code == 200
assert 'Book Places' in response.data.decode()


def test_no_link_to_purchase_page_if_no_more_places_available(
mocker, client, clubs, competitions
):
for competition in competitions:
competition['places'] = "0"
mocker.patch('server.clubs', clubs)
mocker.patch('server.competitions', competitions)
response = client.post('show-summary', data=clubs[0])
assert response.status_code == 200
assert 'No more places available' in response.data.decode()


def test_no_link_to_purchase_page_of_past_competition(
mocker, client, clubs, competitions
):
Expand Down

0 comments on commit 51735aa

Please sign in to comment.