dashboard fix

This commit is contained in:
2025-02-19 21:22:11 +01:00
parent d5a92d187f
commit e097facef7
3 changed files with 281 additions and 191 deletions

View File

@@ -444,40 +444,54 @@ def dashboard():
user_id = session['user_id']
db = get_db()
# User abfragen
# User und Settings ermitteln
user = db.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
if not user:
flash("Benutzer nicht gefunden.", "red")
return redirect(url_for('logout'))
# Settings aus user_settings
settings = db.execute("SELECT * FROM user_settings WHERE user_id = ?", (user_id,)).fetchone()
# Standardwerte
# Standardwerte aus settings
if not settings:
wallpaper = '19.png'
city = 'Berlin'
show_forecast = True
bookmarks = []
# (Dieses 'bookmarks' ist optional, kannst du weglassen,
# wenn du ausschließlich die DB-Tabelle 'bookmarks' nutzt)
bookmarks_setting = []
else:
wallpaper = settings['wallpaper']
city = settings['city']
show_forecast = bool(settings['show_forecast'])
if settings['bookmarks']:
bookmarks = settings['bookmarks'].split(",")
bookmarks_setting = settings['bookmarks'].split(",") # Nur als Fallback
else:
bookmarks = []
bookmarks_setting = []
# Wetter holen (wenn gewünscht)
current_temp, weather_icon, forecast = get_weather(city)
if current_temp is None:
current_temp = "N/A"
weather_icon = "fa-question"
forecast = []
# DB-Bookmarks für den eingeloggten User
user_bookmarks = db.execute("""
SELECT id, title, url, icon_class
FROM bookmarks
WHERE user_id=?
ORDER BY id DESC
""", (user_id,)).fetchall()
# Domain, Logo usw.
domain = "example.com"
logo_path = url_for('static', filename='clickcandit.png')
# Notifications für diesen User (user_id) oder globale (user_id IS NULL)
notifications = db.execute("""
SELECT id, user_id, message, created_at
FROM notifications
WHERE user_id=? OR user_id IS NULL
ORDER BY created_at DESC
""", (user_id,)).fetchall()
# Ggf. Time-Entries (falls du sie zeigen willst)
time_entries = db.execute("""
SELECT *
FROM time_entries
WHERE user_id=?
ORDER BY start_time DESC
""", (user_id,)).fetchall()
# Beispiel-Apps
user_app_chunks = [
@@ -499,19 +513,33 @@ def dashboard():
]
]
# Wetter (Dummy oder API)
current_temp, weather_icon, forecast = get_weather(city)
if current_temp is None:
current_temp = "N/A"
weather_icon = "fa-question"
forecast = []
domain = "clickcandit.com/login"
logo_path = url_for('static', filename='clickcandit.png')
return render_template(
'dashboard.html',
user=user['username'],
wallpaper=wallpaper,
city=city,
show_forecast=show_forecast,
bookmarks=bookmarks,
bookmarks=bookmarks_setting, # falls noch gebraucht, sonst weglassen
current_temp=current_temp,
weather_icon=weather_icon,
forecast=forecast,
domain=domain,
logo_path=logo_path,
user_app_chunks=user_app_chunks
user_app_chunks=user_app_chunks,
# Hier die neuen Variablen:
user_bookmarks=user_bookmarks,
notifications=notifications,
time_entries=time_entries
)
# ------------------------------------------------------------
@@ -590,6 +618,66 @@ def inject_wallpaper():
"WALLPAPER_URL": url_for('static', filename='24.png')
}
@app.route('/admin/notifications/multi', methods=['POST'])
def add_notification_multi():
if not is_admin():
flash("Zugriff verweigert!", "red")
return redirect(url_for('dashboard'))
message = request.form.get('message')
target_list = request.form.getlist('target_users') # ['all'] oder ['1','2'] etc.
if not message:
flash("Bitte eine Nachricht eingeben.", "red")
return redirect(url_for('admin_panel'))
db = get_db()
# Wenn 'all' in der Liste, Notification für alle
if 'all' in target_list:
db.execute("INSERT INTO notifications (user_id, message) VALUES (NULL, ?)", (message,))
else:
# Sonst für jede ausgewählte ID
for uid in target_list:
db.execute("INSERT INTO notifications (user_id, message) VALUES (?, ?)", (uid, message))
db.commit()
flash("Benachrichtigungen erstellt!", "green")
return redirect(url_for('admin_panel'))
@app.route('/admin/bookmarks/multi', methods=['POST'])
def add_bookmarks_multi():
if not is_admin():
flash("Zugriff verweigert!", "red")
return redirect(url_for('dashboard'))
# Checkboxen im Template: name='target_users'
target_list = request.form.getlist('target_users') # Liste der IDs (Strings)
title = request.form.get('title')
url_ = request.form.get('url')
icon = request.form.get('icon_class', 'fas fa-bookmark')
# Ggf. Validierung
if not title or not url_:
flash("Bitte Titel und URL angeben!", "red")
return redirect(url_for('admin_panel'))
if not target_list:
flash("Bitte mindestens einen Benutzer auswählen!", "red")
return redirect(url_for('admin_panel'))
db = get_db()
for uid in target_list:
db.execute(
"INSERT INTO bookmarks (user_id, title, url, icon_class) VALUES (?, ?, ?, ?)",
(uid, title, url_, icon)
)
db.commit()
flash("Neues Lesezeichen für mehrere Benutzer hinzugefügt!", "green")
return redirect(url_for('admin_panel'))
@app.route('/save_settings', methods=['POST'])
def save_settings():
"""