-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
315 lines (266 loc) · 8.68 KB
/
app.py
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from flask import Flask, request, render_template, Response
import sqlite3
import traceback
# create db connection
def get_db_connection():
conn = sqlite3.connect('/var/www/F1_LEADERBOARD/test.db')
conn.row_factory = sqlite3.Row # Set row_factory to sqlite3.Row
cursor = conn.cursor()
return conn, cursor
##SET PASSWORD
## IF NO HTTPS; This should deffienetly be replaced by some rolling code like OAUTH 2FA
def PSWD():
return '2365'
# ==================== DATABASE METHODS =================
#preconditions: teamNo
#postconditions: returns team data
def getTeam(teamNo):
conn, cursor = get_db_connection()
cursor.execute("SELECT * FROM teams WHERE id = ?", (teamNo,))
team = cursor.fetchone()
conn.close()
return team
#preconditions: teamNo
#postconditions: returns all times for a team; sorted chronologically
def getTeamTimes(teamNo):
conn, cursor = get_db_connection()
cursor.execute("SELECT * FROM times WHERE team_id = ? ORDER BY id", (teamNo,))
times = cursor.fetchall()
conn.close()
return times
#preconditions: teamNo
#postconditions: returns the fastest time for a team
def getFastestTime(teamNo):
conn, cursor = get_db_connection()
cursor.execute('''
SELECT MIN(time_record) AS fastest_time
FROM times
WHERE team_id = ?
''', (teamNo,))
result = cursor.fetchone()
conn.close()
if result == None:
return 999
return result['fastest_time']
#preconditions: filter e.g. 4W or 5W, or 5 for all 5th years
#postconditions: returns a list of teams with their fastest times
def getLeaderboard(filter):
# wild card, return whole DB
if filter == '*':
filter = ''
filter = f"%{filter}%"
conn, cursor = get_db_connection()
cursor.execute('''
SELECT te.id, te.name, te.class, MIN(t.time_record) AS fastest_time
FROM teams te
LEFT JOIN times t ON te.id = t.team_id
WHERE te.class LIKE ?
GROUP BY te.id, te.name
ORDER BY
CASE WHEN MIN(t.time_record) IS NULL THEN 999 ELSE MIN(t.time_record) END;
''', (filter,))
teams = cursor.fetchall()
# get teams with no time
conn.close()
return teams
#preconditions: none
#postconditions: returns all distinct classes in table
def getClasses():
conn, cursor = get_db_connection()
cursor.execute('''
SELECT DISTINCT class
FROM teams
ORDER BY class;
''')
classes = cursor.fetchall()
conn.close()
if classes == None:
return []
return set(item['class'] for item in classes)
#preconditions: teamNo
#postconditions: checks if team exists:
def teamExists(teamNo):
conn, cursor = get_db_connection()
cursor.execute('''
SELECT COUNT(*)
FROM teams
WHERE id = ?
''', (teamNo,))
result = cursor.fetchone()
conn.close()
return result[0] > 0
# ============== PAGE METHODS ============
# create flask app
app = Flask(__name__)
## Main List View
@app.route('/leaderboard/<filter>')
def leaderboard(filter):
teams = getLeaderboard(filter)
filterDesc = None
if filter.isnumeric():
filterDesc = "Year " + filter
elif filter=="*":
filterDesc = "All Students "
else:
filterDesc = "Class " + filter
return render_template('board.html', teams=teams, desc=filterDesc)
## Index page
@app.route('/')
def index():
# determine what links to present
classes = getClasses()
groups = []
for i in classes:
if i[0] not in groups:
groups.append(i[0])
return render_template('index.html', classes=classes, groups=groups)
### Team Info Page
@app.route('/team/<teamNo>')
def moreInfo(teamNo):
# get team from db
record = getTeam(teamNo)
# if team invalid; error and redirect
if record == None:
return render_template('error.html', message="Team not found")
members = record['members']
# calculate graph
timeList = []
labelList = []
count = 1
for i in getTeamTimes(teamNo):
timeList.append(i['time_record'])
labelList.append(count)
count+=1
# render and return
return render_template('moreInfo.html', number=str(teamNo), name=record['name'].title(), members=members, tclass=record['class'], fastest = getFastestTime(teamNo), chartLabels=str(labelList), chartTimes=str(timeList))
### Add Team Page
@app.route('/addTeam')
def admin():
return render_template('addTeam.html')
## Add Time Page
@app.route('/addTime')
def addTime():
return render_template('addTime.html')
# edit team page
# lists all teams; then you can hit edit
@app.route('/editTeam', methods=['GET'])
def editTeam():
# get all teams
conn, cursor = get_db_connection()
cursor.execute("SELECT * FROM teams")
teams = cursor.fetchall()
#print(teams)
conn.close()
return render_template('editTeam.html', teams = teams)
# edit team page
# has details of a specific team
@app.route('/editTeam/<teamNo>')
def editTeamPage(teamNo):
# get team from db
team = getTeam(teamNo)
times = getTeamTimes(teamNo)
#print(dict(team))
#for t in times:
# print(dict(t))
# if team invalid; error and redirect
if team == None:
return render_template('error.html', message="Team not found")
return render_template('editATeam.html', team=team, times=times)
##======== API METHODS
# add a new team
@app.route('/submitTeam', methods=['POST'])
def submitTeam():
# get form data
data = request.get_json()
print(data)
# check if team already exists:
if teamExists(data['number']):
return Response(status=409) # conflict status
# check password
if data['pswd'] != PSWD():
return Response(status=401)
# clean up data
members = data['members']
members = [i.strip().title() for i in members]
members = ', '.join(members)
# attempt to add team
#
try:
conn, cursor = get_db_connection()
# insert team
cursor.execute('INSERT INTO teams (id, name, members, class) VALUES (?, ?, ?, ?)',
(data['number'], data['name'].title().strip(), members, data['className'].strip().upper()))
conn.commit() # save to db
except Exception as e:
print("Exception whilst inserting team into DB")
print(e)
return Response(status=500)
return Response(status=200)
# add a time
@app.route('/addTime', methods=['POST'])
def addTimeAPI():
# get form data
data = request.get_json()
print(data)
# check if team exists:
if teamExists(data['team']) == False:
return Response(status=404) # conflict status
# check password
if data['pswd'] != PSWD():
return Response(status=401)
# attempt add
try:
conn, cursor = get_db_connection()
# insert team
cursor.execute('INSERT INTO times (team_id, time_record) VALUES (?, ?)',
(data['team'], data['time']))
conn.commit() # save to db
except Exception as e:
print("Exception whilst inserting time into DB")
print(e)
return Response(status=500)
return Response(status=200)
# edit a team
@app.route('/editTeam', methods=['POST'])
def editTeamSubmit():
data = request.get_json()
# check if team exists:
if teamExists(data['teamID']) == False:
return Response(status=404) # conflict status
# check password
if data['pswd'] != PSWD():
return Response(status=401)
# clean data
members = data['teamMembers'].split(", ")
members = [i.strip().title() for i in members]
members = ', '.join(members)
# try update team table:
try:
conn, cursor = get_db_connection()
# insert team
cursor.execute('UPDATE teams SET name = ?, members = ?, class = ? WHERE id = ?',
(data['teamName'].title().strip(), members, data['teamClass'].strip().upper(), data['teamID']))
conn.commit()
except Exception as e:
print("Exception whilst updating team in DB")
print(e)
return Response(status=500)
# try update times table
try:
# delete all times
cursor.execute('DELETE FROM times WHERE team_id = ?', (data['teamID'],))
conn.commit()
# add back in times
for i in data['timeRecords']:
if float(i) > 0:
cursor.execute('INSERT INTO times (team_id, time_record) VALUES (?, ?)',
(data['teamID'], i))
conn.commit()
except Exception as e:
print("Exception whilst updating times in DB")
print(e)
traceback.print_exc()
return Response(status=500)
return Response(status=200)
if __name__ == '__main__':
app.run(host='localhost',port='8000',debug=True)