fixes for new config editor
This commit is contained in:
parent
f1f2a14ccd
commit
4340a58450
@ -121,55 +121,63 @@ def _slugify(name: str) -> str:
|
|||||||
|
|
||||||
@admin_bp.route('/create_config', methods=['POST'])
|
@admin_bp.route('/create_config', methods=['POST'])
|
||||||
def admin_create_config():
|
def admin_create_config():
|
||||||
data = request.get_json() or {}
|
try:
|
||||||
name = data.get('name') or 'Neue Lok'
|
data = request.get_json(force=True, silent=True) or {}
|
||||||
hub_id = int(data.get('hub_id', 0))
|
name = data.get('name') or 'Neue Lok'
|
||||||
hub_type = data.get('hub_type', '4channel')
|
hub_id = int(data.get('hub_id', 0))
|
||||||
image = data.get('image')
|
hub_type = data.get('hub_type', '4channel')
|
||||||
|
image = data.get('image')
|
||||||
|
|
||||||
filename = _slugify(name) + '.json'
|
filename = _slugify(name) + '.json'
|
||||||
path = os.path.join(current_app.config['CONFIG_DIR'], filename)
|
path = os.path.join(current_app.config['CONFIG_DIR'], filename)
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
return jsonify({"success": False, "message": "Datei existiert bereits"}), 400
|
return jsonify({"success": False, "message": "Datei existiert bereits"}), 400
|
||||||
|
|
||||||
template = {
|
template = {
|
||||||
"name": name,
|
"name": name,
|
||||||
"image": image or "",
|
"image": image or "",
|
||||||
"hub_id": hub_id,
|
"hub_id": hub_id,
|
||||||
"hub_type": hub_type,
|
"hub_type": hub_type,
|
||||||
"channels": [],
|
"channels": [],
|
||||||
"sounds": []
|
"sounds": []
|
||||||
}
|
}
|
||||||
os.makedirs(current_app.config['CONFIG_DIR'], exist_ok=True)
|
os.makedirs(current_app.config['CONFIG_DIR'], exist_ok=True)
|
||||||
with open(path, 'w', encoding='utf-8') as f:
|
with open(path, 'w', encoding='utf-8') as f:
|
||||||
json.dump(template, f, ensure_ascii=False, indent=2)
|
json.dump(template, f, ensure_ascii=False, indent=2)
|
||||||
logger.info(f"Neue Config angelegt: {filename}")
|
logger.info(f"Neue Config angelegt: {filename}")
|
||||||
return jsonify({"success": True, "filename": filename})
|
return jsonify({"success": True, "filename": filename})
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception("create_config fehlgeschlagen")
|
||||||
|
return jsonify({"success": False, "message": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
@admin_bp.route('/create_theme', methods=['POST'])
|
@admin_bp.route('/create_theme', methods=['POST'])
|
||||||
def admin_create_theme():
|
def admin_create_theme():
|
||||||
data = request.get_json() or {}
|
try:
|
||||||
name = data.get('name') or 'Neues Theme'
|
data = request.get_json(force=True, silent=True) or {}
|
||||||
folder = data.get('folder') or _slugify(name)
|
name = data.get('name') or 'Neues Theme'
|
||||||
filename = data.get('filename') or 'theme.json'
|
folder = data.get('folder') or _slugify(name)
|
||||||
|
filename = data.get('filename') or 'theme.json'
|
||||||
|
|
||||||
base_dir = current_app.config['SOUNDBOARD_CONFIG_DIR']
|
base_dir = current_app.config['SOUNDBOARD_CONFIG_DIR']
|
||||||
theme_dir = os.path.join(base_dir, folder)
|
theme_dir = os.path.join(base_dir, folder)
|
||||||
os.makedirs(theme_dir, exist_ok=True)
|
os.makedirs(theme_dir, exist_ok=True)
|
||||||
path = os.path.join(theme_dir, filename)
|
path = os.path.join(theme_dir, filename)
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
return jsonify({"success": False, "message": "Theme-Datei existiert bereits"}), 400
|
return jsonify({"success": False, "message": "Theme-Datei existiert bereits"}), 400
|
||||||
|
|
||||||
template = {
|
template = {
|
||||||
"name": name,
|
"name": name,
|
||||||
"backgrounds": [],
|
"backgrounds": [],
|
||||||
"sounds": [],
|
"sounds": [],
|
||||||
"random_pool": [],
|
"random_pool": [],
|
||||||
"random_limit_per_hour": 2,
|
"random_limit_per_hour": 2,
|
||||||
"random_window_minutes": 60
|
"random_window_minutes": 60
|
||||||
}
|
}
|
||||||
with open(path, 'w', encoding='utf-8') as f:
|
with open(path, 'w', encoding='utf-8') as f:
|
||||||
json.dump(template, f, ensure_ascii=False, indent=2)
|
json.dump(template, f, ensure_ascii=False, indent=2)
|
||||||
logger.info(f"Neues Theme angelegt: {os.path.relpath(path, base_dir)}")
|
logger.info(f"Neues Theme angelegt: {os.path.relpath(path, base_dir)}")
|
||||||
return jsonify({"success": True, "path": os.path.relpath(path, base_dir)})
|
return jsonify({"success": True, "path": os.path.relpath(path, base_dir)})
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception("create_theme fehlgeschlagen")
|
||||||
|
return jsonify({"success": False, "message": str(e)}), 500
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 93 KiB |
@ -212,7 +212,9 @@
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data)
|
||||||
});
|
});
|
||||||
const json = await res.json();
|
const text = await res.text();
|
||||||
|
let json;
|
||||||
|
try { json = JSON.parse(text); } catch (_) { throw new Error('Serverantwort ist kein JSON:\n'+text); }
|
||||||
if (!json.success) throw new Error(json.message || 'Fehler');
|
if (!json.success) throw new Error(json.message || 'Fehler');
|
||||||
alert('Konfiguration angelegt: ' + json.filename);
|
alert('Konfiguration angelegt: ' + json.filename);
|
||||||
location.reload();
|
location.reload();
|
||||||
@ -233,7 +235,9 @@
|
|||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data)
|
||||||
});
|
});
|
||||||
const json = await res.json();
|
const text = await res.text();
|
||||||
|
let json;
|
||||||
|
try { json = JSON.parse(text); } catch (_) { throw new Error('Serverantwort ist kein JSON:\n'+text); }
|
||||||
if (!json.success) throw new Error(json.message || 'Fehler');
|
if (!json.success) throw new Error(json.message || 'Fehler');
|
||||||
alert('Theme angelegt: ' + json.path);
|
alert('Theme angelegt: ' + json.path);
|
||||||
location.reload();
|
location.reload();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user