From e86271ab9a49cf4183d03bd998f485d599955338 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Tue, 3 Sep 2024 11:49:52 +0200 Subject: [PATCH] modified: app.py modified: templates/index.html modified: templates/settings.html --- app.py | 53 ++++++++++++++++++++++------------------- templates/index.html | 3 +-- templates/settings.html | 8 +++---- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/app.py b/app.py index f992d74..1ce1228 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,6 @@ from flask import Flask, render_template, redirect, url_for, request, session import os import subprocess -import psutil import mysql.connector app = Flask(__name__) @@ -11,12 +10,13 @@ app.secret_key = os.getenv("FLASK_SECRET_KEY", "default_secret_key") DB_HOST = os.getenv("DB_HOST") DB_PORT = os.getenv("DB_PORT") DB_USER = os.getenv("DB_USER") -DB_PASS = os.getenv("DB_PASSWORD") -DB_NAME = os.getenv("DB_DATABASE") +DB_PASS = os.getenv("DB_PASS") +DB_NAME = os.getenv("DB_NAME") -# Globale Variablen für die Intros +# Globale Variablen für die Intros und Themes INTRO_FILE = "introduction.txt" ASKNOTES_INTRO_FILE = "asknotesintro.txt" +THEME_FILE = "theme.txt" # Speichern der Prozess-ID bot_process = None @@ -68,6 +68,21 @@ def get_db_connection(): database=DB_NAME ) +def get_current_theme(): + """Lädt das aktuelle Theme.""" + return load_text_file(THEME_FILE) or "light" # Standard-Theme ist 'light' + +def set_current_theme(theme): + """Speichert das aktuelle Theme.""" + save_text_file(THEME_FILE, theme) + +@app.route("/") +def index(): + if "username" in session: + theme = get_current_theme() + return render_template("index.html", bot_running=bot_status(), theme=theme) + return redirect(url_for("login")) + @app.route("/login", methods=["GET", "POST"]) def login(): if request.method == "POST": @@ -78,7 +93,8 @@ def login(): return redirect(url_for("index")) else: return "Invalid credentials!" - return render_template("login.html") + theme = get_current_theme() + return render_template("login.html", theme=theme) @app.route("/logout") def logout(): @@ -107,46 +123,33 @@ def settings(): asknotes_introduction = request.form.get("asknotes_introduction") theme = request.form.get("theme") - # Speichern der Intros + # Speichern der Intros und des Themes save_text_file(INTRO_FILE, introduction) save_text_file(ASKNOTES_INTRO_FILE, asknotes_introduction) - - # Speichern des ausgewählten Themes in der Session - session['theme'] = theme + set_current_theme(theme) return redirect(url_for("settings")) # Laden der aktuellen Inhalte aus den Textdateien introduction = load_text_file(INTRO_FILE) asknotes_introduction = load_text_file(ASKNOTES_INTRO_FILE) + current_theme = get_current_theme() - return render_template("settings.html", introduction=introduction, asknotes_introduction=asknotes_introduction, theme=session.get('theme', 'light')) - return redirect(url_for("login")) - -@app.route("/") -def index(): - if "username" in session: - # Sicherstellen, dass das Theme gesetzt ist - if 'theme' not in session: - session['theme'] = 'light' # Standard-Theme setzen - return render_template("index.html", bot_running=bot_status(), theme=session['theme']) + return render_template("settings.html", introduction=introduction, asknotes_introduction=asknotes_introduction, current_theme=current_theme) return redirect(url_for("login")) @app.route("/users") def users(): + """Zeigt eine Liste aller Benutzer aus der Datenbank an.""" if "username" in session: - # Sicherstellen, dass das Theme gesetzt ist - if 'theme' not in session: - session['theme'] = 'light' - connection = get_db_connection() cursor = connection.cursor(dictionary=True) cursor.execute("SELECT * FROM user_data") users = cursor.fetchall() cursor.close() connection.close() - - return render_template("users.html", users=users, theme=session['theme']) + theme = get_current_theme() + return render_template("users.html", users=users, theme=theme) return redirect(url_for("login")) if __name__ == "__main__": diff --git a/templates/index.html b/templates/index.html index b885e9f..fa11c03 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,9 +3,8 @@ - Admin Panel - +
diff --git a/templates/settings.html b/templates/settings.html index 6556399..8d92ecf 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -3,7 +3,6 @@ - Settings @@ -20,12 +19,13 @@
-
+