Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
139 lines (89 loc) · 4.42 KB
/
Copy pathserver.py
File metadata and controls
139 lines (89 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import json
from datetime import datetime
from flask import Flask, render_template, request, redirect, flash, url_for
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
app = Flask(__name__)
app.secret_key = 'something_special'
competitions = loadCompetitions()
clubs = loadClubs()
def find_by(items, key, value):
matching_items = [item for item in items if item[key] == value]
if not matching_items:
return None
return matching_items[0]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/showSummary', methods=['POST'])
def showSummary():
email = request.form['email']
club = find_by(clubs, "email", email)
if club is None:
flash("Sorry, that email wasn't found.")
return render_template('index.html')
else:
return render_template('welcome.html', club=club, competitions=competitions)
def is_competition_in_the_future(competition):
competition_date = datetime.strptime(competition['date'], '%Y-%m-%d %H:%M:%S')
return datetime.now() < competition_date
@app.route('/book/<competition>/<club>')
def book(competition, club):
foundClub = find_by(clubs, "name", club)
foundCompetition = find_by(competitions, "name", competition)
if not foundClub or not foundCompetition:
flash("Sorry, we could not find that club or competition.")
return render_template('welcome.html', club=foundClub, competitions=competitions)
if not is_competition_in_the_future(foundCompetition):
flash("Sorry, this competition has already taken place.")
return render_template('welcome.html', club=foundClub, competitions=competitions)
return render_template('booking.html', club=foundClub, competition=foundCompetition)
def has_enough_points(club, places_required):
return places_required <= int(club['points'])
def has_enough_places(competition, places_required):
return places_required <= int(competition['numberOfPlaces'])
def is_within_twelve_limit(competition, club, places_required):
return competition['bookings'].get(club['name'], 0) + places_required <= 12
def is_valid_places_input(places_input):
return places_input.isdigit() and int(places_input) >= 1
def register_purchase(competition, club, placesRequired):
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired
club['points'] = int(club['points']) - placesRequired
booked = competition['bookings'].get(club['name'], 0)
competition['bookings'][club['name']] = booked + placesRequired
@app.route('/purchasePlaces', methods=['POST'])
def purchasePlaces():
competition = find_by(competitions, "name", request.form['competition'])
club = find_by(clubs, "name", request.form['club'])
if not competition or not club:
flash("Sorry, we could not find that club or competition.")
return render_template('welcome.html', club=club, competitions=competitions)
places_input = request.form['places']
if not is_valid_places_input(places_input):
flash("Please enter a number of places greater than zero.")
return render_template('welcome.html', club=club, competitions=competitions)
placesRequired = int(places_input)
if not has_enough_points(club, placesRequired):
flash("Sorry, you do not have enough points for that many places.")
return render_template('welcome.html', club=club, competitions=competitions)
if not has_enough_places(competition, placesRequired):
flash("Sorry, there are not enough places left in this competition.")
return render_template('welcome.html', club=club, competitions=competitions)
if not is_within_twelve_limit(competition, club, placesRequired):
flash("Sorry, a club cannot book more than 12 places in one competition.")
return render_template('welcome.html', club=club, competitions=competitions)
register_purchase(competition, club, placesRequired)
flash("Great! Your booking is complete.")
return render_template('welcome.html', club=club, competitions=competitions)
@app.route('/pointsBoard')
def pointsBoard():
return render_template('board.html', clubs=clubs)
@app.route('/logout')
def logout():
return redirect(url_for('index'))