From bea9a109e10418ac5b737336027bc6ffa4b68e6c Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Thu, 12 Sep 2024 16:09:57 +0200 Subject: [PATCH] modified: app.py modified: templates/admin_giveaways.html modified: templates/redeem_giveaway.html modified: templates/user_giveaways.html --- app.py | 175 ++++++++------------------------- templates/admin_giveaways.html | 45 +++++---- templates/redeem_giveaway.html | 60 +++++++---- templates/user_giveaways.html | 33 +++++-- 4 files changed, 132 insertions(+), 181 deletions(-) diff --git a/app.py b/app.py index 2a5037f..4bd1f53 100644 --- a/app.py +++ b/app.py @@ -411,154 +411,61 @@ def download_logs(): return redirect(url_for("landing_page")) @app.route("/admin/giveaways", methods=["GET", "POST"]) -def manage_giveaways(): - """Verwalte alle Giveaways (nur für Admins).""" - if "discord_user" in session: - user_info = session["discord_user"] - user_id = user_info["id"] - - # Überprüfe, ob der Benutzer Admin-Rechte hat - connection = get_db_connection() # Verbindung zur User-Datenbank +def admin_giveaways(): + """Zeigt eine Liste aller Giveaways an und ermöglicht das Bearbeiten und Sortieren.""" + if is_admin(): + connection = get_giveaway_db_connection() # Verbindung zur Giveaway-Datenbank cursor = connection.cursor(dictionary=True) - cursor.execute("SELECT permission FROM user_data WHERE user_id = %s", (user_id,)) - user_data = cursor.fetchone() + # Sortierung nach bestimmten Feldern + sort_field = request.args.get("sort", "id") # Standardmäßig nach 'id' sortieren + order = request.args.get("order", "asc") # Standardmäßig aufsteigend sortieren - if user_data and user_data["permission"] >= 8: - connection.close() - - # Verbindung zur Giveaway-Datenbank herstellen - giveaway_connection = get_giveaway_db_connection() - giveaway_cursor = giveaway_connection.cursor(dictionary=True) - - # Alle Giveaways aus der Giveaway-Datenbank abrufen - giveaway_cursor.execute("SELECT * FROM giveaways") - giveaways = giveaway_cursor.fetchall() - - giveaway_cursor.close() - giveaway_connection.close() - - return render_template("admin_giveaways.html", user_info=user_info, giveaways=giveaways) - else: - return redirect(url_for("user_dashboard")) + # Holen aller Giveaways aus der Datenbank + cursor.execute(f"SELECT * FROM giveaways ORDER BY {sort_field} {order}") + giveaways = cursor.fetchall() + + cursor.close() + connection.close() + return render_template("admin_giveaways.html", giveaways=giveaways, sort_field=sort_field, order=order) return redirect(url_for("login")) - -@app.route("/admin/giveaway/edit/", methods=["GET", "POST"]) -def edit_giveaway(id): - """Bearbeite ein bestehendes Giveaway.""" - if "discord_user" in session: - user_info = session["discord_user"] - user_id = user_info["id"] - - # Überprüfe, ob der Benutzer Admin-Rechte hat - connection = get_db_connection() +@app.route("/admin/giveaways/edit/", methods=["GET", "POST"]) +def edit_giveaway(giveaway_id): + """Bearbeitet ein spezifisches Giveaway.""" + if is_admin(): + connection = get_giveaway_db_connection() # Verbindung zur Giveaway-Datenbank cursor = connection.cursor(dictionary=True) - cursor.execute("SELECT permission FROM user_data WHERE user_id = %s", (user_id,)) - user_data = cursor.fetchone() + if request.method == "POST": + platform = request.form.get("platform") + name = request.form.get("name") + game_key = request.form.get("game_key") + winner_dc_id = request.form.get("winner_dc_id") + aktiv = bool(request.form.get("aktiv")) - if user_data and user_data["permission"] >= 8: - connection.close() + # Update der Giveaways-Daten + cursor.execute(""" + UPDATE giveaways + SET platform = %s, name = %s, game_key = %s, winner_dc_id = %s, aktiv = %s + WHERE id = %s + """, (platform, name, game_key, winner_dc_id, aktiv, giveaway_id)) + connection.commit() - giveaway_connection = get_giveaway_db_connection() - cursor = giveaway_connection.cursor(dictionary=True) + flash("Giveaway updated successfully!", "success") + return redirect(url_for("admin_giveaways")) - if request.method == "POST": - platform = request.form["platform"] - name = request.form["name"] - game_key = request.form["game_key"] - winner_dc_id = request.form["winner_dc_id"] - - # Update the giveaway - cursor.execute(""" - UPDATE giveaways - SET platform = %s, name = %s, game_key = %s, winner_dc_id = %s - WHERE id = %s - """, (platform, name, game_key, winner_dc_id, id)) - giveaway_connection.commit() - - cursor.close() - giveaway_connection.close() - - return redirect(url_for("manage_giveaways")) - - # Hole die aktuellen Daten des Giveaways - cursor.execute("SELECT * FROM giveaways WHERE id = %s", (id,)) - giveaway = cursor.fetchone() - - cursor.close() - giveaway_connection.close() - - return render_template("edit_giveaway.html", giveaway=giveaway) + # Daten des spezifischen Giveaways laden + cursor.execute("SELECT * FROM giveaways WHERE id = %s", (giveaway_id,)) + giveaway = cursor.fetchone() + + cursor.close() + connection.close() + return render_template("edit_giveaway.html", giveaway=giveaway) return redirect(url_for("login")) -@app.route("/admin/giveaway/delete/", methods=["POST"]) -def delete_giveaway(id): - """Lösche ein Giveaway aus der Datenbank.""" - if "discord_user" in session: - user_info = session["discord_user"] - user_id = user_info["id"] - - connection = get_db_connection() - cursor = connection.cursor(dictionary=True) - - cursor.execute("SELECT permission FROM user_data WHERE user_id = %s", (user_id,)) - user_data = cursor.fetchone() - - if user_data and user_data["permission"] >= 8: - connection.close() - - # Verbindung zur Giveaway-Datenbank herstellen - giveaway_connection = get_giveaway_db_connection() - cursor = giveaway_connection.cursor() - - cursor.execute("DELETE FROM giveaways WHERE id = %s", (id,)) - giveaway_connection.commit() - - cursor.close() - giveaway_connection.close() - - return redirect(url_for("manage_giveaways")) - - return redirect(url_for("login")) - -@app.route("/admin/giveaway/toggle_active/", methods=["POST"]) -def toggle_active(id): - """Aktiviere oder deaktiviere ein Giveaway.""" - if "discord_user" in session: - user_info = session["discord_user"] - user_id = user_info["id"] - - connection = get_db_connection() - cursor = connection.cursor(dictionary=True) - - cursor.execute("SELECT permission FROM user_data WHERE user_id = %s", (user_id,)) - user_data = cursor.fetchone() - - if user_data and user_data["permission"] >= 8: - connection.close() - - giveaway_connection = get_giveaway_db_connection() - cursor = giveaway_connection.cursor(dictionary=True) - - # Hole den aktuellen Status - cursor.execute("SELECT aktiv FROM giveaways WHERE id = %s", (id,)) - giveaway = cursor.fetchone() - - # Toggle den Aktiv-Status - new_status = not giveaway["aktiv"] - cursor.execute("UPDATE giveaways SET aktiv = %s WHERE id = %s", (new_status, id)) - giveaway_connection.commit() - - cursor.close() - giveaway_connection.close() - - return redirect(url_for("manage_giveaways")) - - return redirect(url_for("login")) @app.route("/user/giveaways", methods=["GET"]) def user_giveaways(): diff --git a/templates/admin_giveaways.html b/templates/admin_giveaways.html index 22ee8c5..1f4e6f0 100644 --- a/templates/admin_giveaways.html +++ b/templates/admin_giveaways.html @@ -4,8 +4,14 @@ - Manage Giveaways + Admin - Giveaways + @@ -25,15 +31,12 @@

