refactor: unify get_cached_exceptions into get_cached_data
Add optional serialize/deserialize hooks to get_cached_data() so it can handle the exceptions dict (tuple keys → JSON-safe lists) without needing a separate function. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
46
app.py
46
app.py
@@ -21,27 +21,15 @@ from attendance import get_members_with_fees, get_junior_members_with_fees, SHEE
|
||||
from match_payments import reconcile, fetch_sheet_data, fetch_exceptions, normalize, DEFAULT_SPREADSHEET_ID as PAYMENTS_SHEET_ID
|
||||
from cache_utils import get_sheet_modified_time, read_cache, write_cache, _LAST_CHECKED
|
||||
|
||||
def get_cached_data(cache_key, sheet_id, fetch_func, *args, **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)
|
||||
if mod_time:
|
||||
cached = read_cache(cache_key, mod_time)
|
||||
if cached is not None:
|
||||
return cached
|
||||
return deserialize(cached) if deserialize else cached
|
||||
data = fetch_func(*args, **kwargs)
|
||||
if mod_time:
|
||||
write_cache(cache_key, mod_time, data)
|
||||
return data
|
||||
|
||||
def get_cached_exceptions(sheet_id, creds_path):
|
||||
cache_key = "exceptions_dict"
|
||||
mod_time = get_sheet_modified_time(cache_key)
|
||||
if mod_time:
|
||||
cached = read_cache(cache_key, mod_time)
|
||||
if cached is not None:
|
||||
return {tuple(k): v for k, v in cached}
|
||||
data = fetch_exceptions(sheet_id, creds_path)
|
||||
if mod_time:
|
||||
write_cache(cache_key, mod_time, [[list(k), v] for k, v in data.items()])
|
||||
write_cache(cache_key, mod_time, serialize(data) if serialize else data)
|
||||
return data
|
||||
|
||||
def get_month_labels(sorted_months, merged_months):
|
||||
@@ -123,7 +111,12 @@ def fees():
|
||||
|
||||
# Get exceptions for formatting
|
||||
credentials_path = ".secret/fuj-management-bot-credentials.json"
|
||||
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path)
|
||||
exceptions = get_cached_data(
|
||||
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
|
||||
PAYMENTS_SHEET_ID, credentials_path,
|
||||
serialize=lambda d: [[list(k), v] for k, v in d.items()],
|
||||
deserialize=lambda c: {tuple(k): v for k, v in c},
|
||||
)
|
||||
record_step("fetch_exceptions")
|
||||
|
||||
formatted_results = []
|
||||
@@ -181,7 +174,12 @@ def fees_juniors():
|
||||
|
||||
# Get exceptions for formatting (reusing payments sheet)
|
||||
credentials_path = ".secret/fuj-management-bot-credentials.json"
|
||||
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path)
|
||||
exceptions = get_cached_data(
|
||||
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
|
||||
PAYMENTS_SHEET_ID, credentials_path,
|
||||
serialize=lambda d: [[list(k), v] for k, v in d.items()],
|
||||
deserialize=lambda c: {tuple(k): v for k, v in c},
|
||||
)
|
||||
record_step("fetch_exceptions")
|
||||
|
||||
formatted_results = []
|
||||
@@ -253,7 +251,12 @@ def reconcile_view():
|
||||
|
||||
transactions = get_cached_data("payments_transactions", PAYMENTS_SHEET_ID, fetch_sheet_data, PAYMENTS_SHEET_ID, credentials_path)
|
||||
record_step("fetch_payments")
|
||||
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path)
|
||||
exceptions = get_cached_data(
|
||||
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
|
||||
PAYMENTS_SHEET_ID, credentials_path,
|
||||
serialize=lambda d: [[list(k), v] for k, v in d.items()],
|
||||
deserialize=lambda c: {tuple(k): v for k, v in c},
|
||||
)
|
||||
record_step("fetch_exceptions")
|
||||
result = reconcile(members, sorted_months, transactions, exceptions)
|
||||
record_step("reconcile")
|
||||
@@ -346,7 +349,12 @@ def reconcile_juniors_view():
|
||||
|
||||
transactions = get_cached_data("payments_transactions", PAYMENTS_SHEET_ID, fetch_sheet_data, PAYMENTS_SHEET_ID, credentials_path)
|
||||
record_step("fetch_payments")
|
||||
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path)
|
||||
exceptions = get_cached_data(
|
||||
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
|
||||
PAYMENTS_SHEET_ID, credentials_path,
|
||||
serialize=lambda d: [[list(k), v] for k, v in d.items()],
|
||||
deserialize=lambda c: {tuple(k): v for k, v in c},
|
||||
)
|
||||
record_step("fetch_exceptions")
|
||||
|
||||
# Adapt junior tuple format (name, tier, {month: (fee, total_count, adult_count, junior_count)})
|
||||
|
||||
Reference in New Issue
Block a user