From 9979efbf7a3927003055b69f23eb33a34c86c948 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 23 Dec 2022 03:01:24 +0200 Subject: [PATCH] make it possible to edit badge attributes without removing and re-granting them --- files/routes/admin.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/files/routes/admin.py b/files/routes/admin.py index a28da9aea..235383490 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -463,24 +463,33 @@ def badge_grant_post(v): except: abort(400) if badge_id not in [b.id for b in badges]: - abort(403) - - if user.has_badge(badge_id): - return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, error="User already has that badge.") - - new_badge = Badge(badge_id=badge_id, user_id=user.id) - - desc = request.values.get("description") - if desc: new_badge.description = desc + abort(403, "You can't grant this badge!") + description = request.values.get("description") url = request.values.get("url") + if url: if '\\' in url: abort(400) if url.startswith(SITE_FULL): url = url.split(SITE_FULL, 1)[1] elif url.startswith(BAN_EVASION_FULL): url = url.split(BAN_EVASION_FULL, 1)[1] - new_badge.url = url + + existing = user.has_badge(badge_id) + if existing: + if url or description: + existing.url = url + existing.description = description + g.db.add(existing) + return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, msg="Badge attributes edited successfully!") + return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, error="User already has that badge.") + + new_badge = Badge( + badge_id=badge_id, + user_id=user.id, + url=url, + description=description + ) g.db.add(new_badge) g.db.flush() @@ -496,6 +505,7 @@ def badge_grant_post(v): _note=new_badge.name ) g.db.add(ma) + return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, msg=f"{new_badge.name} Badge granted to @{user.username} successfully!") @app.post("/admin/badge_remove")