Tidying up SQL

This commit is contained in:
Jonathan Ervine 2020-12-11 10:51:21 +08:00
parent 7545f0cdce
commit c8d5c7dee4

View File

@ -20,10 +20,16 @@ basic_auth = BasicAuth(app)
@routes.route('/hkfc-d/motm/<randomUrlSuffix>')
def hkfcD_motm_vote(randomUrlSuffix):
sql = "SELECT playerNumber, playerForenames, playerSurname, playerNickname FROM _hkfcD_matchSquad ORDER BY RAND()"
sql2 = "SELECT nextClub, nextTeam, nextDate, oppoLogo, hkfcLogo, currMotM, currDotD, nextFixture FROM hkfcDAdminSettings"
rows = sql_read(sql)
nextInfo = sql_read_static(sql2)
squadPlayer_lookup = "SELECT playerNumber, playerForenames, playerSurname, playerNickname FROM _hkfcD_matchSquad ORDER BY RAND()"
settings_lookup = "SELECT nextClub, nextTeam, nextDate, oppoLogo, hkfcLogo, currMotM, currDotD, nextFixture FROM hkfcDAdminSettings"
nextFixture_lookup = "SELECT hockeyResults2020.hockeyFixtures.date, hockeyResults.hkfcDAdminSettings.nextFixture FROM hockeyResults2020.hockeyFixtures INNER JOIN hockeyResults.hkfcDAdminSettings ON hockeyResults2020.hockeyFixtures.fixtureNumber = hockeyResults.hkfcDAdminSettings.nextFixture"
motmPicture_lookup = "SELECT playerPictureURL FROM _HKFC_players INNER JOIN hockeyResults.hkfcDAdminSettings ON _HKFC_players.playerNumber=hockeyResults.hkfcDAdminSettings.currMotM"
dotdPicture_lookup = "SELECT playerPictureURL FROM _HKFC_players INNER JOIN hockeyResults.hkfcDAdminSettings ON _HKFC_players.playerNumber=hockeyResults.hkfcDAdminSettings.currDotD"
comments_lookup = "SELECT comment FROM _motmComments INNER JOIN hockeyResults.hkfcDAdminSettings ON _motmComments.matchDate=hockeyResults.hkfcDAdminSettings.nextDate ORDER BY RAND() LIMIT 1"
urlSuffix_lookup = "SELECT motmUrlSuffix FROM hockeyResults.hkfcDAdminSettings WHERE userid='admin'"
rows = sql_read(squadPlayer_lookup)
nextInfo = sql_read_static(settings_lookup)
nextClub = nextInfo[0]['nextClub']
nextTeam = nextInfo[0]['nextTeam']
nextFixture = nextInfo[0]['nextFixture']
@ -32,25 +38,20 @@ def hkfcD_motm_vote(randomUrlSuffix):
currMotM = nextInfo[0]['currMotM']
currDotD = nextInfo[0]['currDotD']
oppo = nextTeam
sql3 = "SELECT hockeyResults2020.hockeyFixtures.date, hockeyResults.hkfcDAdminSettings.nextFixture FROM hockeyResults2020.hockeyFixtures INNER JOIN hockeyResults.hkfcDAdminSettings ON hockeyResults2020.hockeyFixtures.fixtureNumber = hockeyResults.hkfcDAdminSettings.nextFixture"
nextMatchDate = sql_read(sql3)
nextMatchDate = sql_read(nextFixture_lookup)
nextDate = nextMatchDate[0]['date']
formatDate = datetime.strftime(nextDate, '%A, %d %B %Y')
sql3 = "SELECT playerPictureURL FROM _HKFC_players INNER JOIN hockeyResults.hkfcDAdminSettings ON _HKFC_players.playerNumber=hockeyResults.hkfcDAdminSettings.currMotM"
sql4 = "SELECT playerPictureURL FROM _HKFC_players INNER JOIN hockeyResults.hkfcDAdminSettings ON _HKFC_players.playerNumber=hockeyResults.hkfcDAdminSettings.currDotD"
motm = sql_read(sql3)
dotd = sql_read(sql4)
motm = sql_read(motmPicture_lookup)
dotd = sql_read(dotdPicture_lookup)
motmURL = motm[0]['playerPictureURL']
dotdURL = dotd[0]['playerPictureURL']
sql5 = "SELECT comment FROM _motmComments INNER JOIN hockeyResults.hkfcDAdminSettings ON _motmComments.matchDate=hockeyResults.hkfcDAdminSettings.nextDate ORDER BY RAND() LIMIT 1"
comment = sql_read(sql5)
comment = sql_read(comments_lookup)
if comment == "":
comment = "No comments added yet"
form = motmForm()
sql6 = "SELECT motmUrlSuffix FROM hockeyResults.hkfcDAdminSettings WHERE userid='admin'"
urlSuff = sql_read_static(sql6)
urlSuff = sql_read_static(urlSuffix_lookup)
randomSuff = urlSuff[0]['motmUrlSuffix']
if randomSuff == randomUrlSuffix:
return render_template('_hkfcDMotmVote.html', data=rows, comment=comment, formatDate=formatDate, matchNumber=nextFixture, oppo=oppo, hkfcLogo=hkfcLogo, oppoLogo=oppoLogo, dotdURL=dotdURL, motmURL=motmURL, form=form)
@ -59,8 +60,8 @@ def hkfcD_motm_vote(randomUrlSuffix):
@routes.route('/hkfc-d/comments', methods=['GET', 'POST'])
def hkfcd_match_comments():
sql = "SELECT nextClub, nextTeam, nextDate, oppoLogo, hkfcLogo FROM hkfcDAdminSettings"
row = sql_read_static(sql)
settings_lookup = "SELECT nextClub, nextTeam, nextDate, oppoLogo, hkfcLogo FROM hkfcDAdminSettings"
row = sql_read_static(settings_lookup)
# nextTeam already seems to include all the team+club details
# _oppo = row[0]['nextClub'] + " " + row[0]['nextTeam']
_oppo = row[0]['nextClub']
@ -68,24 +69,28 @@ def hkfcd_match_comments():
_matchDate = row[0]['nextDate'].strftime('%Y_%m_%d')
hkfcLogo = row[0]['hkfcLogo']
oppoLogo = row[0]['oppoLogo']
comment_insert = "INSERT INTO _motmComments (matchDate, opposition, comment) VALUES ('" + commentDate + "', '" + _oppo + "', '" + _fixed_comment + "')"
comment_lookup = "SELECT comment FROM _motmComments WHERE matchDate='" + _matchDate + "' ORDER BY RAND()"
if request.method == 'POST':
_comment = request.form['matchComment']
if _comment != 'Optional comments added here':
_fixed_comment = _comment.replace("'", "\\'")
sql3 = "INSERT INTO _motmComments (matchDate, opposition, comment) VALUES ('" + commentDate + "', '" + _oppo + "', '" + _fixed_comment + "')"
sql_write(sql3)
sql = "SELECT comment FROM _motmComments WHERE matchDate='" + _matchDate + "' ORDER BY RAND()"
comments = sql_read(sql)
sql_write(comment_insert)
comments = sql_read(comment_lookup)
return render_template('_hkfcDMatchComments.html', comments=comments, hkfcLogo=hkfcLogo, oppoLogo=oppoLogo)
@routes.route('/hkfc-d/statAdmin', methods=['GET', 'POST'])
def hkfc_d_stats_admin():
form = goalsAssistsForm()
sql = "SELECT date, homeTeam, awayTeam, venue, fixtureNumber FROM hockeyFixtures WHERE homeTeam='HKFC D' OR awayTeam='HKFC D'"
matches = sql_read(sql)
fixtures_lookup = "SELECT date, homeTeam, awayTeam, venue, fixtureNumber FROM hockeyFixtures WHERE homeTeam='HKFC D' OR awayTeam='HKFC D'"
squadPlayer_lookup = "SELECT playerNumber, playerNickname FROM _hkfcD_matchSquad"
matches = sql_read(fixtures_lookup)
form.match.choices = [(match['fixtureNumber'], match['date']) for match in matches]
sql2 = "SELECT playerNumber, playerNickname FROM _hkfcD_matchSquad"
players = sql_read(sql2)
players = sql_read(squadPlayer_lookup)
return render_template('_goalsAssistsAdmin.html', data=players, form=form)
@routes.route('/hkfc-d/goalsAssistsSubmit', methods=['POST'])
@ -97,9 +102,10 @@ def goalsAssistsSubmit():
assists = request.form.getlist('assists')
goals = request.form.getlist('goals')
match = request.form['match']
playerGoalsAssists_update = "INSERT INTO _hkfc_d_motm (playerNumber, playerName, assistsTotal, goalsTotal, assists_" + match + ", goals_" + match + ") SELECT playerNumber, playerNickname, '" + assists[idx] + "', '" + goals[idx] + "', '" + assists[idx] + "', '" + goals[idx] + "' FROM _HKFC_players WHERE playerNumber='" + player + "' ON DUPLICATE KEY UPDATE assistsTotal = assistsTotal + " + assists[idx] + ", goalsTotal = goalsTotal + " + goals[idx] + ", assists_" + match + " = " + assists[idx] + ", goals_" + match + " = " + goals[idx] + ""
for idx, player in enumerate(playerNumber):
sql = "INSERT INTO _hkfc_d_motm (playerNumber, playerName, assistsTotal, goalsTotal, assists_" + match + ", goals_" + match + ") SELECT playerNumber, playerNickname, '" + assists[idx] + "', '" + goals[idx] + "', '" + assists[idx] + "', '" + goals[idx] + "' FROM _HKFC_players WHERE playerNumber='" + player + "' ON DUPLICATE KEY UPDATE assistsTotal = assistsTotal + " + assists[idx] + ", goalsTotal = goalsTotal + " + goals[idx] + ", assists_" + match + " = " + assists[idx] + ", goals_" + match + " = " + goals[idx] + ""
sql_write(sql)
sql_write(playerGoalsAssists_update)
except Exception as e:
print(e)
finally: