Start adding random choices

This commit is contained in:
Jonathan Ervine 2020-11-26 15:48:28 +08:00
parent b4a12a8a91
commit 11cba3f138
2 changed files with 73 additions and 2 deletions

View File

@ -235,9 +235,80 @@ def hkfcD_vote_thanks():
if _motm and _dotd and request.method == 'POST': if _motm and _dotd and request.method == 'POST':
prev_identity = request.cookies.get('sessionID') prev_identity = request.cookies.get('sessionID')
user_agent = request.headers.get('User-Agent') user_agent = request.headers.get('User-Agent')
warn(prev_identity) warn("Previous identity found: "+prev_identity)
if prev_identity: if prev_identity:
vote_query = "SELECT dotd_" + _matchDate + " FROM motmSessions WHERE sessionID='" + prev_identity + "'" vote_query = "SELECT dotd_" + _matchDate + " FROM motmSessions WHERE sessionID='" + prev_identity + "'"
vote_check = sql_read(vote_query)
if not vote_check:
warn('Cookie exists but no record in DB - check: '+prev_identity)
return render_template('_hkfcDSmithersFail.html', sessionID=prev_identity)
else:
vote_valid = vote_check[0]['dotd_' + _matchDate ]
if vote_valid:
warn("Naughty, naughty, you've already voted!")
return render_template('_hkfcDVoteFraud.html', sessionID=prev_identity)
else:
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"
sql4 = "UPDATE motmSessions SET motm_" + _matchDate + "=" + _motm + " WHERE sessionID='" + prev_identity + "'"
sql5 = "UPDATE motmSessions SET dotd_" + _matchDate + "=" + _dotd + " WHERE sessionID='" + prev_identity + "'"
sql_write(sql4)
sql_write(sql5)
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)
resp = make_response(render_template('_hkfcDVoteThanks.html'))
expire_date = datetime.now()
expire_date = expire_date + timedelta(days=90)
resp.set_cookie('sessionID', prev_identity, expires=expire_date)
return resp
else:
identity = randomUrlSuffix(8)
id_commit = "INSERT INTO motmSessions (sessionID) VALUES ('" + identity + "')"
ua_commit = "INSERT INTO motmSessions (userAgent) VALUES ('" + user_agent + "')"
sql_write(id_commit)
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)
resp = make_response(render_template('_hkfcDVoteThanks.html'))
expire_date = datetime.now()
expire_date = expire_date + timedelta(days=90)
resp.set_cookie('sessionID', identity, expires=expire_date)
return resp
else:
return 'Ouch ... something went wrong here'
except Exception as e:
print(e)
finally:
print('Votes cast - thanks')
@routes.route('/hkfc-d/vote-chicken', methods=[i'GET', 'POST'])
def hkfcD_vote_chicken():
try:
prev_identity = request.cookies.get('sessionID')
user_agent = request.headers.get('User-Agent')
nextFixture = "SELECT nextFixture FROM hkfcDAdminSettings"
_matchDate = sql_read_static(nextFixture)
if prev_identity:
vote_query = "SELECT dotd_" + _matchDate + " FROM motmSessions WHERE sessionID='" + prev_identity + "'"
vote_check = sql_read(vote_query) vote_check = sql_read(vote_query)
if not vote_check: if not vote_check:
warn('Cookie exists but no record in DB - check: '+prev_identity) warn('Cookie exists but no record in DB - check: '+prev_identity)
@ -298,7 +369,6 @@ def hkfcD_vote_thanks():
print(e) print(e)
finally: finally:
print('Votes cast') print('Votes cast')
@routes.route('/hkfc-d/vote-results') @routes.route('/hkfc-d/vote-results')
def hkfcD_vote_results(): def hkfcD_vote_results():
_matchDate = str(mySettings('fixture')) _matchDate = str(mySettings('fixture'))

View File

@ -74,6 +74,7 @@
</div> </div>
</div> </div>
<button type="submit" class="btn btn-success">Submit</button> <button type="submit" class="btn btn-success">Submit</button>
<a class="btn btn-warning" href="/hkfc-d/motm-chicken">Chicken</a>
<a class="btn btn-danger" href="/dashboard" role="button">Cancel</a> <a class="btn btn-danger" href="/dashboard" role="button">Cancel</a>
</form> </form>
</div> </div>