2026-06-02 00:47:03 -04:00
|
|
|
import json
|
2026-06-10 10:06:13 -04:00
|
|
|
import time
|
|
|
|
|
from datetime import datetime, timezone
|
2026-06-07 00:21:08 -04:00
|
|
|
import config_utils
|
|
|
|
|
import factory
|
2026-06-02 00:47:03 -04:00
|
|
|
|
|
|
|
|
|
2026-06-10 10:06:13 -04:00
|
|
|
def _fmt_ts(ts):
|
|
|
|
|
try:
|
|
|
|
|
dt = datetime.fromtimestamp(int(ts), tz=timezone.utc)
|
|
|
|
|
return dt.strftime('%Y-%m-%d %H:%M UTC')
|
|
|
|
|
except Exception:
|
|
|
|
|
return '-'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _active_sessions_table():
|
|
|
|
|
rows = config_utils.get_active_sessions()
|
|
|
|
|
no_data = '<p class="text-muted" style="margin:0">No active sessions.</p>'
|
|
|
|
|
if not rows:
|
|
|
|
|
return no_data
|
|
|
|
|
now = int(time.time())
|
|
|
|
|
trs = ''
|
|
|
|
|
for email, access_level, logged_in_at, last_seen in rows:
|
|
|
|
|
ago = config_utils.relative_time(int(last_seen), now)
|
|
|
|
|
trs += (
|
|
|
|
|
f'<tr>'
|
|
|
|
|
f'<td class="table-cell">{factory.e(email)}</td>'
|
|
|
|
|
f'<td class="table-cell">{factory.e(access_level)}</td>'
|
|
|
|
|
f'<td class="table-cell">{_fmt_ts(logged_in_at)}</td>'
|
|
|
|
|
f'<td class="table-cell">{factory.e(ago)} ago</td>'
|
|
|
|
|
f'</tr>'
|
|
|
|
|
)
|
|
|
|
|
return (
|
|
|
|
|
'<table class="data-table"><thead><tr>'
|
|
|
|
|
'<th class="table-header">Email</th>'
|
|
|
|
|
'<th class="table-header">Access Level</th>'
|
|
|
|
|
'<th class="table-header">Logged In</th>'
|
|
|
|
|
'<th class="table-header">Last Seen</th>'
|
|
|
|
|
'</tr></thead><tbody>' + trs + '</tbody></table>'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-06-02 00:47:03 -04:00
|
|
|
def collect_tokens(cfg):
|
2026-06-07 00:21:08 -04:00
|
|
|
tokens = config_utils.collect_layout_tokens(cfg)
|
2026-06-02 12:49:39 -04:00
|
|
|
tokens['ACCOUNT_LEVEL_OPTIONS'] = json.dumps([
|
|
|
|
|
{'value': 'viewer', 'label': 'Viewer (read-only access to live data)'},
|
|
|
|
|
{'value': 'administrator', 'label': 'Administrator (can modify configuration)'},
|
|
|
|
|
{'value': 'manager', 'label': 'Manager (full access including account management)'},
|
|
|
|
|
])
|
2026-06-10 10:06:13 -04:00
|
|
|
tokens['ACTIVE_SESSIONS_TABLE'] = _active_sessions_table()
|
2026-06-07 00:21:08 -04:00
|
|
|
content = factory.load_json(f'{factory.PAGES_DIR}/accountmanage/content.json')
|
|
|
|
|
for table_item in factory.iter_table_items(content.get('items', [])):
|
2026-06-02 12:49:39 -04:00
|
|
|
ds = table_item.get('datasource', '')
|
2026-06-07 00:21:08 -04:00
|
|
|
tokens[factory.table_token_key(ds)] = factory.build_table(table_item, tokens, config_utils.load_datasource(ds))
|
2026-06-02 12:49:39 -04:00
|
|
|
return tokens
|