Development

This commit is contained in:
Matthew Grotke 2026-06-07 00:21:08 -04:00
parent 563d82daf3
commit 70ccfe2c29
48 changed files with 549 additions and 578 deletions

View file

@ -1,15 +1,9 @@
import os, json, sys, importlib.util as _importlib_util
from flask import Flask, Blueprint, session, redirect, get_flashed_messages, send_from_directory
from markupsafe import Markup
from config_utils import (
ACCOUNTS_FILE, APP_DIR, CONFIGS_DIR, HEALTH_FILE, WWW_DIR,
load_config, queue_command, _find_cmd_in_queues,
)
from factory import (
LEVEL_RANK, PAGES_DIR, e, client_level, passes, build_items,
load_json, render_layout,
)
import settings as settings
import config_utils
import factory
import settings
from pages.actions.action import bp as actions_bp
from pages.bannedips.action import bp as bannedips_bp
from pages.ddns.action import bp as ddns_bp
@ -41,7 +35,7 @@ app.secret_key = os.environ.get('SECRET_KEY', os.urandom(24))
@app.route('/www/<path:filename>')
def serve_www(filename):
response = send_from_directory(WWW_DIR, filename)
response = send_from_directory(config_utils.WWW_DIR, filename)
if settings.is_production():
response.cache_control.max_age = 86400
response.cache_control.public = True
@ -55,7 +49,7 @@ page_view_cache = {}
def load_page_view(page_name):
if page_name not in page_view_cache:
path = os.path.join(PAGES_DIR, page_name, 'view.py')
path = os.path.join(factory.PAGES_DIR, page_name, 'view.py')
if not os.path.exists(path):
page_view_cache[page_name] = None
else:
@ -75,30 +69,30 @@ def view(page_name):
return serve_view(page_name)
def serve_view(page_name):
view_def = load_json(os.path.join(PAGES_DIR, page_name, 'content.json'))
view_def = factory.load_json(os.path.join(factory.PAGES_DIR, page_name, 'content.json'))
if not view_def:
from flask import abort
abort(404)
view_req = view_def.get('client_requirement')
level = client_level()
if not passes(view_req, level):
level = factory.client_level()
if not factory.passes(view_req, level):
return redirect('/overview' if level > 0 else '/accountlogin')
cfg = load_config()
cfg = config_utils.load_config()
if level >= LEVEL_RANK['administrator']:
if level >= factory.LEVEL_RANK['administrator']:
try:
st = json.load(open(HEALTH_FILE))
st = json.load(open(config_utils.HEALTH_FILE))
has_problems = any(
item.get('status') == 'problem'
for section in ('configurations', 'logs', 'services')
for item in st.get(section, [])
)
if has_problems:
fix_uuid, _ = _find_cmd_in_queues('fix problems')
fix_uuid, _ = config_utils._find_cmd_in_queues('fix problems')
if fix_uuid is None:
queue_command('fix problems', user=session.get('email_address', ''))
config_utils.queue_command('fix problems', user=session.get('email_address', ''))
except Exception:
pass
@ -107,17 +101,17 @@ def serve_view(page_name):
if page_view and hasattr(page_view, 'collect_tokens'):
tokens.update(page_view.collect_tokens(cfg))
if page_name == 'radius' and not os.path.exists(f'{CONFIGS_DIR}/.radius-secret'):
queue_command('gen radius')
if page_name == 'radius' and not os.path.exists(f'{config_utils.CONFIGS_DIR}/.radius-secret'):
config_utils.queue_command('gen radius')
flash_html = ''
for category, message in get_flashed_messages(with_categories=True):
variant = {'error': 'danger', 'warning': 'warning', 'success': 'success'}.get(category, 'info')
msg_html = message if isinstance(message, Markup) else e(message)
msg_html = message if isinstance(message, Markup) else factory.e(message)
flash_html += f'<div class="info-bar info-bar-{variant} info-bar-flash"><span>{msg_html}</span></div>'
content_html = flash_html + build_items(view_def.get('items', []), tokens, view_req)
return render_layout(page_name, content_html, tokens, page_name=page_name)
content_html = flash_html + factory.build_items(view_def.get('items', []), tokens, view_req)
return factory.render_layout(page_name, content_html, tokens, page_name=page_name)
# Register blueprints =================================================
@ -151,7 +145,7 @@ def _seed_initial_account():
email = os.environ.get('INITIAL_MANAGER_EMAIL', '').strip().lower()
if not email:
try:
with open(ACCOUNTS_FILE) as f:
with open(config_utils.ACCOUNTS_FILE) as f:
data = json.load(f)
except Exception:
data = {'accounts': []}
@ -160,7 +154,7 @@ def _seed_initial_account():
'Set it in docker-compose.yml to seed the initial manager account.', file=sys.stderr)
return
try:
with open(ACCOUNTS_FILE) as f:
with open(config_utils.ACCOUNTS_FILE) as f:
data = json.load(f)
except Exception:
data = {'accounts': []}
@ -172,7 +166,7 @@ def _seed_initial_account():
'hashed_password': '',
'timezone': '',
}]
with open(ACCOUNTS_FILE, 'w') as f:
with open(config_utils.ACCOUNTS_FILE, 'w') as f:
json.dump(data, f, indent=2)
print(f'[main] Seeded initial manager account: {email}', file=sys.stderr)