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