added admin functions
This commit is contained in:
@@ -76,13 +76,81 @@ def control_page():
|
||||
return render_template('control.html', config=current_config)
|
||||
|
||||
|
||||
# ── Admin ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@app.route('/admin')
|
||||
def admin():
|
||||
"""Admin-Übersicht: Configs anzeigen, bearbeiten, Logs etc."""
|
||||
configs = load_configs()
|
||||
return render_template('admin.html', configs=configs)
|
||||
|
||||
|
||||
@app.route('/admin/edit/<filename>', methods=['GET', 'POST'])
|
||||
def admin_edit_config(filename):
|
||||
path = os.path.join(app.config['CONFIG_DIR'], filename)
|
||||
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
new_content = request.form.get('config_content')
|
||||
if not new_content:
|
||||
raise ValueError("Kein Inhalt übermittelt")
|
||||
|
||||
# Validierung: versuche zu parsen
|
||||
json.loads(new_content)
|
||||
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
|
||||
logger.info(f"Config {filename} erfolgreich gespeichert")
|
||||
return redirect(url_for('admin'))
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Ungültiges JSON in {filename}: {e}")
|
||||
error_msg = f"Ungültiges JSON: {str(e)}"
|
||||
except Exception as e:
|
||||
logger.error(f"Speicherfehler {filename}: {e}")
|
||||
error_msg = f"Fehler beim Speichern: {str(e)}"
|
||||
|
||||
# Bei Fehler: zurück zum Formular mit Fehlermeldung
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
return render_template('admin_edit.html', filename=filename, content=content, error=error_msg)
|
||||
|
||||
# GET: Formular laden
|
||||
if not os.path.exists(path):
|
||||
return "Konfiguration nicht gefunden", 404
|
||||
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
return render_template('admin_edit.html', filename=filename, content=content)
|
||||
|
||||
|
||||
@app.route('/admin/delete/<filename>', methods=['POST'])
|
||||
def admin_delete_config(filename):
|
||||
path = os.path.join(app.config['CONFIG_DIR'], filename)
|
||||
if os.path.exists(path):
|
||||
try:
|
||||
os.remove(path)
|
||||
logger.info(f"Config gelöscht: {filename}")
|
||||
return jsonify({"success": True, "message": f"{filename} gelöscht"})
|
||||
except Exception as e:
|
||||
logger.error(f"Löschfehler {filename}: {e}")
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
return jsonify({"success": False, "message": "Datei nicht gefunden"}), 404
|
||||
|
||||
|
||||
@app.route('/admin/logs')
|
||||
def admin_logs():
|
||||
log_path = 'app.log'
|
||||
if os.path.exists(log_path):
|
||||
with open(log_path, 'r', encoding='utf-8') as f:
|
||||
logs = f.read()
|
||||
else:
|
||||
logs = "Keine Logs vorhanden."
|
||||
|
||||
return render_template('admin_logs.html', logs=logs)
|
||||
|
||||
|
||||
@app.route('/configs/<path:filename>')
|
||||
def serve_config_file(filename):
|
||||
return send_from_directory(app.config['CONFIG_DIR'], filename)
|
||||
|
||||
Reference in New Issue
Block a user