added json editor for configuration files
This commit is contained in:
+63
-2
@@ -6,7 +6,7 @@ from datetime import datetime
|
||||
|
||||
from flask import Blueprint, render_template, request, jsonify, redirect, url_for, current_app
|
||||
from app.state import current_config
|
||||
from app.utils.helpers import load_configs, load_default_sounds
|
||||
from app.utils.helpers import load_configs, load_default_sounds, load_soundboard_configs
|
||||
|
||||
admin_bp = Blueprint('admin', __name__)
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -14,7 +14,8 @@ logger = logging.getLogger(__name__)
|
||||
@admin_bp.route('/')
|
||||
def admin():
|
||||
configs = load_configs()
|
||||
return render_template('admin.html', configs=configs)
|
||||
sb_configs = load_soundboard_configs(current_app.config['SOUNDBOARD_CONFIG_DIR'])
|
||||
return render_template('admin.html', configs=configs, sb_configs=sb_configs)
|
||||
|
||||
|
||||
@admin_bp.route('/edit/<filename>', methods=['GET', 'POST'])
|
||||
@@ -112,3 +113,63 @@ def admin_logs(date=None):
|
||||
dates=dates,
|
||||
today=current_month)
|
||||
pass
|
||||
|
||||
|
||||
def _slugify(name: str) -> str:
|
||||
return "".join(c.lower() if c.isalnum() else "_" for c in name).strip("_") or "config"
|
||||
|
||||
|
||||
@admin_bp.route('/create_config', methods=['POST'])
|
||||
def admin_create_config():
|
||||
data = request.get_json() or {}
|
||||
name = data.get('name') or 'Neue Lok'
|
||||
hub_id = int(data.get('hub_id', 0))
|
||||
hub_type = data.get('hub_type', '4channel')
|
||||
image = data.get('image')
|
||||
|
||||
filename = _slugify(name) + '.json'
|
||||
path = os.path.join(current_app.config['CONFIG_DIR'], filename)
|
||||
if os.path.exists(path):
|
||||
return jsonify({"success": False, "message": "Datei existiert bereits"}), 400
|
||||
|
||||
template = {
|
||||
"name": name,
|
||||
"image": image or "",
|
||||
"hub_id": hub_id,
|
||||
"hub_type": hub_type,
|
||||
"channels": [],
|
||||
"sounds": []
|
||||
}
|
||||
os.makedirs(current_app.config['CONFIG_DIR'], exist_ok=True)
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(template, f, ensure_ascii=False, indent=2)
|
||||
logger.info(f"Neue Config angelegt: {filename}")
|
||||
return jsonify({"success": True, "filename": filename})
|
||||
|
||||
|
||||
@admin_bp.route('/create_theme', methods=['POST'])
|
||||
def admin_create_theme():
|
||||
data = request.get_json() or {}
|
||||
name = data.get('name') or 'Neues Theme'
|
||||
folder = data.get('folder') or _slugify(name)
|
||||
filename = data.get('filename') or 'theme.json'
|
||||
|
||||
base_dir = current_app.config['SOUNDBOARD_CONFIG_DIR']
|
||||
theme_dir = os.path.join(base_dir, folder)
|
||||
os.makedirs(theme_dir, exist_ok=True)
|
||||
path = os.path.join(theme_dir, filename)
|
||||
if os.path.exists(path):
|
||||
return jsonify({"success": False, "message": "Theme-Datei existiert bereits"}), 400
|
||||
|
||||
template = {
|
||||
"name": name,
|
||||
"backgrounds": [],
|
||||
"sounds": [],
|
||||
"random_pool": [],
|
||||
"random_limit_per_hour": 2,
|
||||
"random_window_minutes": 60
|
||||
}
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(template, f, ensure_ascii=False, indent=2)
|
||||
logger.info(f"Neues Theme angelegt: {os.path.relpath(path, base_dir)}")
|
||||
return jsonify({"success": True, "path": os.path.relpath(path, base_dir)})
|
||||
|
||||
Reference in New Issue
Block a user