-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
778 lines (650 loc) · 33.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
from flask import Flask, render_template, json, redirect
from flask_mysqldb import MySQL
from flask import request
from dotenv import load_dotenv
import pymysql
import os
load_dotenv()
# Configuration
app = Flask(__name__)
app = Flask(__name__, static_url_path='/static')
# database connection
# Template:
# app.config["MYSQL_HOST"] = "classmysql.engr.oregonstate.edu"
# app.config["MYSQL_USER"] = "cs340_OSUusername"
# app.config["MYSQL_PASSWORD"] = "XXXX" | last 4 digits of OSU id
# app.config["MYSQL_DB"] = "cs340_OSUusername"
# app.config["MYSQL_CURSORCLASS"] = "DictCursor"
# database connection info
# app.config["MYSQL_HOST"] = "classmysql.engr.oregonstate.edu"
# app.config["MYSQL_USER"] = ""
# app.config["MYSQL_PASSWORD"] = ""
# app.config["MYSQL_DB"] = ""
# app.config["MYSQL_CURSORCLASS"] = "DictCursor"
# mysql = MySQL(app)
app.config["MYSQL_HOST"] = os.getenv("db_url")
app.config["MYSQL_USER"] = os.getenv("user")
app.config["MYSQL_PASSWORD"] = os.getenv("password")
app.config["MYSQL_DB"] = os.getenv("db_name")
app.config["MYSQL_CURSORCLASS"] = "DictCursor"
mysql = MySQL(app)
timeout = 10
mysql = pymysql.connect(
charset="utf8mb4",
connect_timeout=timeout,
cursorclass=pymysql.cursors.DictCursor,
db=app.config["MYSQL_DB"],
host=app.config["MYSQL_HOST"],
password=app.config["MYSQL_PASSWORD"],
read_timeout=timeout,
port=16478,
user=app.config["MYSQL_USER"],
write_timeout=timeout,
)
# Routes
@app.route('/index')
def home():
return render_template("home.j2")
@app.route('/')
def index():
return render_template("home.j2")
####################################
## TOURNAMENTS FUNCTIONS ##
####################################
# route for tournaments page
# Citation for the following function
# Adapted from cs340-flask-starter-app
# Modified variables and queries to fit requirements needed for our Tournaments table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/tournaments", methods=["GET", "POST"])
def tournaments():
if request.method == "POST" and "tournamentName" in request.form:
# grab inputs from form
name = request.form["tournamentName"]
location = request.form["location"]
startDate = request.form["startDate"]
endDate = request.form["endDate"]
# query to insert form values into database
query = "INSERT INTO Tournaments (tournamentName, location, startDate, endDate) VALUES (%s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (name, location, startDate, endDate))
mysql.commit()
# redirect back to tournament page
return redirect("/tournaments")
elif request.method == "POST" and "tournamentID" in request.form:
# grab inputs from form
tournament = request.form["tournamentID"]
game = request.form["gameID"]
# query to insert form values
query = "INSERT into Tournaments_has_Games (tournamentID, gameID) VALUES (%s, %s)"
cur = mysql.cursor()
cur.execute(query, (tournament, game))
mysql.commit()
# redirect back to tournament page
return redirect("/tournaments")
if request.method == "GET":
# mySQL query to grab all the tournaments
query = "SELECT Tournaments.tournamentID, Tournaments.tournamentName, Tournaments.location, Tournaments.startDate, Tournaments.endDate from Tournaments;"
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# mySQL query to grab all the games
query = "SELECT Games.gameID, Games.title FROM Games;"
cur = mysql.cursor()
cur.execute(query)
games = cur.fetchall()
# mySQL query to grab all the tournaments and their games
query = "SELECT Tournaments.tournamentID, Tournaments.tournamentName as 'Name', Games.gameID, Games.title as 'Title' FROM Tournaments INNER JOIN Tournaments_has_Games ON Tournaments.tournamentID = Tournaments_has_Games.tournamentID INNER JOIN Games ON Games.gameID = Tournaments_has_Games.gameID ORDER BY Tournaments.tournamentID ASC;"
cur = mysql.cursor()
cur.execute(query)
intersection = cur.fetchall()
return render_template("tournaments.j2", data=data, intersection=intersection, games=games)
# route for delete functionality
# Citation for the following function
# Adapted from flask-starter-app > bsg_people code
# Modified variables and queries to fit requirements needed for Tournaments table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/delete_tournament/<int:id>")
def delete_tournament(id):
# mySQL query to delete the person with our passed id
query = "DELETE FROM Tournaments WHERE tournamentID = '%s';"
cur = mysql.cursor()
cur.execute(query, (id,))
mysql.commit()
# redirect back to people page
return redirect("/tournaments")
# route for edit functionality
# Citation for the following function
# Adapted from flask-starter-app code
# Modified variables and queries to select desired data from Tournaments database
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/edit_tournament/<int:id>", methods=["POST", "GET"])
def edit_tournament(id):
if request.method == "GET":
# mySQL query to grab the info of the player with passed id
query = "SELECT Tournaments.tournamentID, Tournaments.tournamentName, Tournaments.location, Tournaments.startDate, Tournaments.endDate FROM Tournaments where tournamentID = %s" % (id)
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# render edit_tournament page passing our query data, teams data, and games data to the edit_tournament template
return render_template("edit_tournament.j2", data=data)
# meat and potatoes of our update functionality
if request.method == "POST":
# fire off if user clicks the 'Edit Tournament' button
if request.form.get("Edit_Tournament"):
# grab user form inputs
id = request.form["tournamentID"]
name = request.form["tournamentName"]
location = request.form["location"]
startDate = request.form["startDate"]
endDate = request.form["endDate"]
# no null inputs allowed in Tournaments
query = "UPDATE Tournaments SET Tournaments.tournamentName = %s, Tournaments.location = %s, Tournaments.startDate = %s, Tournaments.endDate = %s WHERE Tournaments.tournamentID = %s"
cur = mysql.cursor()
cur.execute(query, (name, location, startDate, endDate, id))
mysql.commit()
# redirect back to players page after we execute the update query
return redirect("/tournaments")
# route for delete functionality
# Citation for the following function
# Adapted from flask-starter-app > bsg_people code
# Modified variables and queries to fit requirements needed for interesection table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/delete_tournament_game", methods=["POST"])
def delete_tournament_game():
# grab hidden inputs
gameID = request.form["gameID"]
tournamentID = request.form["tournamentID"]
# mySQL query to delete the tournament-game with our passed id
query = "DELETE FROM Tournaments_has_Games WHERE Tournaments_has_Games.gameID = %s AND Tournaments_has_Games.tournamentID = %s;"
cur = mysql.cursor()
cur.execute(query, (gameID, tournamentID))
mysql.commit()
# redirect back to tournaments page
return redirect("/tournaments")
# route for edit functionality
# Citation for the following function
# Adapted from flask-starter-app code
# Modified variables and queries to select desired data from intersection database
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/edit_tournament_game/<int:id><int:id2>", methods=["POST", "GET"])
def edit_tournament_game(id, id2):
if request.method == "GET":
# mySQL query to grab the info of the tournament-game with passed id
cur = mysql.cursor()
query = "SELECT Tournaments.tournamentID, Tournaments.tournamentName, Games.gameID, Games.title FROM Tournaments INNER JOIN Tournaments_has_Games ON Tournaments.tournamentID = Tournaments_has_Games.tournamentID INNER JOIN Games ON Games.gameID = Tournaments_has_Games.gameID WHERE Tournaments_has_Games.gameID = %s AND Tournaments_has_Games.tournamentID = %s"
cur.execute(query, (id, id2))
data = cur.fetchall()
## mySQL query to grab game id/title data for our dropdown
query2 = "SELECT gameID, title FROM Games"
cur = mysql.cursor()
cur.execute(query2)
games_data = cur.fetchall()
#mysql query to grab tournament id/name data for dropdown
query3 = "SELECT tournamentID, tournamentName FROM Tournaments"
cur = mysql.cursor()
cur.execute(query3)
tournaments_data = cur.fetchall()
# render edit_tournament_game page passing our query data, teams data, and games data to the edit_tournament_game template
return render_template("edit_tournament_game.j2", data=data, games=games_data, tournaments=tournaments_data)
# meat and potatoes of our update functionality
if request.method == "POST":
# fire off if user clicks the 'Edit Tournament Game' button
if request.form.get("Edit_Tournament_Game"):
# grab user form inputs
tournamentID = request.form["tournamentID"]
gameID = request.form["gameID"]
query = "UPDATE Tournaments_has_Games SET tournamentID = %s, gameID = %s WHERE tournamentID = %s and gameID = %s"
cur = mysql.cursor()
cur.execute(query, (tournamentID, gameID, id2, id))
mysql.commit()
# redirect back to tournaments page after we execute the update query
return redirect("/tournaments")
####################################
## GAMES FUNCTIONS ##
####################################
# route for games page
# Citation for the following function
# Adapted from cs340-flask-starter-app
# Modified variables and queries to fit requirements needed for our Games table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/games", methods=["GET", "POST"])
def games():
if request.method == "POST":
# grab inputs from form
title = request.form["title"]
genre = request.form["genre"]
platform = request.form["platform"]
# query to insert form values into database
query = "INSERT INTO Games (title, genre, platform) VALUES (%s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (title, genre, platform))
mysql.commit()
# redirect back to tournament page
return redirect("/games")
if request.method == "GET":
# mySQL query to grab all the games
query = "SELECT Games.gameID, Games.title, Games.genre, Games.platform FROM Games;"
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
return render_template("games.j2", data=data)
# route for delete functionality
# Citation for the following function
# Adapted from flask-starter-app > bsg_people code
# Modified variables and queries to fit requirements needed for Games table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/delete_game/<int:id>")
def delete_game(id):
# mySQL query to delete the game with our passed id
query = "DELETE FROM Games WHERE gameID = '%s';"
cur = mysql.cursor()
cur.execute(query, (id,))
mysql.commit()
# redirect back to people page
return redirect("/games")
# route for edit functionality
# Citation for the following function
# Adapted from flask-starter-app code
# Modified variables and queries to select desired data from Games database
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/edit_game/<int:id>", methods=["POST", "GET"])
def edit_game(id):
if request.method == "GET":
# mySQL query to grab the info of the game with passed id
query = "SELECT Games.gameID, Games.title, Games.genre, Games.platform FROM Games where gameID = %s" % (id)
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# render edit_game page passing our query data, teams data, and games data to the edit_game template
return render_template("edit_game.j2", data=data)
# meat and potatoes of our update functionality
if request.method == "POST":
# fire off if user clicks the 'Edit Game' button
if request.form.get("Edit_Game"):
# grab user form inputs
id = request.form["gameID"]
title = request.form["title"]
genre = request.form["genre"]
platform = request.form["platform"]
# no null inputs allowed in Games
query = "UPDATE Games SET Games.title = %s, Games.genre = %s, Games.platform = %s WHERE Games.gameID = %s"
cur = mysql.cursor()
cur.execute(query, (title, genre, platform, id))
mysql.commit()
# redirect back to games page after we execute the update query
return redirect("/games")
####################################
## TEAMS FUNCTIONS ##
####################################
# route for teams page
# Citation for the following function
# Adapted from cs340-flask-starter-app
# Modified variables and queries to fit requirements needed for our Teams table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/teams", methods=["GET", 'POST'])
def teams():
if request.method == "POST":
# grab inputs from form
if request.form.get("addTeam"):
teamName = request.form["teamName"]
location = request.form["location"]
sponsor = request.form["sponsor"]
tournament = request.form["tournament"]
description = request.form["description"]
# account for combinations of null sponsor or null tournament
if sponsor == "0" and tournament == "0":
query = "INSERT INTO Teams (teamName, location, description) VALUES (%s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (teamName, location, description))
mysql.commit()
elif sponsor == "0":
query = "INSERT INTO Teams (teamName, location, tournamentID, description) VALUES (%s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (teamName, location, tournament, description))
mysql.commit()
elif tournament == "0":
query = "INSERT INTO Teams (teamName, location, sponsorID, description) VALUES (%s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (teamName, location, sponsor, description))
mysql.commit()
else:
query = "INSERT INTO Teams (teamName, location, sponsorID, tournamentID, description) VALUES (%s, %s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (teamName, location, sponsor, tournament, description))
mysql.commit()
# redirect back to teams page
return redirect("/teams")
if request.method == "GET":
# mySQL query to grab all the teams
query = "SELECT Teams.teamID, Teams.teamName, Teams.location, IFNULL(Sponsors.sponsorName, '*No Sponsor*') as sponsor, IFNULL(Tournaments.tournamentName, '*No Tournament*') as tournament, Teams.description FROM Teams LEFT JOIN Sponsors ON Sponsors.sponsorID = Teams.sponsorID LEFT JOIN Tournaments ON Tournaments.tournamentID = Teams.tournamentID;"
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# mySQL query to grab sponsor id/name data for our dropdown
query2 = "SELECT sponsorID, sponsorName FROM Sponsors"
cur = mysql.cursor()
cur.execute(query2)
sponsors_data = cur.fetchall()
# mySQL query to grab tournament id/name data for our dropdown
query3 = "SELECT tournamentID, tournamentName FROM Tournaments"
cur = mysql.cursor()
cur.execute(query3)
tournaments_data = cur.fetchall()
# send data to populate drop down boxes
return render_template("teams.j2", data=data, sponsors=sponsors_data, tournaments=tournaments_data)
# route for delete functionality
# Citation for the following function
# Adapted from flask-starter-app > bsg_people code
# Modified variables and queries to fit requirements needed for Teams table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/delete_team/<int:id>")
def delete_team(id):
# mySQL query to delete the team with our passed id
query = "DELETE FROM Teams WHERE teamID = '%s';"
cur = mysql.cursor()
cur.execute(query, (id,))
mysql.commit()
# redirect back to teams page
return redirect("/teams")
# route for edit functionality
# Citation for the following function
# Adapted from flask-starter-app code
# Modified variables and queries to select desired data from Teams database
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/edit_team/<int:id>", methods=["POST", "GET"])
def edit_team(id):
if request.method == "GET":
# mySQL query to grab the info of the teams with passed id
query = "SELECT Teams.teamID, Teams.teamName, Teams.location, IFNULL(Sponsors.sponsorName, '*No Sponsor*') as sponsor, IFNULL(Tournaments.tournamentName, '*No Tournament*') as tournament, Teams.description FROM Teams LEFT JOIN Sponsors ON Sponsors.sponsorID=Teams.sponsorID LEFT JOIN Tournaments ON Tournaments.tournamentID=Teams.tournamentID WHERE teamID = %s" % (id)
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# mySQL query to grab sponsor id/name data for our dropdown
query2 = "SELECT sponsorID, sponsorName FROM Sponsors"
cur = mysql.cursor()
cur.execute(query2)
sponsors_data = cur.fetchall()
#mysql query to grab tournament id/name data for dropdown
query3 = "SELECT tournamentID, tournamentName FROM Tournaments"
cur = mysql.cursor()
cur.execute(query3)
tournaments_data = cur.fetchall()
# render edit_team page passing our query data, sponsors data, and tournaments data to the edit_team template
return render_template("edit_team.j2", data=data, sponsors=sponsors_data, tournaments=tournaments_data)
# meat and potatoes of our update functionality
if request.method == "POST":
# fire off if user clicks the 'Edit Player' button
if request.form.get("Edit_Team"):
# grab user form inputs
id = request.form["teamID"]
teamName = request.form["teamName"]
location = request.form["location"]
sponsor = request.form["sponsor"]
tournament = request.form["tournament"]
description = request.form["description"]
# account for null sponsor, tournament, and description
if (sponsor == "0") and (tournament == "0") and (description == "0"):
# mySQL query to update the attributes of person with our passed id value
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = NULL, Teams.tournamentID = NULL, Teams.description = NULL WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, id))
mysql.commit()
# account for null tournament and description
elif (tournament == "0") and (description == "0"):
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = %s, Teams.tournamentID = NULL, Teams.description = NULL WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, sponsor, id))
mysql.commit()
# account for null sponsor and description
elif (sponsor == "0") and (description == "0"):
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = NULL, Teams.tournamentID = %s, Teams.description = NULL WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, tournament, id))
mysql.commit()
# account for null sponsor and tournament
elif (sponsor == "0") and (tournament == "0"):
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = NULL, Teams.tournamentID = NULL, Teams.description = %s WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, description, id))
mysql.commit()
# account for null tournament
elif (tournament == "0"):
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = %s, Teams.tournamentID = NULL, Teams.description = %s WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, sponsor, description, id))
mysql.commit()
# account for null sponsor
elif (sponsor == "0"):
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = NULL, Teams.tournamentID = %s, Teams.description = %s WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, tournament, description, id))
mysql.commit()
# account for null description
elif (description == "0"):
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = %s, Teams.tournamentID = %s, Teams.description = NULL WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, sponsor, tournament, id))
mysql.commit()
# no null inputs
else:
query = "UPDATE Teams SET Teams.teamName = %s, Teams.location = %s, Teams.sponsorID = %s, Teams.tournamentID = %s, Teams.description = %s WHERE Teams.teamID = %s"
cur = mysql.cursor()
cur.execute(query, (teamName, location, sponsor, tournament, description, id))
mysql.commit()
# redirect back to teams page after we execute the update query
return redirect("/teams")
####################################
## PLAYERS FUNCTIONS ##
####################################
# route for players page
# Citation for the following function
# Adapted from cs340-flask-starter-app
# Modified variables and queries to fit requirements needed for our Players table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/players", methods=["GET", "POST"])
def players():
if request.method == "POST":
# grab inputs from form
if request.form.get("addPlayer"):
name = request.form["name"]
username = request.form["username"]
birthdate = request.form["birthdate"]
captain = request.form["captain"]
team = request.form["team"]
game = request.form["game"]
# account for combinations of null team or null game
if team == "0" and game == "0":
query = "INSERT INTO Players (name, username, birthdate, captain) VALUES (%s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (name, username, birthdate, captain))
mysql.commit()
elif team == "0":
query = "INSERT INTO Players (name, username, birthdate, captain, gameID) VALUES (%s, %s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (name, username, birthdate, captain, game))
mysql.commit()
elif game == "0":
query = "INSERT INTO Players (name, username, birthdate, captain, teamID) VALUES (%s, %s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (name, username, birthdate, captain, team))
mysql.commit()
else:
query = "INSERT INTO Players (name, username, birthdate, captain, teamID, gameID) VALUES (%s, %s, %s, %s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (name, username, birthdate, captain, team, game))
mysql.commit()
# redirect back to player page
return redirect("/players")
if request.method == "GET":
# mySQL query to grab all the players
query = "SELECT Players.playerID, Players.name, Players.username, Players.birthdate, Players.captain, IFNULL(Teams.teamName, '*No Team*') as team, IFNULL(Games.title, '*No Game*') as game FROM Players LEFT JOIN Teams ON Teams.teamID=Players.teamID LEFT JOIN Games ON Games.gameID=Players.gameID;"
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# mySQL query to grab team id/name data for our dropdown
query2 = "SELECT teamID, teamName FROM Teams"
cur = mysql.cursor()
cur.execute(query2)
teams_data = cur.fetchall()
# mySQL query to grab game id/name data for our dropdown
query3 = "SELECT gameID, title FROM Games"
cur = mysql.cursor()
cur.execute(query3)
games_data = cur.fetchall()
# send data to populate drop down boxes
return render_template("players.j2", data=data, teams=teams_data, games=games_data)
# route for delete functionality
# Citation for the following function
# Adapted from flask-starter-app > bsg_people code
# Modified variables and queries to fit requirements needed for Players database
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/delete_player/<int:id>")
def delete_player(id):
# mySQL query to delete the player with our passed id
query = "DELETE FROM Players WHERE playerID = '%s';"
cur = mysql.cursor()
cur.execute(query, (id,))
mysql.commit()
# redirect back to players page
return redirect("/players")
# route for edit functionality
# Citation for the following function
# Adapted from flask-starter-app code
# Modified variables and queries to select desired data from Players database
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/edit_player/<int:id>", methods=["POST", "GET"])
def edit_player(id):
if request.method == "GET":
# mySQL query to grab the info of the player with passed id
query = "SELECT Players.playerID, Players.name, Players.username, Players.birthdate, Players.captain, IFNULL(Teams.teamName, '*No Team*') as team, IFNULL(Games.title, '*No Game*') as game FROM Players LEFT JOIN Teams ON Teams.teamID=Players.teamID LEFT JOIN Games ON Games.gameID=Players.gameID WHERE playerID = %s" % (id)
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# mySQL query to grab team id/name data for our dropdown
query2 = "SELECT teamID, teamName FROM Teams"
cur = mysql.cursor()
cur.execute(query2)
teams_data = cur.fetchall()
#mysql query to grab game id/name data for dropdown
query3 = "SELECT gameID, title FROM Games"
cur = mysql.cursor()
cur.execute(query3)
games_data = cur.fetchall()
# render edit_player page passing our query data, teams data, and games data to the edit_player template
return render_template("edit_player.j2", data=data, teams=teams_data, games=games_data)
# meat and potatoes of our update functionality
if request.method == "POST":
# fire off if user clicks the 'Edit Player' button
if request.form.get("Edit_Player"):
# grab user form inputs
id = request.form["playerID"]
name = request.form["name"]
username = request.form["username"]
captain = request.form["captain"]
team = request.form["team"]
game = request.form["game"]
# account for null game AND team
if (game == "0") and team == "0":
# mySQL query to update the attributes of person with our passed id value
query = "UPDATE Players SET Players.name = %s, Players.username = %s, Players.captain = %s, Players.teamID = NULL, Players.gameID = NULL WHERE Players.playerID = %s"
cur = mysql.cursor()
cur.execute(query, (name, username, captain, id))
mysql.commit()
# account for null team
elif team == "0":
query = "UPDATE Players SET Players.name = %s, Players.username = %s, Players.captain = %s, Players.teamID = NULL, Players.gameID = %s WHERE Players.playerID = %s"
cur = mysql.cursor()
cur.execute(query, (name, username, captain, game, id))
mysql.commit()
# account for null game
elif game == "" or game == "None" or game == 0:
query = "UPDATE Players SET Players.name = %s, Players.username = %s, Players.captain = %s, Players.teamID = %s, Players.gameID = NULL WHERE Players.playerID = %s"
cur = mysql.cursor()
cur.execute(query, (name, username, captain, team, id))
mysql.commit()
# no null inputs
else:
query = "UPDATE Players SET Players.name = %s, Players.username = %s, Players.captain = %s, Players.teamID = %s, Players.gameID = %s WHERE Players.playerID = %s"
cur = mysql.cursor()
cur.execute(query, (name, username, captain, team, game, id))
mysql.commit()
# redirect back to players page after we execute the update query
return redirect("/players")
####################################
## SPONSORS FUNCTIONS ##
####################################
# route for sponsors page
# Citation for the following function
# Adapted from cs340-flask-starter-app
# Modified variables and queries to fit requirements needed for our Sponsors table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/sponsors", methods=["GET", "POST"])
def sponsors():
if request.method == "POST":
# grab inputs from form
sponsor = request.form["sponsorName"]
contactPerson = request.form["contactPerson"]
contactEmail = request.form["contactEmail"]
# query to insert form values into database
query = "INSERT INTO Sponsors (sponsorName, contactPerson, contactEmail) VALUES (%s, %s, %s)"
cur = mysql.cursor()
cur.execute(query, (sponsor, contactPerson, contactEmail))
mysql.commit()
# redirect back to sponsors page
return redirect("/sponsors")
if request.method == "GET":
# mySQL query to grab all the sponsors
query = "SELECT Sponsors.sponsorID, Sponsors.sponsorName, Sponsors.contactPerson, Sponsors.contactEmail from Sponsors;"
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
return render_template("sponsors.j2", data=data)
# route for delete functionality
# Citation for the following function
# Adapted from flask-starter-app > bsg_people code
# Modified variables and queries to fit requirements needed for Sponsors table
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/delete_sponsor/<int:id>")
def delete_sponsor(id):
# mySQL query to delete the sponsor with our passed id
query = "DELETE FROM Sponsors WHERE sponsorID = '%s';"
cur = mysql.cursor()
cur.execute(query, (id,))
mysql.commit()
# redirect back to sponsors page
return redirect("/sponsors")
# route for edit functionality
# Citation for the following function
# Adapted from flask-starter-app code
# Modified variables and queries to select desired data from Sponsors database
# Source URL: https://github.com/osu-cs340-ecampus/flask-starter-app
@app.route("/edit_sponsor/<int:id>", methods=["POST", "GET"])
def edit_sponsor(id):
if request.method == "GET":
# mySQL query to grab the info of the sponsor with passed id
query = "SELECT Sponsors.sponsorID, Sponsors.sponsorName, Sponsors.contactPerson, Sponsors.contactEmail FROM Sponsors where sponsorID = %s" % (id)
cur = mysql.cursor()
cur.execute(query)
data = cur.fetchall()
# render edit_sponsor page passing our query data, teams data, and games data to the edit_sponsor template
return render_template("edit_sponsor.j2", data=data)
# meat and potatoes of our update functionality
if request.method == "POST":
# fire off if user clicks the 'Edit Sponsor' button
if request.form.get("Edit_Sponsor"):
# grab user form inputs
id = request.form["sponsorID"]
name = request.form["sponsorName"]
contactPerson = request.form["contactPerson"]
email = request.form["contactEmail"]
# no null inputs allowed in Sponsors
query = "UPDATE Sponsors SET Sponsors.sponsorName = %s, Sponsors.contactPerson = %s, Sponsors.contactEmail = %s WHERE Sponsors.sponsorID = %s"
cur = mysql.cursor()
cur.execute(query, (name, contactPerson, email, id))
mysql.commit()
# redirect back to sponsors page after we execute the update query
return redirect("/sponsors")
# Listener
if __name__ == "__main__":
app.run(port=3000, debug=True)