Skip to content

Commit 7a0ea82

Browse files
committed
fix (purchasePlaces, book): fix views to forbid some booking
to respect the app specifications and closes some issues Resolves OpenClassrooms-Student-Center#2, resolves OpenClassrooms-Student-Center#4, resolves OpenClassrooms-Student-Center#5, resolves OpenClassrooms-Student-Center#6,
1 parent 1a90eec commit 7a0ea82

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

Diff for: server.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import json
1+
import json, datetime
22

33
from flask import Flask, render_template, request, redirect, flash, url_for
44

5+
from utils import string_to_datetime
56

67
def loadClubs():
78
with open('clubs.json') as c:
@@ -42,7 +43,8 @@ def book(competition, club):
4243
foundClub = [c for c in clubs if c['name'] == club][0]
4344
foundCompetition = [c for c in competitions if c['name'] == competition][0]
4445
if foundClub and foundCompetition:
45-
return render_template('booking.html',club=foundClub,competition=foundCompetition)
46+
places_maximum = min([12, int(foundCompetition['numberOfPlaces']), int(foundClub['points'])])
47+
return render_template('booking.html',club=foundClub,competition=foundCompetition, places_maximum=places_maximum)
4648
else:
4749
flash("Something went wrong-please try again")
4850
return render_template('welcome.html', club=club, competitions=competitions)
@@ -52,9 +54,30 @@ def book(competition, club):
5254
def purchasePlaces():
5355
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
5456
club = [c for c in clubs if c['name'] == request.form['club']][0]
55-
placesRequired = int(request.form['places'])
57+
places_maximum = min([12, int(competition['numberOfPlaces']), int(club['points'])])
58+
try:
59+
placesRequired = int(request.form['places'])
60+
except:
61+
if request.form.get('places') == '' or request.form.get('places') == None:
62+
placesRequired = 0
63+
else:
64+
flash(f'({request.form.get("places")}) is not an allowed value !')
65+
return render_template('booking.html', club=club, competition=competition, places_maximum=places_maximum), 403
66+
if placesRequired > 12:
67+
flash('You can\'t book more than 12 places !')
68+
return render_template('booking.html', club=club, competition=competition, places_maximum=places_maximum), 403
69+
elif placesRequired > int(competition['numberOfPlaces']):
70+
flash(f'You can\'t book more than the total of available places ({competition["numberOfPlaces"]}) !')
71+
return render_template('booking.html', club=club, competition=competition, places_maximum=places_maximum), 403
72+
elif placesRequired > int(club['points']):
73+
flash(f'You can\'t book more than your total of points ({club["points"]}) !')
74+
return render_template('booking.html', club=club, competition=competition, places_maximum=places_maximum), 403
75+
elif string_to_datetime(competition['date']) < datetime.datetime.now():
76+
flash(f'You can\'t book on a past competition !')
77+
return render_template('booking.html', club=club, competition=competition, places_maximum=places_maximum), 403
78+
club['points'] = int(club['points'])-placesRequired
5679
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
57-
flash('Great-booking complete!')
80+
flash(f'Great-booking complete ({placesRequired} booked) !')
5881
return render_template('welcome.html', club=club, competitions=competitions)
5982

6083

Diff for: templates/booking.html

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@
66
</head>
77
<body>
88
<h2>{{competition['name']}}</h2>
9+
Point available: {{club['points']}}<br/>
910
Places available: {{competition['numberOfPlaces']}}
11+
{% with messages = get_flashed_messages()%}
12+
{% if messages %}
13+
<ul>
14+
{% for message in messages %}
15+
<li>{{message}}</li>
16+
{% endfor %}
17+
</ul>
18+
{% endif%}
1019
<form action="/purchasePlaces" method="post">
1120
<input type="hidden" name="club" value="{{club['name']}}">
1221
<input type="hidden" name="competition" value="{{competition['name']}}">
13-
<label for="places">How many places?</label><input type="number" name="places" id=""/>
22+
<label for="places">How many places?</label><input type="number" name="places" id="" max="{{places_maximum}}" min="0"/>
1423
<button type="submit">Book</button>
1524
</form>
25+
{% endwith %}
1626
</body>
1727
</html>

Diff for: utils.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import datetime
2+
3+
4+
def string_to_datetime(string: str) -> datetime.datetime:
5+
date_time = string.split(' ')
6+
date = date_time[0].split('-')
7+
time = date_time[1].split(':')
8+
return datetime.datetime(
9+
*[
10+
*[ int(value) for value in date],
11+
*[ int(value) for value in time]
12+
]
13+
)

0 commit comments

Comments
 (0)