1
- import json
1
+ import json , datetime
2
2
3
3
from flask import Flask , render_template , request , redirect , flash , url_for
4
4
5
+ from utils import string_to_datetime
5
6
6
7
def loadClubs ():
7
8
with open ('clubs.json' ) as c :
@@ -42,7 +43,8 @@ def book(competition, club):
42
43
foundClub = [c for c in clubs if c ['name' ] == club ][0 ]
43
44
foundCompetition = [c for c in competitions if c ['name' ] == competition ][0 ]
44
45
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 )
46
48
else :
47
49
flash ("Something went wrong-please try again" )
48
50
return render_template ('welcome.html' , club = club , competitions = competitions )
@@ -52,9 +54,30 @@ def book(competition, club):
52
54
def purchasePlaces ():
53
55
competition = [c for c in competitions if c ['name' ] == request .form ['competition' ]][0 ]
54
56
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
56
79
competition ['numberOfPlaces' ] = int (competition ['numberOfPlaces' ])- placesRequired
57
- flash ('Great-booking complete!' )
80
+ flash (f 'Great-booking complete ( { placesRequired } booked) !' )
58
81
return render_template ('welcome.html' , club = club , competitions = competitions )
59
82
60
83
0 commit comments