From 0a63ceca7f3cd2663ce8a83569a7e64f12634162 Mon Sep 17 00:00:00 2001 From: oberon Date: Wed, 11 Feb 2026 19:35:42 +0100 Subject: [PATCH] added admin functions --- app.py | 70 ++++++++++++++++++++++- templates/admin.html | 113 ++++++++++++++++++++++++++++++++------ templates/admin_edit.html | 44 +++++++++++++++ templates/admin_logs.html | 17 ++++++ 4 files changed, 227 insertions(+), 17 deletions(-) create mode 100644 templates/admin_edit.html create mode 100644 templates/admin_logs.html diff --git a/app.py b/app.py index 46c05c8..557bad9 100644 --- a/app.py +++ b/app.py @@ -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/', 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/', 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/') def serve_config_file(filename): return send_from_directory(app.config['CONFIG_DIR'], filename) diff --git a/templates/admin.html b/templates/admin.html index 1068d29..dd0ead0 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -4,27 +4,108 @@ {% block content %}
-

Admin-Bereich

-

Konfigurationen verwalten, Logs einsehen, etc.

+

Admin-Bereich

+ + + +

Vorhandene Konfigurationen

-

Vorhandene Konfigurationen

{% if configs %} -
    - {% for cfg in configs %} -
  • - {{ cfg.name }} ({{ cfg.filename }}) - - Bearbeiten - -
  • - {% endfor %} -
+
+ + + + + + + + + + + {% for cfg in configs %} + + + + + + + {% endfor %} + +
NameHub-IDDateiAktionen
{{ cfg.name }}{{ cfg.hub_id | default('–') }}{{ cfg.filename }} + + Bearbeiten + + +
+
{% else %} -
Noch keine Konfigurationen vorhanden.
+
+ Noch keine Konfigurationen vorhanden. Erstelle eine neue. +
{% endif %} +
-
- Zurück zur Übersicht + + + +{% endblock %} + +{% block scripts %} + {% endblock %} \ No newline at end of file diff --git a/templates/admin_edit.html b/templates/admin_edit.html new file mode 100644 index 0000000..7ee7a45 --- /dev/null +++ b/templates/admin_edit.html @@ -0,0 +1,44 @@ +{% extends "base.html" %} + +{% block title %}Bearbeite {{ filename }}{% endblock %} + +{% block content %} +
+

Bearbeite Konfiguration

+

{{ filename }}

+ + {% if error %} + + {% endif %} + +
+
+ + +
+
+ + Abbrechen +
+
+ +
+
Hilfe – Beispielstruktur
+
+{
+  "name": "BR 01 Dampflok",
+  "image": "br01.jpg",
+  "hub_id": 0,
+  "hub_type": "6channel",
+  "channels": [
+    {"port": "A", "type": "motor", "name": "Fahrtrichtung", "invert": false},
+    {"port": "C", "type": "light", "name": "Spitzenlicht", "on_value": 1.0}
+  ]
+}
+    
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/admin_logs.html b/templates/admin_logs.html new file mode 100644 index 0000000..2005184 --- /dev/null +++ b/templates/admin_logs.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} + +{% block title %}Logs – MK Control{% endblock %} + +{% block content %} +
+

Log-Datei (app.log)

+ +
+{{ logs | safe }}
+  
+ + +
+{% endblock %} \ No newline at end of file