Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8b3223f865 | |||
| 276e18a9c8 | |||
| 61f2126c1b |
36
app.py
36
app.py
@@ -7,7 +7,7 @@ import os
|
|||||||
import io
|
import io
|
||||||
import qrcode
|
import qrcode
|
||||||
import logging
|
import logging
|
||||||
from flask import Flask, render_template, g, send_file, request
|
from flask import Flask, render_template, g, send_file, request, jsonify
|
||||||
|
|
||||||
# Configure logging, allowing override via LOG_LEVEL environment variable
|
# Configure logging, allowing override via LOG_LEVEL environment variable
|
||||||
log_level = os.environ.get("LOG_LEVEL", "INFO").upper()
|
log_level = os.environ.get("LOG_LEVEL", "INFO").upper()
|
||||||
@@ -23,7 +23,7 @@ from config import (
|
|||||||
)
|
)
|
||||||
from attendance import get_members_with_fees, get_junior_members_with_fees, ADULT_MERGED_MONTHS, JUNIOR_MERGED_MONTHS
|
from attendance import get_members_with_fees, get_junior_members_with_fees, ADULT_MERGED_MONTHS, JUNIOR_MERGED_MONTHS
|
||||||
from match_payments import reconcile, fetch_sheet_data, fetch_exceptions, normalize
|
from match_payments import reconcile, fetch_sheet_data, fetch_exceptions, normalize
|
||||||
from cache_utils import get_sheet_modified_time, read_cache, write_cache, _LAST_CHECKED
|
from cache_utils import get_sheet_modified_time, read_cache, write_cache, _LAST_CHECKED, flush_cache
|
||||||
|
|
||||||
def get_cached_data(cache_key, sheet_id, fetch_func, *args, serialize=None, deserialize=None, **kwargs):
|
def get_cached_data(cache_key, sheet_id, fetch_func, *args, serialize=None, deserialize=None, **kwargs):
|
||||||
mod_time = get_sheet_modified_time(cache_key)
|
mod_time = get_sheet_modified_time(cache_key)
|
||||||
@@ -105,8 +105,13 @@ def inject_render_time():
|
|||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
# Redirect root to /fees for convenience while there are no other apps
|
# Redirect root to /adults for convenience while there are no other apps
|
||||||
return '<meta http-equiv="refresh" content="0; url=/fees" />'
|
return '<meta http-equiv="refresh" content="0; url=/adults" />'
|
||||||
|
|
||||||
|
@app.route("/flush-cache", methods=["POST"])
|
||||||
|
def flush_cache_endpoint():
|
||||||
|
deleted = flush_cache()
|
||||||
|
return jsonify({"status": "ok", "deleted_files": deleted})
|
||||||
|
|
||||||
@app.route("/fees")
|
@app.route("/fees")
|
||||||
def fees():
|
def fees():
|
||||||
@@ -582,7 +587,7 @@ def juniors_view():
|
|||||||
if expected == "?" or (isinstance(expected, int) and expected > 0):
|
if expected == "?" or (isinstance(expected, int) and expected > 0):
|
||||||
if expected == "?":
|
if expected == "?":
|
||||||
status = "empty"
|
status = "empty"
|
||||||
cell_text = "?"
|
cell_text = f"?{count_str}"
|
||||||
elif paid >= expected:
|
elif paid >= expected:
|
||||||
status = "ok"
|
status = "ok"
|
||||||
cell_text = f"{paid}/{fee_display}"
|
cell_text = f"{paid}/{fee_display}"
|
||||||
@@ -705,6 +710,8 @@ def reconcile_juniors_view():
|
|||||||
# Filter to juniors for the main table
|
# Filter to juniors for the main table
|
||||||
junior_names = sorted([name for name, tier, _ in adapted_members])
|
junior_names = sorted([name for name, tier, _ in adapted_members])
|
||||||
|
|
||||||
|
junior_members_dict_rc = {name: fees_dict for name, _, fees_dict in junior_members}
|
||||||
|
|
||||||
formatted_results = []
|
formatted_results = []
|
||||||
for name in junior_names:
|
for name in junior_names:
|
||||||
data = result["members"][name]
|
data = result["members"][name]
|
||||||
@@ -715,6 +722,23 @@ def reconcile_juniors_view():
|
|||||||
expected = mdata["expected"]
|
expected = mdata["expected"]
|
||||||
paid = int(mdata["paid"])
|
paid = int(mdata["paid"])
|
||||||
|
|
||||||
|
orig_fee_data = junior_members_dict_rc.get(name, {}).get(m)
|
||||||
|
adult_count = 0
|
||||||
|
junior_count = 0
|
||||||
|
att_count = 0
|
||||||
|
if orig_fee_data and len(orig_fee_data) == 4:
|
||||||
|
_, att_count, adult_count, junior_count = orig_fee_data
|
||||||
|
|
||||||
|
breakdown = ""
|
||||||
|
if adult_count > 0 and junior_count > 0:
|
||||||
|
breakdown = f":{junior_count}J,{adult_count}A"
|
||||||
|
elif junior_count > 0:
|
||||||
|
breakdown = f":{junior_count}J"
|
||||||
|
elif adult_count > 0:
|
||||||
|
breakdown = f":{adult_count}A"
|
||||||
|
|
||||||
|
count_str = f" ({att_count}{breakdown})" if att_count > 0 else ""
|
||||||
|
|
||||||
status = "empty"
|
status = "empty"
|
||||||
cell_text = "-"
|
cell_text = "-"
|
||||||
amount_to_pay = 0
|
amount_to_pay = 0
|
||||||
@@ -722,7 +746,7 @@ def reconcile_juniors_view():
|
|||||||
if expected == "?" or (isinstance(expected, int) and expected > 0):
|
if expected == "?" or (isinstance(expected, int) and expected > 0):
|
||||||
if expected == "?":
|
if expected == "?":
|
||||||
status = "empty"
|
status = "empty"
|
||||||
cell_text = "?"
|
cell_text = f"?{count_str}"
|
||||||
elif paid >= expected:
|
elif paid >= expected:
|
||||||
status = "ok"
|
status = "ok"
|
||||||
cell_text = "OK"
|
cell_text = "OK"
|
||||||
|
|||||||
@@ -155,3 +155,19 @@ def write_cache(sheet_id: str, modified_time: str, data: list | dict) -> None:
|
|||||||
logger.info(f"Wrote cache for {sheet_id}")
|
logger.info(f"Wrote cache for {sheet_id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to write cache {sheet_id}: {e}")
|
logger.error(f"Failed to write cache {sheet_id}: {e}")
|
||||||
|
|
||||||
|
def flush_cache():
|
||||||
|
"""Delete all cache files and reset in-memory state. Returns count of deleted files."""
|
||||||
|
global _DRIVE_SERVICE
|
||||||
|
_LAST_CHECKED.clear()
|
||||||
|
_DRIVE_SERVICE = None
|
||||||
|
|
||||||
|
deleted = 0
|
||||||
|
if CACHE_DIR.exists():
|
||||||
|
for f in CACHE_DIR.glob("*_cache.json"):
|
||||||
|
f.unlink()
|
||||||
|
deleted += 1
|
||||||
|
logger.info(f"Deleted cache file: {f.name}")
|
||||||
|
|
||||||
|
logger.info(f"Cache flushed: {deleted} files deleted, timers reset")
|
||||||
|
return deleted
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class TestWebApp(unittest.TestCase):
|
|||||||
"""Test that / returns the refresh meta tag"""
|
"""Test that / returns the refresh meta tag"""
|
||||||
response = self.client.get('/')
|
response = self.client.get('/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertIn(b'url=/fees', response.data)
|
self.assertIn(b'url=/adults', response.data)
|
||||||
|
|
||||||
@patch('app.get_cached_data', side_effect=_bypass_cache)
|
@patch('app.get_cached_data', side_effect=_bypass_cache)
|
||||||
@patch('app.get_members_with_fees')
|
@patch('app.get_members_with_fees')
|
||||||
|
|||||||
Reference in New Issue
Block a user