mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-06 07:16:38 -06:00
implement /api/changed route, new lastModified function
This commit is contained in:
@@ -3,9 +3,12 @@ api.py
|
||||
|
||||
Handles backend routes assisting
|
||||
"""
|
||||
import json
|
||||
|
||||
from flask import request
|
||||
|
||||
from trivia import app
|
||||
from trivia.utils import lastModified
|
||||
|
||||
|
||||
@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.
|
||||
"""
|
||||
|
||||
time = request.args.get('last')
|
||||
from trivia.utils import lastChange
|
||||
time = int(request.args.get('last') or lastModified())
|
||||
return json.dumps(lastChange < time)
|
||||
|
||||
|
||||
@app.route("/api/refresh")
|
||||
|
||||
@@ -12,12 +12,20 @@ from trivia import Team
|
||||
# Generate paths
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
DATA_DIR = os.path.join(BASE_DIR, 'data')
|
||||
SCORES_FILE = os.path.join(DATA_DIR, 'scores.json')
|
||||
|
||||
# Initialize global data/tracking vars
|
||||
lastChange: int = -1
|
||||
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:
|
||||
"""
|
||||
Refreshes scores data safely.
|
||||
@@ -26,8 +34,7 @@ def refreshScores() -> None:
|
||||
"""
|
||||
|
||||
global lastChange
|
||||
filepath = os.path.join(DATA_DIR, 'scores.json')
|
||||
curChange = os.stat(filepath).st_mtime
|
||||
curChange = lastModified()
|
||||
|
||||
if lastChange < curChange:
|
||||
try:
|
||||
@@ -35,7 +42,7 @@ def refreshScores() -> None:
|
||||
lastChange = curChange
|
||||
|
||||
print('Attempting to load and parse scores file.')
|
||||
with open(filepath) as file:
|
||||
with open(SCORES_FILE) as file:
|
||||
temp = json.load(file)
|
||||
|
||||
# Place all values into Team object for jinja
|
||||
|
||||
Reference in New Issue
Block a user