354 lines
17 KiB
Python
354 lines
17 KiB
Python
#import MySQLdb
|
|
import pymysql
|
|
import os
|
|
import json
|
|
import datetime
|
|
from datetime import datetime
|
|
from flask import render_template, request, jsonify, flash
|
|
from flask_basicauth import BasicAuth
|
|
from app import app, randomUrlSuffix
|
|
from readSettings import mySettings
|
|
from dbWrite import sql_write, sql_write_static, sql_read, sql_read_static
|
|
from tables import matchSquadTable, convenorFixtureList
|
|
from forms import adminSettingsForm, motmForm, goalsAssistsForm, adminSettingsForm2
|
|
from . import routes
|
|
|
|
app.config['BASIC_AUTH_USERNAME'] = 'admin'
|
|
app.config['BASIC_AUTH_PASSWORD'] = 'letmein'
|
|
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)
|
|
nextClub = nextInfo[0]['nextClub']
|
|
nextTeam = nextInfo[0]['nextTeam']
|
|
nextFixture = nextInfo[0]['nextFixture']
|
|
hkfcLogo = nextInfo[0]['hkfcLogo']
|
|
oppoLogo = nextInfo[0]['oppoLogo']
|
|
currMotM = nextInfo[0]['currMotM']
|
|
currDotD = nextInfo[0]['currDotD']
|
|
oppo = nextTeam
|
|
sql3 = "SELECT 2019_hockeyResults.hockeyFixtures.date, hockeyResults.hkfcDAdminSettings.nextFixture FROM 2019_hockeyResults.hockeyFixtures INNER JOIN hockeyResults.hkfcDAdminSettings ON 2019_hockeyResults.hockeyFixtures.fixtureNumber = hockeyResults.hkfcDAdminSettings.nextFixture"
|
|
nextMatchDate = sql_read(sql3)
|
|
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)
|
|
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)
|
|
if comment == "":
|
|
comment = "No comments added yet"
|
|
form = motmForm()
|
|
sql6 = "SELECT motmUrlSuffix FROM hockeyResults.hkfcDAdminSettings WHERE userid='admin'"
|
|
urlSuff = sql_read_static(sql6)
|
|
randomSuff = urlSuff[0]['motmUrlSuffix']
|
|
print(randomSuff)
|
|
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)
|
|
else:
|
|
return render_template('_error.html')
|
|
|
|
@routes.route('/hkfc-d/comments', methods=['GET', 'POST'])
|
|
def hkfcd_match_comments():
|
|
sql = "SELECT nextClub, nextTeam, nextDate, oppoLogo, hkfcLogo FROM hkfcDAdminSettings"
|
|
row = sql_read(sql)
|
|
_oppo = row[0]['nextClub'] + " " + row[0]['nextTeam']
|
|
commentDate = row[0]['nextDate'].strftime('%Y-%m-%d')
|
|
_matchDate = row[0]['nextDate'].strftime('%Y_%m_%d')
|
|
hkfcLogo = row[0]['hkfcLogo']
|
|
oppoLogo = row[0]['oppoLogo']
|
|
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)
|
|
return render_template('_hkfcDMatchComments.html', comments=comments, hkfcLogo=hkfcLogo, oppoLogo=oppoLogo)
|
|
|
|
@routes.route('/hkfc-d/statAdmin', methods=['GET', 'POST'])
|
|
@basic_auth.required
|
|
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)
|
|
form.match.choices = [(match['fixtureNumber'], match['date']) for match in matches]
|
|
sql2 = "SELECT playerNumber, playerNickname FROM _hkfcD_matchSquad"
|
|
players = sql_read(sql2)
|
|
return render_template('_goalsAssistsAdmin.html', data=players, form=form)
|
|
|
|
@routes.route('/hkfc-d/goalsAssistsSubmit', methods=['POST'])
|
|
@basic_auth.required
|
|
def goalsAssistsSubmit():
|
|
try:
|
|
data = request.form
|
|
playerName = request.form.getlist('playerName')
|
|
playerNumber = request.form.getlist('playerNumber')
|
|
assists = request.form.getlist('assists')
|
|
goals = request.form.getlist('goals')
|
|
match = request.form['match']
|
|
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)
|
|
except Exception as e:
|
|
print(e)
|
|
finally:
|
|
return render_template('_hkfcDGoalsThanks.html', data=data)
|
|
|
|
|
|
@routes.route('/hkfc-d/motmAdmin', methods=['GET', 'POST'])
|
|
@basic_auth.required
|
|
def hkfcDMotmAdmin():
|
|
form = adminSettingsForm2()
|
|
prevFixture = mySettings('prevFixture')
|
|
if prevFixture is None:
|
|
prevFixture = '1'
|
|
else:
|
|
prevFixture = str(prevFixture)
|
|
if request.method == 'POST':
|
|
if form.saveButton.data:
|
|
print('Saved')
|
|
else:
|
|
print('Activated')
|
|
_nextTeam = request.form['nextOppoTeam']
|
|
_nextFixture = request.form['nextMatch']
|
|
_currMotM = request.form['currMotM']
|
|
_currDotD = request.form['currDotD']
|
|
sql1 = "SELECT club FROM _clubTeams WHERE displayName='" + _nextTeam + "'"
|
|
_nextClubName = sql_read_static(sql1)
|
|
_nextClub = _nextClubName[0]['club']
|
|
sql = "UPDATE hkfcDAdminSettings SET nextFixture='" + _nextFixture + "', nextClub='" + _nextClub + "', nextTeam='" + _nextTeam + "', currMotM=" + _currMotM + ", currDotD=" + _currDotD + ""
|
|
sql_write_static(sql)
|
|
sql2 = "UPDATE hkfcDAdminSettings INNER JOIN mensHockeyClubs ON hkfcDAdminSettings.nextClub = mensHockeyClubs.hockeyClub SET hkfcDAdminSettings.oppoLogo = mensHockeyClubs.logoURL WHERE mensHockeyClubs.hockeyClub='" + _nextClub + "'"
|
|
sql_write_static(sql2)
|
|
if form.saveButton.data:
|
|
flash('Settings saved!')
|
|
urlSuffix = randomUrlSuffix(8)
|
|
print(urlSuffix)
|
|
sql3 = "UPDATE hkfcDAdminSettings SET motmUrlSuffix='" + urlSuffix + "' WHERE userid='admin'"
|
|
sql_write_static(sql3)
|
|
flash('MotM URL https://hockey.ervine.cloud/hkfc-d/motm/'+urlSuffix)
|
|
elif form.activateButton.data:
|
|
sql4 = "ALTER TABLE _hkfc_d_motm ADD COLUMN motm_" + _nextFixture + " smallint DEFAULT 0, ADD COLUMN dotd_" + _nextFixture + " smallint DEFAULT 0, ADD COLUMN assists_" + _nextFixture + " smallint DEFAULT 0, ADD COLUMN goals_" + _nextFixture + " smallint DEFAULT 0 "
|
|
sql_write(sql4)
|
|
sql5 = "SELECT motmUrlSuffix FROM hkfcDAdminSettings WHERE userid='admin'"
|
|
tempSuffix = sql_read_static(sql5)
|
|
currSuffix = tempSuffix[0]['motmUrlSuffix']
|
|
print(currSuffix)
|
|
flash('Man of the Match vote is now activated')
|
|
flash('MotM URL https://hockey.ppspone.cloud/hkfc-d/motm/'+currSuffix)
|
|
else:
|
|
flash('Something went wrong - check with Smithers')
|
|
|
|
sql7 = "SELECT date, homeTeam, awayTeam, venue, fixtureNumber FROM hockeyFixtures WHERE homeTeam='HKFC D' OR awayTeam='HKFC D'"
|
|
matches = sql_read(sql7)
|
|
form.nextMatch.choices = [(match['fixtureNumber'], match['date']) for match in matches]
|
|
sql4 = "SELECT hockeyClub FROM mensHockeyClubs ORDER BY hockeyClub"
|
|
sql5 = "SELECT nextClub, oppoLogo FROM hkfcDAdminSettings"
|
|
sql6 = "SELECT playerNumber, playerForenames, playerSurname FROM _hkfcD_matchSquad_" + prevFixture + " ORDER BY playerForenames"
|
|
clubs = sql_read_static(sql4)
|
|
settings = sql_read_static(sql5)
|
|
players = sql_read(sql6)
|
|
form.nextOppoClub.choices = [(oppo['hockeyClub'], oppo['hockeyClub']) for oppo in clubs]
|
|
form.currMotM.choices = [(player['playerNumber'], player['playerForenames'] + " " + player['playerSurname']) for player in players]
|
|
form.currDotD.choices = [(player['playerNumber'], player['playerForenames'] + " " + player['playerSurname']) for player in players]
|
|
clubLogo = settings[0]['oppoLogo']
|
|
|
|
return render_template('_hkfcDMotmAdmin.html', form=form, nextOppoLogo=clubLogo)
|
|
|
|
|
|
@routes.route('/hkfc-d/motmAdmin/<club>')
|
|
def hkfc_d_admin_team(club):
|
|
sql = "SELECT team FROM _clubTeams WHERE club='" + club + "'"
|
|
myteams = sql_read(sql)
|
|
return jsonify(myteams)
|
|
|
|
@routes.route('/hkfc-d/getLogo/<club>')
|
|
def hkfc_d_get_logo(club):
|
|
sql = "SELECT logoURL FROM mensHockeyClubs WHERE hockeyClub='" + club + "'"
|
|
clubLogo = sql_read(sql)
|
|
return jsonify(clubLogo)
|
|
|
|
|
|
@routes.route('/hkfc-d/motmAdmin2/<fixture>')
|
|
def hkfc_d_admin_team2(fixture):
|
|
sql = "SELECT homeTeam, awayTeam FROM hockeyFixtures WHERE fixtureNumber='" + fixture + "'"
|
|
myteams = sql_read(sql)
|
|
if myteams[0]['homeTeam'].startswith("HKFC"):
|
|
nextOppo = myteams[0]['awayTeam']
|
|
else:
|
|
nextOppo = myteams[0]['homeTeam']
|
|
sql2 = "SELECT club FROM _clubTeams WHERE displayName ='" + nextOppo + "'"
|
|
return jsonify(nextOppo)
|
|
|
|
|
|
@routes.route('/hkfc-d/motmAdminLogo2/<fixture>')
|
|
def hkfc_d_admin_logo2(fixture):
|
|
sql = "SELECT homeTeam, awayTeam FROM hockeyFixtures WHERE fixtureNumber='" + fixture + "'"
|
|
myteams = sql_read(sql)
|
|
if myteams[0]['homeTeam'].startswith("HKFC D"):
|
|
nextOppo = myteams[0]['awayTeam']
|
|
else:
|
|
nextOppo = myteams[0]['homeTeam']
|
|
sql2 = "SELECT club FROM _clubTeams WHERE displayName ='" + nextOppo + "'"
|
|
clubs = sql_read_static(sql2)
|
|
clubName = clubs[0]['club']
|
|
sql3 = "SELECT logoUrl FROM mensHockeyClubs WHERE hockeyClub ='" + clubName + "'"
|
|
logo = sql_read_static(sql3)
|
|
clubLogo = logo[0]['logoUrl']
|
|
return jsonify(clubLogo)
|
|
|
|
|
|
@routes.route('/hkfc-d/statsAdmin/<fixture>')
|
|
def hkfc_dstatsAdminLookup(fixture):
|
|
sql = "SELECT homeTeam, awayTeam FROM hockeyFixtures WHERE fixtureNumber='" + fixture + "'"
|
|
teams = sql_read(sql)
|
|
|
|
#### The below SQL could be used to try and populate the team sheet for goals and assists after the match has been committed. TODO
|
|
|
|
# sql2 = "SELECT playerNumber, playerNickname FROM _hkfcD_matchSquad_" + fixture + ""
|
|
# players = sql_read(sql2)
|
|
# return jsonify(teams, players)
|
|
|
|
#### Return the teams to the javascript code.
|
|
return jsonify(teams)
|
|
|
|
@routes.route('/hkfc-d/vote-thanks', methods=['POST'])
|
|
def hkfcD_vote_thanks():
|
|
try:
|
|
_motm = request.form['motmVote']
|
|
_dotd = request.form['dotdVote']
|
|
_comments = request.form['motmComment']
|
|
_fixed_comments = _comments.replace("'", "\\'")
|
|
_matchDate = request.form['matchNumber']
|
|
_oppo = request.form['oppo']
|
|
|
|
if _motm and _dotd and request.method == 'POST':
|
|
sql = "INSERT INTO _hkfc_d_motm (playerNumber, playerName, motmTotal, motm_" + _matchDate + ") SELECT playerNumber, playerNickname, '1', '1' FROM _HKFC_players WHERE playerNumber='" + _motm + "' ON DUPLICATE KEY UPDATE motmTotal = motmTotal + 1, motm_" + _matchDate + " = motm_" + _matchDate + " + 1"
|
|
sql2 = "INSERT INTO _hkfc_d_motm (playerNumber, playerName, dotdTotal, dotd_" + _matchDate + ") SELECT playerNumber, playerNickname, '1', '1' FROM _HKFC_players WHERE playerNumber='" + _dotd + "' ON DUPLICATE KEY UPDATE dotdTotal = dotdTotal + 1, dotd_" + _matchDate + " = dotd_" + _matchDate + " + 1"
|
|
if _comments == "":
|
|
print("No comment")
|
|
elif _comments == "Optional comments added here":
|
|
print("No comment")
|
|
else:
|
|
### The matchDate has been replaced with the matchNumber - this should be corrected at some point (via a fixture table lookup)
|
|
sql3 = "INSERT INTO _motmComments (_matchDate, opposition, comment) VALUES ('" + _matchDate + "', '" + _oppo + "', '" + _fixed_comments + "')"
|
|
sql_write(sql3)
|
|
sql_write(sql)
|
|
sql_write(sql2)
|
|
return render_template('_hkfcDVoteThanks.html')
|
|
else:
|
|
return 'Ouch ... something went wrong here'
|
|
except Exception as e:
|
|
print(e)
|
|
finally:
|
|
print('Votes cast')
|
|
|
|
@routes.route('/hkfc-d/vote-results')
|
|
def hkfcD_vote_results():
|
|
_matchDate = str(mySettings('fixture'))
|
|
print(_matchDate)
|
|
sql = "SELECT playerName, motm_" + _matchDate + ", dotd_" + _matchDate + " FROM _hkfc_d_motm WHERE (motm_" + _matchDate + " > '0') OR (dotd_" + _matchDate + " > '0')"
|
|
print(sql)
|
|
rows = sql_read(sql)
|
|
print(rows)
|
|
return json.dumps(rows)
|
|
|
|
|
|
@routes.route('/hkfc-d/poty-results')
|
|
def hkfcD_poty_results():
|
|
sql = "SELECT playerName, motmTotal, dotdTotal FROM _hkfc_d_motm WHERE (motmTotal > '0') OR (dotdTotal > '0')"
|
|
print(sql)
|
|
rows = sql_read(sql)
|
|
return json.dumps(rows)
|
|
|
|
|
|
@routes.route('/hkfc-d/voting')
|
|
@basic_auth.required
|
|
def hkfcD_voting():
|
|
matchDate = mySettings('fixture')
|
|
return render_template('_hkfcDVoteChart.html', _matchDate=matchDate)
|
|
|
|
|
|
@routes.route('/hkfc-d/poty')
|
|
@basic_auth.required
|
|
def hkfcD_poty():
|
|
return render_template('_hkfcDPotYChart.html')
|
|
|
|
|
|
@routes.route('/hkfc-d/matchSquad')
|
|
@basic_auth.required
|
|
def hkfcD_match_squad():
|
|
sql1 = "SELECT team from _clubTeams WHERE club='HKFC' ORDER BY team"
|
|
sql2 = "SELECT playerTeam, playerForenames, playerSurname, playerNickname, playerNumber FROM _HKFC_players"
|
|
teams = sql_read(sql1)
|
|
players = sql_read(sql2)
|
|
return render_template('_hkfcDMatchSquad.html', teams=teams, players=players)
|
|
|
|
@routes.route('/hkfc-d/matchSquadSubmit', methods=['POST'])
|
|
@basic_auth.required
|
|
def hkfcD_match_squad_submit():
|
|
_playerNumbers = request.form.getlist('playerNumber')
|
|
for _playerNumber in _playerNumbers:
|
|
sql = "INSERT INTO _hkfcD_matchSquad (playerNumber, playerForenames, playerSurname, playerNickname) SELECT playerNumber, playerForenames, playerSurname, playerNickname FROM _HKFC_players WHERE playerNumber='" + _playerNumber + "'"
|
|
sql_write(sql)
|
|
sql2 = "SELECT playerNumber, playerForenames, playerSurname, playerNickname FROM _hkfcD_matchSquad"
|
|
players = sql_read(sql2)
|
|
table = matchSquadTable(players)
|
|
table.border = True
|
|
table.classes = ['table-striped', 'table-condensed', 'table-hover']
|
|
return render_template('_hkfcDMatchSquadSelected.html', table=table)
|
|
|
|
@routes.route('/hkfc-d/matchSquadList')
|
|
@basic_auth.required
|
|
def hkfcD_match_squad_list():
|
|
sql = "SELECT playerNumber, playerForenames, playerSurname, playerNickname FROM _hkfcD_matchSquad"
|
|
players = sql_read(sql)
|
|
table = matchSquadTable(players)
|
|
table.border = True
|
|
table.classes = ['table-striped', 'table-condensed', 'table-hover']
|
|
return render_template('_hkfcDMatchSquadSelected.html', table=table)
|
|
|
|
|
|
@routes.route('/convenor/delPlayerFromSquad', methods=['POST'])
|
|
@basic_auth.required
|
|
def delPlayerFromSquad():
|
|
_playerNumber = request.args['playerNumber']
|
|
sql = "DELETE FROM _hkfcD_matchSquad WHERE playerNumber=" + _playerNumber + ""
|
|
sql_write(sql)
|
|
return render_template('_hkfcDPlayerRemoved.html', number=_playerNumber)
|
|
|
|
|
|
@routes.route('/hkfc-d/matchSquadReset')
|
|
@basic_auth.required
|
|
def hkfcD_matchSquadReset():
|
|
_matchNumber = str(mySettings('fixture'))
|
|
print(_matchNumber)
|
|
sql1 = "RENAME TABLE _hkfcD_matchSquad TO _hkfcD_matchSquad_" + _matchNumber + ""
|
|
sql2 = "CREATE TABLE _hkfcD_matchSquad (playerNumber smallint UNIQUE, playerForenames varchar(50), playerSurname varchar(30), playerNickname varchar(30) NOT NULL, PRIMARY KEY (playerNumber))"
|
|
sql3 = "UPDATE hkfcDAdminSettings SET prevFixture='" + _matchNumber + "'"
|
|
sql_write(sql1)
|
|
sql_write(sql2)
|
|
sql_write_static(sql3)
|
|
return render_template('_hkfcDMatchSquadReset.html')
|
|
|
|
|
|
@routes.route('/hkfc-d/fixtureList')
|
|
def hkfcDFixtures():
|
|
sql = "SELECT date, division, homeTeam, awayTeam, venue, time, umpire1, umpire2 FROM hockeyFixtures WHERE homeTeam='HKFC D' OR awayTeam='HKFC D'"
|
|
rows = sql_read(sql)
|
|
table = convenorFixtureList(rows)
|
|
table.border = True
|
|
table.classes = ['table-striped', 'table-condensed', 'table-hover']
|
|
return render_template('_convenorFixtureList.html', table=table)
|