cteward-ng/cteward_ng/filters.py

128 lines
3.4 KiB
Python
Raw Permalink Normal View History

2026-06-06 10:18:15 +02:00
"""Data filter functions.
Replaces filters.js:
- MEMBERLIST_ACTIVE_ONLY: keep only active crew/raumfahrer/passive
- MEMBERLIST_SELF_ONLY: keep only the requesting user's record
- runfilter: apply a filter function if configured
"""
def memberlist_active_only(ctx):
"""Filter member list to active members only.
Replaces MEMBERLIST_ACTIVE_ONLY from filters.js.
The requesting user is always included regardless of status.
"""
username = ctx.get('username', '')
newdata = []
for row in ctx.get('data', []):
# Always include self
if username == row.get('Kurzname'):
newdata.append(row)
continue
status = _realstatus(row)
if status not in ('crew', 'raumfahrer', 'passiv'):
continue
kurzname = row.get('Kurzname', '')
if not kurzname or kurzname[0] == 'X':
continue
newrow = {
'Kurzname': kurzname,
'Kennung3': row.get('Kennung3'),
'Eintritt': row.get('Eintritt'),
'Kontaktwoher': row.get('Kontaktwoher'),
}
if row.get('Nachname'):
newrow['Nachname'] = row['Nachname']
if row.get('Vorname'):
newrow['Vorname'] = row['Vorname']
if row.get('Austritt'):
newrow['Austritt'] = row['Austritt']
newdata.append(newrow)
ctx['data'] = newdata
return ctx
def memberlist_self_only(ctx):
"""Filter member list to only the requesting user.
Replaces MEMBERLIST_SELF_ONLY from filters.js.
"""
username = ctx.get('username', '')
newdata = []
for row in ctx.get('data', []):
if username == row.get('Kurzname'):
newdata.append(row)
break
ctx['data'] = newdata
return ctx
def runfilter(ctx):
"""Apply the configured filter, if any.
Replaces runfilter() from filters.js.
"""
filt = ctx.get('filter')
if filt is None:
return ctx
return filt(ctx)
# ── Internal helpers ──────────────────────────────────────────────────────────
# We import realstatus lazily to avoid circular imports
_realstatus = None
def _get_realstatus():
global _realstatus
if _realstatus is None:
from .memberdata import realstatus as _rs
_realstatus = _rs
return _realstatus
# Override the closure to use the real function
def memberlist_active_only(ctx):
rs = _get_realstatus()
username = ctx.get('username', '')
newdata = []
for row in ctx.get('data', []):
if username == row.get('Kurzname'):
newdata.append(row)
continue
status = rs(row)
if status not in ('crew', 'raumfahrer', 'passiv'):
continue
kurzname = row.get('Kurzname', '')
if not kurzname or kurzname[0] == 'X':
continue
newrow = {
'Kurzname': kurzname,
'Kennung3': row.get('Kennung3'),
'Eintritt': row.get('Eintritt'),
'Kontaktwoher': row.get('Kontaktwoher'),
}
if row.get('Nachname'):
newrow['Nachname'] = row['Nachname']
if row.get('Vorname'):
newrow['Vorname'] = row['Vorname']
if row.get('Austritt'):
newrow['Austritt'] = row['Austritt']
newdata.append(newrow)
ctx['data'] = newdata
return ctx