Giveaways Management

-

Manage all active and inactive giveaways in the system.

- - - +
+ - - - - + + + @@ -46,26 +49,26 @@ - - - + + {% endfor %}
IDPlatformNameUUIDIDPlatformName Game Key Winner Discord ID Active{{ giveaway.id }} {{ giveaway.platform }} {{ giveaway.name }}{{ giveaway.uuid }} {{ giveaway.game_key }}{{ giveaway.winner_dc_id }}{{ 'Yes' if giveaway.aktiv else 'No' }}{{ giveaway.winner_dc_id or 'Not Assigned' }} - Edit -
- -
-
- -
+ {% if giveaway.aktiv %} + Active + {% else %} + Inactive + {% endif %} +
+ Edit
+ + + diff --git a/templates/redeem_giveaway.html b/templates/redeem_giveaway.html index 230c9f6..f7139ba 100644 --- a/templates/redeem_giveaway.html +++ b/templates/redeem_giveaway.html @@ -6,37 +6,57 @@ Redeem Giveaway +

Giveaway Prize

-

Congratulations! You have won the giveaway for {{ giveaway.name }} on - {{ giveaway.platform }}.

+

Congratulations! You've won {{ giveaway.name }} on {{ giveaway.platform }}.

- {% if key %} - -
-
-
Your Key
-

{{ key }}

+
+ {% if key %} + +
Your Game Key
+

{{ key }}

+ -
- Back to Giveaways - {% else %} - -
-
-
Claim Your Key
-

Do you want to reveal your game key? Once revealed, it will be marked as claimed and cannot be undone.

- + {% else %} + +
Claim Your Key
+

Do you want to reveal your game key? Once revealed, it will be marked as claimed and cannot be undone.

+
- +
+ + {% endif %}
- Back to Giveaways - {% endif %}
diff --git a/templates/user_giveaways.html b/templates/user_giveaways.html index 9af6930..b9746c1 100644 --- a/templates/user_giveaways.html +++ b/templates/user_giveaways.html @@ -6,29 +6,50 @@ Your Giveaways +

Your Giveaways

-

Here you can view the giveaways you've won.

+

Here are the giveaways you've won. Click "Claim Your Key" to receive your prize.

{% if giveaways %} {% for giveaway in giveaways %}
-
-
+
+
{{ giveaway.name }}

Platform: {{ giveaway.platform }}

- Claim Your - Key + Claim Your Key
{% endfor %} {% else %} -

You haven't won any giveaways yet.

+
+
+

No Giveaways Won

+

It looks like you haven't won any giveaways yet. Keep participating and good luck!

+
+
{% endif %}