Development
This commit is contained in:
parent
59ac3c5973
commit
3d0dc265ba
31 changed files with 1093 additions and 2794 deletions
|
|
@ -1,7 +1,14 @@
|
|||
import os, json, sys
|
||||
from flask import Flask
|
||||
from config_utils import ACCOUNTS_FILE
|
||||
from view_common import bp as view_page_bp
|
||||
import os, json, sys, importlib.util as _importlib_util
|
||||
from flask import Flask, Blueprint, session, redirect, get_flashed_messages
|
||||
from markupsafe import Markup
|
||||
from config_utils import (
|
||||
ACCOUNTS_FILE, APP_DIR, CONFIGS_DIR, HEALTH_FILE,
|
||||
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,
|
||||
)
|
||||
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
|
||||
|
|
@ -27,7 +34,82 @@ from api_apply_health import bp as api_apply_health_bp
|
|||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = os.environ.get('SECRET_KEY', os.urandom(24))
|
||||
app.register_blueprint(view_page_bp)
|
||||
|
||||
# View blueprint ======================================================
|
||||
|
||||
bp = Blueprint('view_page', __name__)
|
||||
|
||||
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')
|
||||
if not os.path.exists(path):
|
||||
page_view_cache[page_name] = None
|
||||
else:
|
||||
spec = _importlib_util.spec_from_file_location(f'page_view_{page_name}', path)
|
||||
mod = _importlib_util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
page_view_cache[page_name] = mod
|
||||
return page_view_cache[page_name]
|
||||
|
||||
|
||||
@bp.route('/')
|
||||
def index():
|
||||
return serve_view('overview')
|
||||
|
||||
@bp.route('/<page_name>')
|
||||
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'))
|
||||
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):
|
||||
return redirect('/overview' if level > 0 else '/accountlogin')
|
||||
|
||||
cfg = load_config()
|
||||
|
||||
if level >= LEVEL_RANK['administrator']:
|
||||
try:
|
||||
st = json.load(open(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')
|
||||
if fix_uuid is None:
|
||||
queue_command('fix problems', user=session.get('email_address', ''))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
page_view = load_page_view(page_name)
|
||||
tokens = {}
|
||||
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')
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
# Register blueprints =================================================
|
||||
|
||||
app.register_blueprint(bp)
|
||||
app.register_blueprint(actions_bp)
|
||||
app.register_blueprint(bannedips_bp)
|
||||
app.register_blueprint(ddns_bp)
|
||||
|
|
@ -51,6 +133,7 @@ app.register_blueprint(mdns_bp)
|
|||
app.register_blueprint(radius_bp)
|
||||
app.register_blueprint(api_apply_health_bp)
|
||||
|
||||
|
||||
def _seed_initial_account():
|
||||
email = os.environ.get('INITIAL_MANAGER_EMAIL', '').strip().lower()
|
||||
if not email:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue