admin overview: expose derived membership state (active / in_grace / awaiting_contribution / inactive)

The admin panel needs to show a member's membership state without inventing
policy of its own. Compute it in the portal (the source of truth) from what the
allowance reconciliation already knows — the payment_reminders send record and
the latest allowance_log decision — and carry it on each /admin/overview row as
state + state_reason. Read-only; no side effects.
This commit is contained in:
inference-bot committed 2026-11-04 18:30:42 +00:00
1 parent 97da432bd4
commit f1945aaf53
1 file changed
+42
+42
View File
@@ -3175,12 +3175,15 @@ async def admin_overview(request: Request):
credits, credits,
spend, spend,
) )
mstate, mstate_reason = member_membership_state(email, bool(active))
entry = { entry = {
"email": email, "email": email,
"all_time_spend": round(member_all_time, 2), "all_time_spend": round(member_all_time, 2),
"username": usernames.get(user_id), "username": usernames.get(user_id),
"activated": invite_accepted.get(user_id, False), "activated": invite_accepted.get(user_id, False),
"active": bool(active), "active": bool(active),
"state": mstate,
"state_reason": mstate_reason,
"manual": email.lower() in MANUAL_MEMBERS, "manual": email.lower() in MANUAL_MEMBERS,
"org": org_slug or None, "org": org_slug or None,
"matrix_mxid": matrix_mxid or None, "matrix_mxid": matrix_mxid or None,
@@ -4090,6 +4093,45 @@ def _ensure_allowance_tables(conn) -> None:
conn.commit() conn.commit()
def member_membership_state(email: str, active: bool) -> tuple[str, str]:
"""Derived membership state for the admin panel — read-only, no side effects.
Mirrors what the allowance reconciliation already knows, so the panel reflects
the portal rather than inventing policy of its own:
inactive — membership deactivated (members.active = 0).
in_grace — a payment reminder has been sent for this member's
current cycle and no contribution has arrived yet;
access continues for PAYMENT_GRACE_DAYS.
awaiting_contribution — the reconciliation flagged the member unpaid but no
reminder has been sent yet.
active — current / no open reminder.
"""
if not active:
return "inactive", "deactivated"
try:
conn = get_db()
rem = conn.execute(
"SELECT sent_at FROM payment_reminders WHERE email = ? "
"ORDER BY sent_at DESC LIMIT 1",
(email,),
).fetchone()
log = conn.execute(
"SELECT decision FROM allowance_log WHERE email = ? ORDER BY id DESC LIMIT 1",
(email,),
).fetchone()
conn.close()
except Exception: # tables absent / unreadable: fail safe
return "active", ""
if rem:
return "in_grace", (f"awaiting contribution — reminder sent "
f"{str(rem[0])[:10]}, grace {int(PAYMENT_GRACE_DAYS)}d")
if log and log[0] in ("reminded", "would_remind", "remind_failed"):
return "awaiting_contribution", "no current contribution at Open Collective"
return "active", ""
def _parse_oc_dt(value): def _parse_oc_dt(value):
"""Parse an Open Collective ISO timestamp ('…Z') into an aware datetime.""" """Parse an Open Collective ISO timestamp ('…Z') into an aware datetime."""
if not value: if not value: