refactor banned domains a little bit

master
Aevann1 2022-10-21 00:14:25 +02:00
parent 25c5c8fb7d
commit c78579ec5a
2 changed files with 61 additions and 36 deletions

View File

@ -1417,28 +1417,19 @@ def admin_banned_domains(v):
banned_domains = g.db.query(BannedDomain).all() banned_domains = g.db.query(BannedDomain).all()
return render_template("admin/banned_domains.html", v=v, banned_domains=banned_domains) return render_template("admin/banned_domains.html", v=v, banned_domains=banned_domains)
@app.post("/admin/banned_domains") @app.post("/admin/ban_domain")
@limiter.limit("1/second;30/minute;200/hour;1000/day") @limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(PERMS['DOMAINS_BAN']) @admin_level_required(PERMS['DOMAINS_BAN'])
def admin_toggle_ban_domain(v): def ban_domain(v):
domain=request.values.get("domain", "").strip() domain=request.values.get("domain", "").strip()
if not domain: abort(400) if not domain: abort(400)
reason=request.values.get("reason").strip() reason=request.values.get("reason").strip()
d = g.db.query(BannedDomain).filter_by(domain=domain).one_or_none()
if d:
g.db.delete(d)
ma = ModAction(
kind="unban_domain",
user_id=v.id,
_note=domain
)
g.db.add(ma)
else:
if not reason: abort(400, 'Reason is required!') if not reason: abort(400, 'Reason is required!')
existing = g.db.get(BannedDomain, domain)
if not existing:
d = BannedDomain(domain=domain, reason=reason) d = BannedDomain(domain=domain, reason=reason)
g.db.add(d) g.db.add(d)
ma = ModAction( ma = ModAction(
@ -1448,9 +1439,28 @@ def admin_toggle_ban_domain(v):
) )
g.db.add(ma) g.db.add(ma)
return redirect("/admin/banned_domains/") return redirect("/admin/banned_domains/")
@app.post("/admin/unban_domain/<domain>")
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(PERMS['DOMAINS_BAN'])
def unban_domain(v, domain):
existing = g.db.get(BannedDomain, domain)
if not existing: abort(400, 'Domain is not banned!')
g.db.delete(existing)
ma = ModAction(
kind="unban_domain",
user_id=v.id,
_note=domain
)
g.db.add(ma)
return {"message": f"{domain} has been unbanned!"}
@app.post("/admin/nuke_user") @app.post("/admin/nuke_user")
@limiter.limit("1/second;30/minute;200/hour;1000/day") @limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(PERMS['POST_COMMENT_MODERATION']) @admin_level_required(PERMS['POST_COMMENT_MODERATION'])

View File

@ -10,28 +10,43 @@
</pre> </pre>
<div class="overflow-x-auto"><table class="table table-striped mb-5"> <script>
function unbanDomain(t, domain) {
post_toast(t,'/admin/unban_domain/' + domain);
t.parentElement.parentElement.remove();
}
</script>
<div class="overflow-x-auto">
<table class="table table-striped mb-5" id="domains-table">
<thead class="bg-primary text-white"> <thead class="bg-primary text-white">
<tr> <tr>
<th>Domain</th> <th>Domain</th>
<th>Ban reason</th> <th>Ban reason</th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody>
{% for domain in banned_domains %} {% for domain in banned_domains %}
<tr> <tr>
<td>{{domain.domain}}</td> <td>{{domain.domain}}</td>
<td>{{domain.reason}}</td> <td>{{domain.reason}}</td>
<td>
<button class="btn btn-danger" onclick="unbanDomain(this, '{{domain.domain}}')">Unban</button>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody>
</table> </table>
<form action="/admin/banned_domains" method="post"> <form action="/admin/ban_domain" method="post">
<input type="hidden" name="formkey" value="{{v.formkey}}"> <input type="hidden" name="formkey" value="{{v.formkey}}">
<input autocomplete="off" name="domain" placeholder="Enter domain here.." class="form-control" required> <input autocomplete="off" name="domain" placeholder="Enter domain here.." class="form-control" required>
<input autocomplete="off" name="reason" placeholder="Enter ban reason here.." oninput="document.getElementById('ban-submit').disabled=false" class="form-control"> <input autocomplete="off" name="reason" placeholder="Enter ban reason here.." oninput="document.getElementById('ban-submit').disabled=false" class="form-control mt-2">
<input autocomplete="off" id="ban-submit" type="submit" onclick="disable(this)" class="btn btn-primary" value="Toggle ban" disabled> <input autocomplete="off" id="ban-submit" type="submit" onclick="disable(this)" class="btn btn-primary mt-2" value="Ban domain" disabled>
</form> </form>
</div>
{% endblock %} {% endblock %}