implement /api/changed route, new lastModified function

This commit is contained in:
Xevion
2020-06-19 17:55:29 -05:00
parent 69534cfdc9
commit d34aa7f620
2 changed files with 16 additions and 5 deletions

View File

@@ -3,9 +3,12 @@ api.py
Handles backend routes assisting Handles backend routes assisting
""" """
import json
from flask import request from flask import request
from trivia import app from trivia import app
from trivia.utils import lastModified
@app.route("/api/changed") @app.route("/api/changed")
@@ -18,8 +21,9 @@ def changed():
TODO: Remove this function once a proper 304 Not Modified implementation is found for client side. TODO: Remove this function once a proper 304 Not Modified implementation is found for client side.
""" """
from trivia.utils import lastChange
time = request.args.get('last') time = int(request.args.get('last') or lastModified())
return json.dumps(lastChange < time)
@app.route("/api/refresh") @app.route("/api/refresh")

View File

@@ -12,12 +12,20 @@ from trivia import Team
# Generate paths # Generate paths
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(BASE_DIR, 'data') DATA_DIR = os.path.join(BASE_DIR, 'data')
SCORES_FILE = os.path.join(DATA_DIR, 'scores.json')
# Initialize global data/tracking vars # Initialize global data/tracking vars
lastChange: int = -1 lastChange: int = -1
teams: List[Team] = [] teams: List[Team] = []
def lastModified() -> float:
"""
returns epoch time of last modification to the scores file.
"""
return os.stat(SCORES_FILE).st_mtime
def refreshScores() -> None: def refreshScores() -> None:
""" """
Refreshes scores data safely. Refreshes scores data safely.
@@ -26,8 +34,7 @@ def refreshScores() -> None:
""" """
global lastChange global lastChange
filepath = os.path.join(DATA_DIR, 'scores.json') curChange = lastModified()
curChange = os.stat(filepath).st_mtime
if lastChange < curChange: if lastChange < curChange:
try: try:
@@ -35,7 +42,7 @@ def refreshScores() -> None:
lastChange = curChange lastChange = curChange
print('Attempting to load and parse scores file.') print('Attempting to load and parse scores file.')
with open(filepath) as file: with open(SCORES_FILE) as file:
temp = json.load(file) temp = json.load(file)
# Place all values into Team object for jinja # Place all values into Team object for jinja