Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
226 lines (200 loc) · 7.2 KB
/
Copy pathserver.py
File metadata and controls
226 lines (200 loc) · 7.2 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
import json
import requests
import datetime as dt
import time
from flask import Flask, render_template, request, redirect, flash, url_for
MAX_BOOK = 12
PLACE_COST = 3
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"]
for i in list_of_competitions:
i["date_stamp"] = dt.datetime.timestamp(
dt.datetime.strptime(i["date"], "%Y-%m-%d %H:%M:%S")
)
return list_of_competitions
app = Flask(__name__)
app.secret_key = "something_special"
competitions = load_competitions()
clubs = load_clubs()
now = time.time()
@app.route("/", strict_slashes=False)
def index():
return render_template("index.html", clubs=clubs)
@app.route("/show-summary", methods=["GET", "POST"], strict_slashes=False)
def show_summary():
try:
club = [club for club in clubs if club["email"] == request.form["email"]][0]
except (IndexError, TypeError):
return page_not_found()
except requests.exceptions.RequestException:
flash("Something went wrong-please try again")
return redirect(url_for(index))
return render_template(
"welcome.html",
now=now,
club=club,
competitions=competitions,
place_cost=PLACE_COST,
)
@app.route("/book/<competition>/<club>", strict_slashes=False)
def book(club, competition):
if club and competition:
try:
club = [i for i in clubs if i["name"] == club][0]
competition = [c for c in competitions if c["name"] == competition][0]
except IndexError:
flash("Something went wrong-please try again")
return render_template("index.html")
try:
club["points"] > 0
except TypeError:
club = [i for i in clubs if i["name"] == club["name"]][0]
competition = [c for c in competitions if c["name"] == competition["name"]][
0
]
if int(club["points"]) < 0:
flash("Something went wrong-please try again")
return render_template(
"welcome.html",
now=now,
club=club,
competitions=competitions,
place_cost=PLACE_COST,
)
elif int(club["points"]) > 0:
if (
int(club["points"]) / PLACE_COST >= MAX_BOOK
and competition["numberOfPlaces"] >= MAX_BOOK
):
flash(f"max places you can book is {MAX_BOOK}")
return render_template(
"booking.html",
club=club,
competition=competition,
maxi=MAX_BOOK,
)
elif (int(club["points"]) / PLACE_COST) >= int(
competition["numberOfPlaces"]
):
flash(f"max places you can book is {competition['numberOfPlaces']}")
return render_template(
"booking.html",
club=club,
competition=competition,
maxi=int(competition["numberOfPlaces"] / PLACE_COST),
)
else:
flash(
f'max places you can book is {int(int(club["points"]) / PLACE_COST)}'
)
return render_template(
"booking.html",
club=club,
competition=competition,
maxi=int(int(club["points"]) / PLACE_COST),
)
else:
flash("You cannot access booking page, you have no points left")
return render_template(
"welcome.html",
now=now,
club=club,
competitions=competitions,
place_cost=PLACE_COST,
)
else:
flash("Something went wrong-please try again")
return render_template(
"welcome.html",
now=now,
club=club,
competitions=competitions,
place_cost=PLACE_COST,
)
@app.route("/purchase-places", methods=["POST"], strict_slashes=False)
def purchase_places():
try:
request.form["club_name"] and request.form["competition_name"] and request.form[
"places"
]
except Exception:
flash("bad request")
return render_template("index.html")
try:
compet = [
c for c in competitions if c["name"] == request.form["competition_name"]
][0]
club = [c for c in clubs if c["name"] == request.form["club_name"]][0]
places = request.form["places"]
if compet["date_stamp"] < now:
flash("You cannot purchase places for a past competition")
return render_template(
"booking.html",
club=club,
competition=compet,
maxi=MAX_BOOK,
)
except Exception:
flash("bad request")
return render_template("index.html")
if places.startswith("-") and club["points"][1:].isdigit():
flash("you cannot book a negative number of places")
return render_template(
"booking.html",
club=club,
competition=compet,
maxi=MAX_BOOK,
)
else:
places_required = int(request.form["places"])
if (places_required * PLACE_COST) > int(club["points"]):
flash("You cannot perform this action")
return render_template(
"welcome.html",
now=now,
club=club,
competitions=competitions,
place_cost=PLACE_COST,
)
elif places_required > int(compet["numberOfPlaces"]):
flash(
f"You cannot perform this action"
f"You asked {places_required} place(s)"
f"and the competition has {compet['numberOfPlaces']} places left"
)
return render_template("booking.html", club=club, competition=compet)
else:
flash("Great-booking complete!")
club["info"] = [
["Places Booked", places_required],
["Cost Per Place", PLACE_COST],
["Points Redeemed", places_required * PLACE_COST],
["Club Points Before", club["points"]],
["Club Points After", int(club["points"]) - (places_required * PLACE_COST)],
]
club["points"] = int(club["points"]) - (places_required * PLACE_COST)
compet["numberOfPlaces"] = int(compet["numberOfPlaces"]) - places_required
return render_template(
"welcome.html",
now=now,
club=club,
competitions=competitions,
place_cost=PLACE_COST,
)
# TODO: Add route for points display
@app.route("/points-board", strict_slashes=False)
def points_board():
return render_template("points_board.html", clubs=clubs)
@app.route("/logout", strict_slashes=False)
def logout():
return redirect(url_for("index"))
@app.errorhandler(404)
def page_not_found():
return render_template("not_found_404.html"), 404
if __name__ == "__main__":
app.run(debug=True)