change refresh route to 'scores', implement app.logger statements instead of print

This commit is contained in:
Xevion
2020-06-20 04:47:36 -05:00
parent 395221288e
commit 774ac1b67f
2 changed files with 9 additions and 9 deletions

View File

@@ -11,9 +11,9 @@ from flask import request, make_response
from trivia import app
@app.route("/api/refresh")
@app.route("/api/refresh/")
def refresh():
@app.route("/api/scores")
@app.route("/api/scores/")
def scores():
"""
Used for refreshing client-side table data. Returns a JSON response with all data necessary to build the table.
"""
@@ -22,8 +22,8 @@ def refresh():
# Create response using namedtuples
r = make_response(json.dumps([team._asdict() for team in teams]))
r.mimetype = 'application/json'
status_code = 200
# Try to handle If-Modified-Since header properly (304 Not Modified
try:
if request.headers['If-Modified-Since']:
@@ -34,6 +34,6 @@ def refresh():
except KeyError:
pass # Header was not supplied. Ignore.
except ValueError:
print('If-Modified-Since Header could not be parsed.') # Header could not be parsed.
app.logger.warning('If-Modified-Since Header could not be parsed.') # Header could not be parsed.
return r, status_code

View File

@@ -7,7 +7,7 @@ import json
import os
from typing import List
from trivia import Team
from trivia import Team, app
# Generate paths
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -41,7 +41,7 @@ def refreshScores() -> None:
# Update tracking var
lastChange = curChange
print('Attempting to load and parse scores file.')
app.logger.debug('Attempting to load and parse scores file.')
with open(SCORES_FILE) as file:
temp = json.load(file)
@@ -53,11 +53,11 @@ def refreshScores() -> None:
scores=team['scores']
) for team in temp
]
print(f'Successfully loaded ({len(temp)} teams).')
app.logger.debug(f'Successfully loaded ({len(temp)} teams).')
global teams
teams = temp
# If invalid or inaccessible, simply do nothing.
except json.JSONDecodeError:
print('Scores file could not be opened or parsed.')
app.logger.error('Scores file could not be opened or parsed.', print_exc=True)