forked from rDrama/rDrama
add mass badge granting and removal
parent
47cfa0d922
commit
52f9a99af1
|
@ -471,65 +471,72 @@ def badge_grant_get(v):
|
||||||
def badge_grant_post(v):
|
def badge_grant_post(v):
|
||||||
badges = admin_badges_grantable_list(v)
|
badges = admin_badges_grantable_list(v)
|
||||||
|
|
||||||
user = get_user(request.values.get("username"), graceful=True)
|
usernames = request.values.get("usernames", "").strip()
|
||||||
if not user:
|
if not usernames:
|
||||||
error = "User not found!"
|
error = "You must enter usernames!"
|
||||||
if v.client: return {"error": error}
|
if v.client: return {"error": error}
|
||||||
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, error=error)
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, error=error)
|
||||||
|
|
||||||
try: badge_id = int(request.values.get("badge_id"))
|
for username in usernames.split():
|
||||||
except: abort(400)
|
user = get_user(username, graceful=True)
|
||||||
|
if not user:
|
||||||
|
error = "User not found!"
|
||||||
|
if v.client: return {"error": error}
|
||||||
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, error=error)
|
||||||
|
|
||||||
if badge_id not in [b.id for b in badges]:
|
try: badge_id = int(request.values.get("badge_id"))
|
||||||
abort(403, "You can't grant this badge!")
|
except: abort(400)
|
||||||
|
|
||||||
description = request.values.get("description")
|
if badge_id not in [b.id for b in badges]:
|
||||||
url = request.values.get("url")
|
abort(403, "You can't grant this badge!")
|
||||||
|
|
||||||
if url:
|
description = request.values.get("description")
|
||||||
if '\\' in url: abort(400)
|
url = request.values.get("url")
|
||||||
if url.startswith(f'{SITE_FULL}/'):
|
|
||||||
url = url.split(SITE_FULL, 1)[1]
|
|
||||||
|
|
||||||
existing = user.has_badge(badge_id)
|
if url:
|
||||||
if existing:
|
if '\\' in url: abort(400)
|
||||||
if url or description:
|
if url.startswith(f'{SITE_FULL}/'):
|
||||||
existing.url = url
|
url = url.split(SITE_FULL, 1)[1]
|
||||||
existing.description = description
|
|
||||||
g.db.add(existing)
|
|
||||||
|
|
||||||
msg = "Badge attributes edited successfully!"
|
existing = user.has_badge(badge_id)
|
||||||
if v.client: return {"message": msg}
|
if existing:
|
||||||
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, msg=msg)
|
if url or description:
|
||||||
|
existing.url = url
|
||||||
|
existing.description = description
|
||||||
|
g.db.add(existing)
|
||||||
|
|
||||||
error = "User already has that badge!"
|
msg = "Badge attributes edited successfully!"
|
||||||
if v.client: return {"error": error}
|
if v.client: return {"message": msg}
|
||||||
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, error=error)
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, msg=msg)
|
||||||
|
|
||||||
new_badge = Badge(
|
error = "User already has that badge!"
|
||||||
badge_id=badge_id,
|
if v.client: return {"error": error}
|
||||||
user_id=user.id,
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, error=error)
|
||||||
url=url,
|
|
||||||
description=description
|
new_badge = Badge(
|
||||||
|
badge_id=badge_id,
|
||||||
|
user_id=user.id,
|
||||||
|
url=url,
|
||||||
|
description=description
|
||||||
|
)
|
||||||
|
|
||||||
|
g.db.add(new_badge)
|
||||||
|
g.db.flush()
|
||||||
|
|
||||||
|
if v.id != user.id:
|
||||||
|
text = f"@{v.username} (a site admin) has given you the following profile badge:\n\n{new_badge.path}\n\n**{new_badge.name}**\n\n{new_badge.badge.description}"
|
||||||
|
send_repeatable_notification(user.id, text)
|
||||||
|
|
||||||
|
ma = ModAction(
|
||||||
|
kind="badge_grant",
|
||||||
|
user_id=v.id,
|
||||||
|
target_user_id=user.id,
|
||||||
|
_note=new_badge.name
|
||||||
)
|
)
|
||||||
|
g.db.add(ma)
|
||||||
|
|
||||||
g.db.add(new_badge)
|
|
||||||
g.db.flush()
|
|
||||||
|
|
||||||
if v.id != user.id:
|
|
||||||
text = f"@{v.username} (a site admin) has given you the following profile badge:\n\n{new_badge.path}\n\n**{new_badge.name}**\n\n{new_badge.badge.description}"
|
|
||||||
send_repeatable_notification(user.id, text)
|
|
||||||
|
|
||||||
ma = ModAction(
|
|
||||||
kind="badge_grant",
|
|
||||||
user_id=v.id,
|
|
||||||
target_user_id=user.id,
|
|
||||||
_note=new_badge.name
|
|
||||||
)
|
|
||||||
g.db.add(ma)
|
|
||||||
|
|
||||||
msg = f"{new_badge.name} Badge granted to @{user.username} successfully!"
|
|
||||||
|
|
||||||
|
msg = f"{new_badge.name} Badge granted to users successfully!"
|
||||||
if v.client: return {"message": msg}
|
if v.client: return {"message": msg}
|
||||||
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, msg=msg)
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=True, msg=msg)
|
||||||
|
|
||||||
|
@ -543,33 +550,44 @@ def badge_grant_post(v):
|
||||||
def badge_remove_post(v):
|
def badge_remove_post(v):
|
||||||
badges = admin_badges_grantable_list(v)
|
badges = admin_badges_grantable_list(v)
|
||||||
|
|
||||||
user = get_user(request.values.get("username"), graceful=True)
|
usernames = request.values.get("usernames", "").strip()
|
||||||
if not user:
|
if not usernames:
|
||||||
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=False, error="User not found!")
|
error = "You must enter usernames!"
|
||||||
|
if v.client: return {"error": error}
|
||||||
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=False, error=error)
|
||||||
|
|
||||||
try: badge_id = int(request.values.get("badge_id"))
|
for username in usernames.split():
|
||||||
except: abort(400)
|
user = get_user(username, graceful=True)
|
||||||
|
if not user:
|
||||||
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=False, error="User not found!")
|
||||||
|
|
||||||
if badge_id not in [b.id for b in badges]:
|
try: badge_id = int(request.values.get("badge_id"))
|
||||||
abort(403)
|
except: abort(400)
|
||||||
|
|
||||||
badge = user.has_badge(badge_id)
|
if badge_id not in [b.id for b in badges]:
|
||||||
if not badge:
|
abort(403)
|
||||||
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=False, error="User doesn't have that badge!")
|
|
||||||
|
|
||||||
if v.id != user.id:
|
badge = user.has_badge(badge_id)
|
||||||
text = f"@{v.username} (a site admin) has removed the following profile badge from you:\n\n{badge.path}\n\n**{badge.name}**\n\n{badge.badge.description}"
|
if not badge:
|
||||||
send_repeatable_notification(user.id, text)
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=False, error="User doesn't have that badge!")
|
||||||
|
|
||||||
ma = ModAction(
|
if v.id != user.id:
|
||||||
kind="badge_remove",
|
text = f"@{v.username} (a site admin) has removed the following profile badge from you:\n\n{badge.path}\n\n**{badge.name}**\n\n{badge.badge.description}"
|
||||||
user_id=v.id,
|
send_repeatable_notification(user.id, text)
|
||||||
target_user_id=user.id,
|
|
||||||
_note=badge.name
|
ma = ModAction(
|
||||||
)
|
kind="badge_remove",
|
||||||
g.db.add(ma)
|
user_id=v.id,
|
||||||
g.db.delete(badge)
|
target_user_id=user.id,
|
||||||
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=False, msg=f"{badge.name} Badge removed from @{user.username} successfully!")
|
_note=badge.name
|
||||||
|
)
|
||||||
|
g.db.add(ma)
|
||||||
|
g.db.delete(badge)
|
||||||
|
|
||||||
|
|
||||||
|
msg = f"{badge.name} Badge removed from users successfully!"
|
||||||
|
if v.client: return {"message": msg}
|
||||||
|
return render_template("admin/badge_admin.html", v=v, badge_types=badges, grant=False, msg=msg)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/admin/alt_votes")
|
@app.get("/admin/alt_votes")
|
||||||
|
|
|
@ -41,15 +41,15 @@
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
<label for="input-username">Username</label>
|
<label for="input-usernames">Usernames</label>
|
||||||
<input autocomplete="off" id="input-username" class="form-control" type="text" name="username" required>
|
<input autocomplete="off" id="input-usernames" class="form-control" type="text" name="usernames" placeholder="Required (separated by space)" required>
|
||||||
|
|
||||||
{% if grant %}
|
{% if grant %}
|
||||||
<label class="mt-2" for="input-url">URL</label>
|
<label class="mt-2" for="input-url">URL</label>
|
||||||
<input autocomplete="off" id="input-url" class="form-control" type="text" name="url" type="url" placeholder="Optional">
|
<input autocomplete="off" id="input-url" class="form-control" type="text" name="url" type="url" placeholder="Optional">
|
||||||
|
|
||||||
<label class="mt-2" for="input-description">Custom description</label>
|
<label class="mt-2" for="input-description">Custom description</label>
|
||||||
<input autocomplete="off" id="input-description" class="form-control" type="text" name="description" placeholder="Leave blank for badge default">
|
<input autocomplete="off" id="input-description" class="form-control" type="text" name="description" placeholder="Optional">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<input autocomplete="off" class="btn btn-primary mt-3" type="submit">
|
<input autocomplete="off" class="btn btn-primary mt-3" type="submit">
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue