mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-07 05:16:48 -06:00
implement /api/changed route, new lastModified function
This commit is contained in:
@@ -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")
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user