modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 19:40:07 +01:00
parent 0deebc2457
commit 5f3e207eba

34
app.py
View File

@@ -140,6 +140,9 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
def get_profile_picture(user_id, avatar_url): def get_profile_picture(user_id, avatar_url):
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, zuerst aus Redis, dann aus dem lokalen Speicher und zuletzt durch Download.""" """Gibt den Pfad zum Profilbild eines Benutzers zurück, zuerst aus Redis, dann aus dem lokalen Speicher und zuletzt durch Download."""
# Debug: Print avatar_url for each call
print(f"Fetching profile picture for user {user_id} with avatar_url: {avatar_url}")
# 1. Prüfen, ob das Bild in Redis gecached ist # 1. Prüfen, ob das Bild in Redis gecached ist
cached_image = r.get(str(user_id)) cached_image = r.get(str(user_id))
if cached_image: if cached_image:
@@ -153,10 +156,11 @@ def get_profile_picture(user_id, avatar_url):
r.set(str(user_id), local_image_path) # Speichern in Redis r.set(str(user_id), local_image_path) # Speichern in Redis
return local_image_path return local_image_path
# 3. Wenn das Bild weder in Redis noch lokal ist, herunterladen und speichern # 3. Download und Cache des Bilds
print(f"Downloading profile picture for user {user_id}") print(f"Downloading profile picture for user {user_id}")
return fetch_and_cache_profile_picture(user_id, avatar_url) return fetch_and_cache_profile_picture(user_id, avatar_url)
@app.context_processor @app.context_processor
def utility_processor(): def utility_processor():
def get_profile_picture(user_id, avatar_url): def get_profile_picture(user_id, avatar_url):
@@ -586,31 +590,33 @@ def user_dashboard(guild_id):
@app.route("/user_dashboard/<int:guild_id>/leaderboard") @app.route("/user_dashboard/<int:guild_id>/leaderboard")
def leaderboard(guild_id): def leaderboard(guild_id):
"""Zeigt das Level Leaderboard für einen bestimmten Server an.""" """Zeigt das Level- und XP-Leaderboard für die Benutzer eines Servers an."""
if "discord_user" in session: try:
connection = get_db_connection() connection = get_db_connection()
cursor = connection.cursor(dictionary=True) cursor = connection.cursor(dictionary=True)
current_date = datetime.now()
one_month_ago = current_date - timedelta(days=30)
cursor.execute(""" cursor.execute("""
SELECT nickname, profile_picture, level, xp, join_date SELECT user_id, nickname, profile_picture, level, xp
FROM user_data FROM user_data
WHERE guild_id = %s WHERE guild_id = %s AND (ban = 0) AND (leave_date IS NULL OR leave_date > NOW() - INTERVAL 1 MONTH)
AND ban = 0
AND (leave_date IS NULL OR leave_date > %s)
ORDER BY level DESC, xp DESC ORDER BY level DESC, xp DESC
""", (guild_id, one_month_ago)) LIMIT 100
""", (guild_id,))
leaderboard_data = cursor.fetchall() leaderboard_data = cursor.fetchall()
# Debug: Print profile_picture URLs for all users in the leaderboard
for user in leaderboard_data:
print(f"User ID: {user['user_id']}, Profile Picture: {user['profile_picture']}")
cursor.close() cursor.close()
connection.close() connection.close()
# Übergabe von enumerate an das Template return render_template("leaderboard.html", leaderboard=leaderboard_data, guild_id=guild_id)
return render_template("leaderboard.html", leaderboard=leaderboard_data, guild_id=guild_id, enumerate=enumerate) except mysql.connector.Error as e:
return redirect(url_for("landing_page")) print(f"Database error: {e}")
return "Database error", 500
@app.route("/server_giveaways/<int:guild_id>") @app.route("/server_giveaways/<int:guild_id>")