63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
import json
|
|
|
|
import config_utils
|
|
import factory
|
|
|
|
|
|
def _format_session(secs):
|
|
if not secs or secs <= 0:
|
|
return 'No limit'
|
|
if secs % 86400 == 0:
|
|
d = secs // 86400
|
|
return f'{d} day{"s" if d != 1 else ""}'
|
|
h = secs / 3600
|
|
return f'{h:g} h'
|
|
|
|
|
|
def collect_tokens(cfg):
|
|
tokens = config_utils.collect_layout_tokens(cfg)
|
|
cp = cfg.get('captive_portal', {})
|
|
captive_vlans = [v for v in cfg.get('vlans', []) if v.get('restricted_vlan') == 'c']
|
|
|
|
n = len(captive_vlans)
|
|
if n > 0:
|
|
variant = 'success'
|
|
text = f'There are currently {n} captive portal VLAN{"s" if n != 1 else ""}. Captive portal is enabled.'
|
|
else:
|
|
variant = 'warning'
|
|
text = 'There are currently 0 captive portal VLANs. Captive portal is disabled.'
|
|
tokens['CAPTIVE_STATUS_BAR'] = f'<div class="info-bar info-bar-inline info-bar-{variant}">{text}</div>'
|
|
|
|
tokens['CAPTIVE_HTTP_PORT'] = str(cp.get('http_port', 25328))
|
|
tokens['CAPTIVE_HTTPS_DOMAIN'] = cp.get('https_domain', '')
|
|
|
|
display_rows = []
|
|
for vlan in captive_vlans:
|
|
cp = vlan.get('captive_portal', {})
|
|
title = cp.get('portal_splash_title', vlan.get('portal_splash_title', ''))
|
|
text = cp.get('portal_splash_text', vlan.get('portal_splash_text', ''))
|
|
terms = cp.get('portal_terms', vlan.get('portal_terms', []))
|
|
require_upw = cp.get('require_username_password', vlan.get('require_username_password', False))
|
|
duration = cp.get('default_session_seconds', vlan.get('default_session_seconds', 0))
|
|
expiration = cp.get('default_expiration_seconds', vlan.get('default_expiration_seconds', 0))
|
|
n = len(terms)
|
|
display_rows.append({
|
|
'vlan_name': vlan['name'],
|
|
'portal_splash_title': title,
|
|
'portal_splash_text': text,
|
|
'portal_terms': terms,
|
|
'portal_terms_display': f'{n} term{"s" if n != 1 else ""}' if n else '--',
|
|
'require_upw': require_upw,
|
|
'require_username_password': require_upw,
|
|
'default_session_seconds': duration,
|
|
'default_expiration_seconds': expiration,
|
|
'session_display': _format_session(duration),
|
|
})
|
|
|
|
content = factory.load_json(f'{factory.PAGES_DIR}/captiveportal/content.json')
|
|
for table_item in factory.iter_table_items(content.get('items', [])):
|
|
ds = table_item.get('datasource', '')
|
|
data = display_rows if ds == 'captive_portals' else []
|
|
tokens[factory.table_token_key(ds)] = factory.build_table(table_item, tokens, data)
|
|
|
|
return tokens
|