use api for software bans admin page

This commit is contained in:
Izalia Mae 2024-03-14 21:58:40 -04:00
parent 17690268bc
commit aeb84d7a72
6 changed files with 126 additions and 42 deletions

View file

@ -9,13 +9,13 @@
%summary << Ban Domain %summary << Ban Domain
#add-item #add-item
%label(for="new-domain") << Domain %label(for="new-domain") << Domain
%input(type="domain" id="new-domain" name="domain" placeholder="Domain") %input(type="domain" id="new-domain" placeholder="Domain")
%label(for="new-reason") << Ban Reason %label(for="new-reason") << Ban Reason
%textarea(id="new-reason" name="new") << {{""}} %textarea(id="new-reason") << {{""}}
%label(for="new-note") << Admin Note %label(for="new-note") << Admin Note
%textarea(id="new-note" name="note") << {{""}} %textarea(id="new-note") << {{""}}
%input(type="button" value="Ban Domain" onclick="ban();") %input(type="button" value="Ban Domain" onclick="ban();")

View file

@ -7,18 +7,17 @@
-block content -block content
%details.section %details.section
%summary << Ban Software %summary << Ban Software
%form(action="/admin/software_bans" method="POST") #add-item
#add-item %label(for="new-name") << Domain
%label(for="name") << Name %input(type="name" id="new-name" placeholder="Domain")
%input(id="name" name="name" placeholder="Name")
%label(for="reason") << Ban Reason %label(for="new-reason") << Ban Reason
%textarea(id="reason" name="reason") << {{""}} %textarea(id="new-reason") << {{""}}
%label(for="note") << Admin Note %label(for="new-note") << Admin Note
%textarea(id="note" name="note") << {{""}} %textarea(id="new-note") << {{""}}
%input(type="submit" value="Ban Software") %input(type="submit" value="Ban Software" onclick="ban()")
%fieldset.section %fieldset.section
%legend << Software Bans %legend << Software Bans
@ -33,23 +32,22 @@
%tbody %tbody
-for ban in bans -for ban in bans
%tr %tr(id="{{ban.name}}")
%td.name %td.name
%details %details
%summary -> =ban.name %summary -> =ban.name
%form(action="/admin/software_bans" method="POST")
.grid-2col
.reason << Reason
%textarea.reason(id="reason" name="reason") << {{ban.reason or ""}}
.note << Note .grid-2col
%textarea.note(id="note" name="note") << {{ban.note or ""}} %label.reason(for="{{ban.name}}-reason") << Reason
%textarea.reason(id="{{ban.name}}-reason") << {{ban.reason or ""}}
%input(type="hidden" name="name" value="{{ban.name}}") %label.note(for="{{ban.name}}-note") << Note
%input(type="submit" value="Update") %textarea.note(id="{{ban.name}}-note") << {{ban.note or ""}}
%input(type="button" value="Update" onclick="update_ban('{{ban.name}}')")
%td.date %td.date
=ban.created.strftime("%Y-%m-%d") =ban.created.strftime("%Y-%m-%d")
%td.remove %td.remove
%a(href="/admin/software_bans/delete/{{ban.name}}" title="Unban software") << &#10006; %a(href="#" onclick="unban('{{ban.name}}')" title="Unban name") << &#10006;

View file

@ -68,22 +68,6 @@ class Client {
return message; return message;
} }
async ban(domain, reason, note) {
const params = {
"domain": domain,
"reason": reason,
"note": note
}
return await this.request("POST", "v1/domain_ban", params);
}
async unban(domain) {
const params = {"domain": domain}
return await this.request("DELETE", "v1/domain_ban", params);
}
} }

View file

@ -35,7 +35,7 @@ async function ban() {
} }
try { try {
var ban = await client.ban(values.domain, values.reason, values.note); var ban = await client.request("POST", "v1/domain_ban", values);
} catch (err) { } catch (err) {
alert(err); alert(err);
@ -59,7 +59,7 @@ async function ban() {
elems.reason.value = null; elems.reason.value = null;
elems.note.value = null; elems.note.value = null;
row.querySelector("details").open = false; document.querySelector("details.section").open = false;
} }
@ -91,7 +91,7 @@ async function update_ban(domain) {
async function unban(domain) { async function unban(domain) {
try { try {
await client.unban(domain); await client.request("DELETE", "v1/domain_ban", {"domain": domain});
} catch (error) { } catch (error) {
alert(error); alert(error);

View file

@ -0,0 +1,102 @@
function create_ban_object(name, reason, note) {
var text = '<details>\n';
text += `<summary>${name}</summary>\n`;
text += '<div class="grid-2col">\n';
text += `<label for="${name}-reason" class="reason">Reason</label>\n`;
text += `<textarea id="${name}-reason" class="reason">${reason}</textarea>\n`;
text += `<label for="${name}-note" class="note">Note</label>\n`;
text += `<textarea id="${name}-note" class="note">${note}</textarea>\n`;
text += `<input type="button" value="Update" onclick="update_ban(\"${name}\"")">`;
text += '</details>';
return text;
}
async function ban() {
var table = document.querySelector("table");
var row = table.insertRow(-1);
var elems = {
name: document.getElementById("new-name"),
reason: document.getElementById("new-reason"),
note: document.getElementById("new-note")
}
var values = {
name: elems.name.value.trim(),
reason: elems.reason.value,
note: elems.note.value
}
if (values.name === "") {
alert("Domain is required");
return;
}
try {
var ban = await client.request("POST", "v1/software_ban", values);
} catch (err) {
alert(err);
return
}
row.id = ban.name;
var new_name = row.insertCell(0);
var new_date = row.insertCell(1);
var new_remove = row.insertCell(2);
new_name.className = "name";
new_date.className = "date";
new_remove.className = "remove";
new_name.innerHTML = create_ban_object(ban.name, ban.reason, ban.note);
new_date.innerHTML = get_date_string(ban.created);
new_remove.innerHTML = `<a href="#" onclick="unban('${ban.name}')" title="Unban name">&#10006;</a>`;
elems.name.value = null;
elems.reason.value = null;
elems.note.value = null;
document.querySelector("details.section").open = false;
}
async function update_ban(name) {
var row = document.getElementById(name);
var elems = {
"reason": row.querySelector("textarea.reason"),
"note": row.querySelector("textarea.note")
}
var values = {
"name": name,
"reason": elems.reason.value,
"note": elems.note.value
}
try {
await client.request("PATCH", "v1/software_ban", values)
} catch (error) {
alert(error);
return;
}
row.querySelector("details").open = false;
}
async function unban(name) {
try {
await client.request("DELETE", "v1/software_ban", {"name": name});
} catch (error) {
alert(error);
return;
}
document.getElementById(name).remove();
}

View file

@ -359,7 +359,7 @@ class SoftwareBan(View):
if not any([data.get('note'), data.get('reason')]): if not any([data.get('note'), data.get('reason')]):
return Response.new_error(400, 'Must include note and/or reason parameters', 'json') return Response.new_error(400, 'Must include note and/or reason parameters', 'json')
ban = conn.update_software_ban(data['name'], **data) ban = conn.update_software_ban(**data)
return Response.new(ban, ctype = 'json') return Response.new(ban, ctype = 'json')