-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathserver.py
More file actions
269 lines (196 loc) · 9.67 KB
/
server.py
File metadata and controls
269 lines (196 loc) · 9.67 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import json
from datetime import datetime
from flask import Flask, render_template, request, redirect, flash, url_for
from werkzeug.security import generate_password_hash, check_password_hash
def load_clubs():
with open('clubs.json') as c:
list_of_clubs = json.load(c)['clubs']
return list_of_clubs
def load_competitions():
with open('competitions.json') as comps:
list_of_competitions = json.load(comps)['competitions']
return list_of_competitions
app = Flask(__name__)
app.secret_key = 'something_special'
competitions = load_competitions()
clubs = load_clubs()
CLUB_POINTS = 15
def update_club_booked_places(club, places, competition_name):
clubs.remove(club)
club.setdefault("booked_places", {})
current = int(club["booked_places"].get(competition_name, 0))
club["booked_places"][competition_name] = str(current + places)
club["points"] = str(int(club["points"]) - places)
clubs.append(club)
save_clubs()
def save_clubs():
with open('clubs.json', 'w') as c:
list_of_clubs = {"clubs": clubs}
json.dump(list_of_clubs, c, indent=4)
def update_competition_available_places(competition, places):
competitions.remove(competition)
competition['number_of_places'] = str(int(competition['number_of_places']) - places)
competitions.append(competition)
save_competitions()
def save_competitions():
with open('competitions.json', 'w') as comps:
list_of_competitions = {"competitions": competitions}
json.dump(list_of_competitions, comps, indent=4)
def add_club(name, email, password, points):
clubs.append({"name": name, "email": email, "password": password, "points": points})
save_clubs()
def update_club_password(club, password):
hashed_password = generate_password_hash(password)
club["password"] = hashed_password
save_clubs()
return club
@app.route('/')
def index():
return render_template('index.html')
@app.route('/signUp')
def sign_up():
return render_template('sign_up.html')
@app.route('/profile/<club>', methods=['GET'])
def profile(club):
the_club = next((c for c in clubs if c['name'] == club), None)
if the_club is None:
flash("Sorry, that club was not found.")
return render_template(template_name_or_list="index.html", error="Club not found"), 404
return render_template(template_name_or_list='profile.html', club=the_club)
@app.route('/profile', methods=['POST'])
def profile_post():
club_name = request.form['name']
club_email = request.form['email']
club_password = request.form['password']
club_password_confirmation = request.form['confirm_password']
club_exists = next((c for c in clubs if c['email'] == club_email or c['name'] == club_name), None)
if club_exists is None:
if club_password != club_password_confirmation:
flash('Sorry, passwords do not match')
return redirect(url_for('sign_up'))
hashed_password = generate_password_hash(club_password)
add_club(club_name, club_email, hashed_password, str(CLUB_POINTS))
the_club = next((c for c in clubs if c['email'] == club_email), None)
if the_club is None:
flash("Sorry, something went wrong. Please try again.")
return render_template(template_name_or_list='sign_up.html')
flash("Great! You have successfully signed up.")
return render_template(template_name_or_list='profile.html', club=the_club)
else:
flash("Sorry, the club already exists.")
return render_template(template_name_or_list='sign_up.html')
@app.route('/changePassword/<club>', methods=['GET', 'POST'])
def change_password(club):
if request.method == 'GET':
the_club = next((c for c in clubs if c['name'] == club), None)
if the_club is None:
flash("Sorry, that club was not found.")
return render_template(template_name_or_list="index.html", error="Email not found"), 404
return render_template(template_name_or_list='change_password.html', club=the_club)
else:
club_password = request.form['password']
club_password_confirmation = request.form['confirm_password']
if club_password != club_password_confirmation:
flash('Sorry, passwords do not match')
return redirect(url_for('change_password'))
the_club = next((c for c in clubs if c['name'] == club), None)
if check_password_hash(the_club['password'], club_password):
flash('Sorry, you have to type a new different password.')
return render_template(template_name_or_list='change_password.html', club=the_club)
the_club = update_club_password(the_club, club_password)
if the_club:
flash("Great! You have successfully changed your password.")
return render_template(template_name_or_list='profile.html', club=the_club)
flash("Sorry, something went wrong. Please try again.")
return render_template(template_name_or_list='index.html')
@app.route('/showSummary/<club>', methods=['GET'])
def show_summary(club):
the_club = next((c for c in clubs if c['name'] == club), None)
return render_template(template_name_or_list='welcome.html',
club=the_club,
competitions=competitions)
@app.route('/showSummary', methods=['POST'])
def show_summary_post():
the_club = next((c for c in clubs if c['email'] == request.form['email']), None)
if the_club is None:
flash("Sorry, that email was not found.")
return render_template(template_name_or_list="index.html", error="Email not found"), 404
if not check_password_hash(the_club['password'], request.form['password']):
flash("Sorry, the password is incorrect.")
return render_template(template_name_or_list="index.html",)
return render_template(template_name_or_list='welcome.html',
club=the_club,
competitions=competitions)
@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]
now = datetime.now()
competition_date = datetime.strptime(found_competition['date'], '%Y-%m-%d %H:%M:%S')
error_message = ""
error_tag = ""
the_competition = next((a_competition for a_competition in competitions
if a_competition['name'] == competition), None)
competition_places = int(the_competition['number_of_places'])
if now > competition_date:
error_message = "Sorry, this competition is outdated. Booking not possible."
error_tag = "Outdated"
elif competition_places == 0:
error_message = "Sorry, this competition is sold out. Booking not possible."
error_tag = "Sold out"
if error_message and error_tag:
flash(error_message)
the_club = next((a_club for a_club in clubs if a_club['name'] == club), None)
return render_template(template_name_or_list='welcome.html',
club=the_club,
competitions=competitions,
error=error_tag), 403
if found_club and found_competition:
return render_template(template_name_or_list='booking.html',
club=found_club,
competition=found_competition)
else:
flash("Sorry, something went wrong. Please try again.")
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions)
@app.route('/purchasePlaces',methods=['POST'])
def purchase_places():
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'])
cumulative_places = places_required + int(club["booked_places"][competition["name"]]) \
if "booked_places" in club else places_required
error_message = ""
error_tag = ""
if places_required < 0:
error_message = "Sorry, you should type a positive number."
error_tag = "Negative number"
elif cumulative_places > 12:
error_message = "Sorry, you are not allow to purchase more than 12 places for this competition."
error_tag = "Over 12 places"
elif places_required > int(competition['number_of_places']):
error_message = "Sorry, there are not enough places available for this competition."
error_tag = "Not enough places"
elif places_required > int(club['points']):
error_message = "Sorry, you do not have enough points to purchase."
error_tag = "Not enough points"
if error_message and error_tag:
flash(error_message)
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions,
error=error_tag), 403
update_club_booked_places(club=club,
places=places_required,
competition_name=competition["name"])
update_competition_available_places(competition=competition, places=places_required)
flash(f"Great! Booking of {places_required} places for "
f"{competition['name']} competition complete!")
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions)
# TODO: Add route for points display
@app.route('/logout')
def logout():
return redirect(url_for('index'))