forked from crhee94/Basketball_League
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
618 lines (559 loc) · 24.6 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
from flask import Flask, render_template, request
import psycopg2
from psycopg2 import Error
app = Flask(__name__)
def connectToDB():
try:
return psycopg2.connect(host = "localhost", port = "5432", database = "test")
except (Exception, psycopg2.Error) as error:
print("Can't connect to database")
print(error)
def createTables(cursor):
try:
cursor.execute(
''' CREATE TABLE IF NOT EXISTS "Team" (
ID SERIAL NOT NULL,
TeamName VARCHAR(100) NOT NULL UNIQUE,
PRIMARY KEY (ID)
);'''
)
print("Team: Works")
cursor.execute(
''' CREATE TABLE IF NOT EXISTS "Player" (
ID SERIAL NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
Height VARCHAR(6),
Weight INT,
Age INT,
TeamId INT NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (TeamId) REFERENCES "Team" (Id)
);'''
)
print("Player: Works")
cursor.execute(
''' CREATE TABLE IF NOT EXISTS "Season" (
Year INT UNIQUE,
PRIMARY KEY (YEAR)
); '''
)
print("Season: Works")
cursor.execute(
''' CREATE TABLE IF NOT EXISTS "Game" (
ID SERIAL NOT NULL,
SeasonYear INT NOT NULL,
HomeTeamId INT NOT NULL,
AwayTeamId INT NOT NULL,
WinnerId INT,
LoserId INT,
GameDate TIMESTAMPTZ,
PRIMARY KEY (ID),
FOREIGN KEY (SeasonYear) REFERENCES "Season" (Year),
FOREIGN KEY (HomeTeamId) REFERENCES "Team" (Id),
FOREIGN KEY (AwayTeamId) REFERENCES "Team" (Id)
); '''
)
print("Game: Works")
cursor.execute(
''' CREATE TABLE IF NOT EXISTS "StatType" (
ID VARCHAR(5) NOT NULL,
Description VARCHAR(30),
PRIMARY KEY (ID)
);'''
)
print("StatType: Works")
cursor.execute(
''' CREATE TABLE IF NOT EXISTS "StatInfo" (
ID SERIAL NOT NULL,
PlayerID INT NOT NULL,
GameID INT NOT NULL,
Type VARCHAR(5) NOT NULL,
StatValue INT NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (PlayerId) REFERENCES "Player" (Id),
FOREIGN KEY (GameId) REFERENCES "Game" (Id),
FOREIGN KEY (Type) REFERENCES "StatType" (Id)
);'''
)
print("StatInfo: Works")
cursor.execute(
''' CREATE TABLE IF NOT EXISTS "TeamInSeason" (
SeasonId INT NOT NULL,
TeamId INT NOT NULL,
PRIMARY KEY (SeasonId, TeamId),
FOREIGN KEY (SeasonId) REFERENCES "Season" (Year),
FOREIGN KEY (TeamId) REFERENCES "Team" (Id)
);'''
)
print("TeamInSeason: Works")
db.commit()
except (Exception, psycopg2.DatabaseError) as error :
print ("Error while creating PostgreSQL table", error)
db.rollback()
finally:
#closing database connection.
if(db):
#cursor.close()
# db.close()
print("PostgreSQL connection is closed")
def queryHomeTeam(game):
cursor.execute(
'''
SELECT "C".FirstName, "C".LastName, "C".TeamName, "A".Points, "B".Rebound, "C".Assist
FROM (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameId, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Points
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( "Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id )
WHERE "StatInfo".Type = 'Pts' AND GameId = %(int)s
) AS "A"
INNER JOIN (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameID, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Rebound
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( "Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id )
WHERE "StatInfo".Type = 'Reb' AND GameId = %(int)s
) AS "B"
ON "A".PlayerId = "B".PlayerId
INNER JOIN (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameID, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Assist
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( "Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id )
WHERE "StatInfo".Type = 'Ast' AND GameId = %(int)s
) AS "C"
ON "B".PlayerId = "C".PlayerId;
''', {'int': game[4:]})
rows = cursor.fetchall()
return rows
def queryAwayTeam(game):
cursor.execute(
'''
SELECT "C".FirstName, "C".LastName, "C".TeamName, "A".Points, "B".Rebound, "C".Assist
FROM (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameId, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Points
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( "Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id )
WHERE "StatInfo".Type = 'Pts' AND GameId = %(int)s
) AS "A"
INNER JOIN (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameID, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Rebound
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( "Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id )
WHERE "StatInfo".Type = 'Reb' AND GameId = %(int)s
) AS "B"
ON "A".PlayerId = "B".PlayerId
INNER JOIN (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameID, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Assist
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( "Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id )
WHERE "StatInfo".Type = 'Ast' AND GameId = %(int)s
) AS "C"
ON "B".PlayerId = "C".PlayerId;
''', {'int': game[4:]})
rows = cursor.fetchall()
return rows
def showSchedule():
cursor.execute(
'''
SELECT "Game".Id, "HomeTeam".TeamName AS HomeTeam, "AwayTeam".TeamName AS AwayTeam, "Game".GameDate
FROM "Game"
LEFT JOIN "Team" AS "HomeTeam" ON ( "Game".HomeTeamId = "HomeTeam".Id )
LEFT JOIN "Team" AS "AwayTeam" ON ( "Game".AwayTeamId = "AwayTeam".Id )
ORDER BY GameDate;
''')
schedules = cursor.fetchall()
print(schedules)
return schedules
def queryTotalPoints(game):
cursor.execute(
'''
SELECT "Team".TeamName AS TeamName, SUM(StatValue) AS Points
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ("Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id)
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Pts' AND "Game".Id = %s
GROUP BY "Team".Id;
''', (game[4:]))
rows = cursor.fetchall()
return rows
def queryTotalRebounds(game):
cursor.execute(
'''
SELECT "Team".TeamName AS TeamName, SUM(StatValue) AS Rebound
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ("Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id)
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Reb' AND "Game".Id = %s
GROUP BY "Team".Id;
''', (game[4:]))
rows = cursor.fetchall()
return rows
def queryTotalAssists(game):
cursor.execute(
'''
SELECT "Team".TeamName AS TeamName, SUM(StatValue) AS Rebound
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ("Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id)
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Ast' AND "Game".Id = %s
GROUP BY "Team".Id;
''', (game[4:]))
rows = cursor.fetchall()
return rows
def showStandings():
cursor.execute(
'''
SELECT "Team".TeamName, Wins, Losses
FROM "Team"
INNER JOIN (
SELECT "W".TeamName AS Winner, "L".TeamName AS Loser, Wins, Losses
FROM (
SELECT "Team".TeamName, COUNT("Game".WinnerId) AS Wins
FROM "Game"
RIGHT JOIN "Team" ON ( "Game".WinnerId = "Team".Id )
GROUP BY TeamName
) AS "W"
INNER JOIN(
SELECT "Team".TeamName, COUNT("Game".LoserId) AS Losses
FROM "Game"
RIGHT JOIN "Team" ON ( "Game".LoserId = "Team".Id )
GROUP BY TeamName
) AS "L" ON "W".TeamName = "L".TeamName
) AS "Final" ON ("Team".TeamName = "Final".Winner AND "Team".TeamName = "Final".Loser)
ORDER BY WINS DESC;
'''
)
standing = cursor.fetchall()
return standing
def showTeamList():
cursor.execute(
'''
SELECT "Team".TeamName
FROM "Team";
'''
)
teams = cursor.fetchall()
return teams
def showTeamInfo(team):
cursor.execute(
'''
SELECT FirstName, LastName, Age, Height, Weight
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
WHERE "Team".Id = %s;
''', (team[4:]))
teams = cursor.fetchall()
return teams
def showAveragePoints(team):
cursor.execute(
'''
SELECT "Team".TeamName AS TeamName, ROUND((CAST(SUM(StatValue) AS DECIMAL) / 3), 1) AS PPG
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ("Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id)
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Pts' AND "Team".Id = %s
GROUP BY "Team".TeamName;
''', (team[4:]))
stat = cursor.fetchall()
return stat
def showAverageRebounds(team):
cursor.execute(
'''
SELECT "Team".TeamName AS TeamName, ROUND((CAST(SUM(StatValue) AS DECIMAL) / 3), 1) AS RPG
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ("Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id)
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Reb' AND "Team".Id = %s
GROUP BY "Team".TeamName;
''', (team[4:]))
stat = cursor.fetchall()
return stat
def showAverageAssists(team):
cursor.execute(
'''
SELECT "Team".TeamName AS TeamName, ROUND((CAST(SUM(StatValue) AS DECIMAL) / 3), 1) AS APG
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ("Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id)
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Ast' AND "Team".Id = %s
GROUP BY "Team".TeamName;
''', (team[4:]))
stat = cursor.fetchall()
return stat
# IF THE GAME HAS NOT BEEN PLAYED YET AND USER SEARCHES THE GAME, SHOW GAME TIME, HOME TEAM, AWAY TEAM
def gamePreview(game):
cursor.execute(
'''
SELECT "Game".Id, "HomeTeam".TeamName AS HomeTeam, "AwayTeam".TeamName AS AwayTeam, "Game".GameDate
FROM "Game"
LEFT JOIN "Team" AS "HomeTeam" ON ( "Game".HomeTeamId = "HomeTeam".Id )
LEFT JOIN "Team" AS "AwayTeam" ON ( "Game".AwayTeamId = "AwayTeam".Id )
WHERE "Game".Id = 4;
''', (game[4:]))
rows = cursor.fetchall()
return rows
def queryPlayerStat(firstname, lastname, teamname):
cursor.execute(
'''
SELECT "D".GameDate, "A".Points, "B".Rebound, "C".Assist
FROM (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameId, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Points, "Game".gamedate AS GameDate
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ( "Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id )
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Pts'
) AS "A"
INNER JOIN (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameID, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Rebound, "Game".gamedate AS GameDate
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ( "Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id )
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Reb'
) AS "B"
ON "A".PlayerId = "B".PlayerId AND "A".GameId = "B".GameId
INNER JOIN (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameID, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Assist, "Game".gamedate AS GameDate
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ( "Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id )
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Ast'
) AS "C"
ON "B".PlayerId = "C".PlayerId AND "B".GameId = "C".GameId
INNER JOIN (
SELECT "Player".Id AS PlayerId, "Game".Id AS GameID, FirstName, LastName, "Team".TeamName AS TeamName, StatValue AS Assist, "Game".gamedate AS GameDate
FROM "Player"
INNER JOIN "Team" ON ( "Player".TeamId = "Team".Id )
INNER JOIN "StatInfo" ON ( "Player".Id = "StatInfo".PlayerId )
INNER JOIN "Game" ON ( ( "Team".Id = "Game".HomeTeamId AND "StatInfo".GameId = "Game".Id )
OR ("Team".Id = "Game".AwayTeamId AND "StatInfo".GameId = "Game".Id) )
WHERE "StatInfo".Type = 'Ast'
) AS "D"
ON "C".PlayerId = "D".PlayerId AND "C".GameId = "D".GameId
WHERE "C".FirstName = %s AND "C".LastName = %s AND "C".TeamName = %s;
''', (firstname, lastname, teamname))
rows = cursor.fetchall()
return rows
def displayTeamsPlayed(game):
cursor.execute(
'''
SELECT TeamName
FROM "Team"
INNER JOIN "Game" ON ( "Team".Id = "Game".HomeTeamId OR "Team".Id = "Game".AwayTeamId )
WHERE "Game".Id = %s;
''', (game[4:]))
teams = cursor.fetchall()
return teams
def updateGameResult(winner, loser, game):
print(winner)
print(loser)
print(game)
cursor.execute(
'''
UPDATE "Game"
SET WinnerId = (SELECT "Team".Id FROM "Team" WHERE TeamName ILIKE %s),
LoserId = (SELECT "Team".Id FROM "Team" WHERE TeamName ILIKE %s)
WHERE "Game".Id = %s;
''', (winner, loser, game[4:]))
db.commit()
db = connectToDB()
cursor = db.cursor()
#createTables(cursor)
@app.route('/')
@app.route('/home')
def home():
"""
cursor.execute(
'''
SELECT pg_get_serial_sequence('"Player"', 'id'); -- returns 'player_id_seq'
'''
)
cursor.execute(
'''
-- reset the sequence, regardless whether table has rows or not:
SELECT setval(pg_get_serial_sequence('"Player"', 'id'), coalesce(max(id) + 1,1), false) FROM "Player";
'''
)
cursor.execute(
'''
SELECT pg_get_serial_sequence('"StatInfo"', 'id');
'''
)
cursor.execute(
'''
-- reset the sequence, regardless whether table has rows or not:
SELECT setval(pg_get_serial_sequence('"StatInfo"', 'id'), coalesce(max(id) + 1,1), false) FROM "StatInfo";
'''
)
db.commit()
"""
schedule = showSchedule()
return render_template("home.html", title='Home', schedules=schedule)
@app.route('/team', methods=['GET', 'POST'])
def team():
if request.method == "POST":
team = request.form['team']
teams = showTeamList()
print(team)
teamInfo = showTeamInfo(team)
ppg = showAveragePoints(team)
rpg = showAverageRebounds(team)
apg = showAverageAssists(team)
return render_template("team.html", title='Team', teams=teams, teamInfo=teamInfo, ppg=ppg, rpg=rpg, apg=apg)
teams = showTeamList()
return render_template("team.html", title='Team', teams=teams)
@app.route('/player_stats', methods=['GET', 'POST'])
def player():
if request.method == 'POST':
firstname = request.form['firstname']
print(firstname)
lastname = request.form['lastname']
print(lastname)
teamname = request.form['teamname']
print(teamname)
playerStat = queryPlayerStat(firstname, lastname, teamname)
print(playerStat)
if firstname and lastname and teamname is not None:
return render_template("player_stats.html", title='Player', playerStat = playerStat,
firstname = firstname, lastname = lastname, teamname = teamname)
#else()
return render_template("player_stats.html", title='Player')
@app.route('/game_stats', methods=['GET', 'POST'])
def game():
if request.method == "POST":
game = request.form['game']
print(game)
homeTeam = queryHomeTeam(game)
awayTeam = queryAwayTeam(game)
totalPoints = queryTotalPoints(game)
totalRebounds = queryTotalRebounds(game)
totalAssists = queryTotalAssists(game)
print(totalPoints)
print(totalRebounds)
print(totalAssists)
if homeTeam and awayTeam is not None:
return render_template("game_stats.html", title='Game', homeTeam=homeTeam,
awayTeam=awayTeam, totalPoints=totalPoints, totalRebounds=totalRebounds, totalAssists=totalAssists)
else:
gameInfo = gamePreview(game)
print(gameInfo)
return render_template("game_stats.html", title='Game', gameInfo = gameInfo)
return render_template("game_stats.html", title='Game')
@app.route('/standings')
def standing():
standing = showStandings()
print(standing)
return render_template("standings.html", title="Standings", standings=standing)
@app.route('/update_game', methods=['GET', 'POST'])
def update_game():
if request.method == "POST":
game = request.form['game']
teams = displayTeamsPlayed(game)
winner = request.form['winner']
loser = request.form['loser']
updateGameResult(winner, loser, game)
return render_template("update_game.html", title="Update Game", winner=winner, loser=loser, game=game)
return render_template("update_game.html", title="Update Game")
@app.route('/insert_player', methods=['GET', 'POST'])
def insert_player():
cursor.execute(
'''
SELECT pg_get_serial_sequence('"Player"', 'id'); -- returns 'player_id_seq'
'''
)
cursor.execute(
'''
-- reset the sequence, regardless whether table has rows or not:
SELECT setval(pg_get_serial_sequence('"Player"', 'id'), coalesce(max(id) + 1,1), false) FROM "Player";
'''
)
db.commit()
if request.method == 'POST':
firstname = request.form['firstname']
print(firstname)
lastname = request.form['lastname']
print(lastname)
height = request.form['height']
print(height)
weight = request.form['weight']
print(weight)
age = request.form['age']
print(age)
teamname = request.form['teamname']
print(teamname)
cursor.execute('''
INSERT INTO "Player"(Id, FirstName, LastName, Height, Weight, Age, TeamID)
VALUES (DEFAULT, %s, %s, %s, %s, %s, (SELECT Id FROM "Team" WHERE TeamName = %s));
''', (firstname, lastname, height, weight, age, teamname))
db.commit()
return render_template("insert_player.html", title='Insert Player')
@app.route('/insert_stat', methods=['GET','POST'])
def insert_stat():
cursor.execute(
'''
SELECT pg_get_serial_sequence('"StatInfo"', 'id');
'''
)
cursor.execute(
'''
-- reset the sequence, regardless whether table has rows or not:
SELECT setval(pg_get_serial_sequence('"StatInfo"', 'id'), coalesce(max(id) + 1,1), false) FROM "StatInfo";
'''
)
db.commit()
if request.method == 'POST':
firstname =request.form['firstname']
lastname =request.form['lastname']
teamname = request.form['teamname']
game = request.form['game']
points = request.form['points']
print(points)
rebounds = request.form['rebounds']
assists = request.form['assists']
cursor.execute(
'''
INSERT INTO "StatInfo"(PlayerID, GameID, Type, StatValue)
VALUES ((SELECT "Player".Id FROM "Player" INNER JOIN "Team" ON ("Player".TeamId = "Team".Id) WHERE firstName = %s AND lastName = %s), %s, %s, %s)
''', (firstname, lastname, game[4:],'Pts', points))
cursor.execute(
'''
INSERT INTO "StatInfo"(PlayerID, GameID, Type, StatValue)
VALUES ((SELECT "Player".Id FROM "Player" INNER JOIN "Team" ON ("Player".TeamId = "Team".Id) WHERE firstName = %s AND lastName = %s), %s, %s, %s)
''', (firstname, lastname, game[4:],'Reb', rebounds))
cursor.execute(
'''
INSERT INTO "StatInfo"(PlayerID, GameID, Type, StatValue)
VALUES ((SELECT "Player".Id FROM "Player" INNER JOIN "Team" ON ("Player".TeamId = "Team".Id) WHERE firstName = %s AND lastName = %s), %s, %s, %s)
''', (firstname, lastname, game[4:],'Ast', assists))
db.commit()
return render_template("insert_stat.html", title='Insert Stat')
if __name__ == "__main__":
app.run(debug=True)