From aeb84d7a72737a6ecf1f5a3845d75bc4f62c2e39 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Thu, 14 Mar 2024 21:58:40 -0400 Subject: [PATCH] use api for software bans admin page --- relay/frontend/page/admin-domain_bans.haml | 6 +- relay/frontend/page/admin-software_bans.haml | 36 ++++--- relay/frontend/static/api.js | 16 --- relay/frontend/static/domain_ban.js | 6 +- relay/frontend/static/software_ban.js | 102 +++++++++++++++++++ relay/views/api.py | 2 +- 6 files changed, 126 insertions(+), 42 deletions(-) create mode 100644 relay/frontend/static/software_ban.js diff --git a/relay/frontend/page/admin-domain_bans.haml b/relay/frontend/page/admin-domain_bans.haml index 43636ae..a9fdbe0 100644 --- a/relay/frontend/page/admin-domain_bans.haml +++ b/relay/frontend/page/admin-domain_bans.haml @@ -9,13 +9,13 @@ %summary << Ban Domain #add-item %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 - %textarea(id="new-reason" name="new") << {{""}} + %textarea(id="new-reason") << {{""}} %label(for="new-note") << Admin Note - %textarea(id="new-note" name="note") << {{""}} + %textarea(id="new-note") << {{""}} %input(type="button" value="Ban Domain" onclick="ban();") diff --git a/relay/frontend/page/admin-software_bans.haml b/relay/frontend/page/admin-software_bans.haml index cf1576c..00f4043 100644 --- a/relay/frontend/page/admin-software_bans.haml +++ b/relay/frontend/page/admin-software_bans.haml @@ -7,18 +7,17 @@ -block content %details.section %summary << Ban Software - %form(action="/admin/software_bans" method="POST") - #add-item - %label(for="name") << Name - %input(id="name" name="name" placeholder="Name") + #add-item + %label(for="new-name") << Domain + %input(type="name" id="new-name" placeholder="Domain") - %label(for="reason") << Ban Reason - %textarea(id="reason" name="reason") << {{""}} + %label(for="new-reason") << Ban Reason + %textarea(id="new-reason") << {{""}} - %label(for="note") << Admin Note - %textarea(id="note" name="note") << {{""}} + %label(for="new-note") << Admin Note + %textarea(id="new-note") << {{""}} - %input(type="submit" value="Ban Software") + %input(type="submit" value="Ban Software" onclick="ban()") %fieldset.section %legend << Software Bans @@ -33,23 +32,22 @@ %tbody -for ban in bans - %tr + %tr(id="{{ban.name}}") %td.name %details %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 - %textarea.note(id="note" name="note") << {{ban.note or ""}} + .grid-2col + %label.reason(for="{{ban.name}}-reason") << Reason + %textarea.reason(id="{{ban.name}}-reason") << {{ban.reason or ""}} - %input(type="hidden" name="name" value="{{ban.name}}") - %input(type="submit" value="Update") + %label.note(for="{{ban.name}}-note") << Note + %textarea.note(id="{{ban.name}}-note") << {{ban.note or ""}} + + %input(type="button" value="Update" onclick="update_ban('{{ban.name}}')") %td.date =ban.created.strftime("%Y-%m-%d") %td.remove - %a(href="/admin/software_bans/delete/{{ban.name}}" title="Unban software") << ✖ + %a(href="#" onclick="unban('{{ban.name}}')" title="Unban name") << ✖ diff --git a/relay/frontend/static/api.js b/relay/frontend/static/api.js index a2d3fbb..43dd433 100644 --- a/relay/frontend/static/api.js +++ b/relay/frontend/static/api.js @@ -68,22 +68,6 @@ class Client { 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); - } } diff --git a/relay/frontend/static/domain_ban.js b/relay/frontend/static/domain_ban.js index b7ae6b6..f01f675 100644 --- a/relay/frontend/static/domain_ban.js +++ b/relay/frontend/static/domain_ban.js @@ -35,7 +35,7 @@ async function ban() { } try { - var ban = await client.ban(values.domain, values.reason, values.note); + var ban = await client.request("POST", "v1/domain_ban", values); } catch (err) { alert(err); @@ -59,7 +59,7 @@ async function ban() { elems.reason.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) { try { - await client.unban(domain); + await client.request("DELETE", "v1/domain_ban", {"domain": domain}); } catch (error) { alert(error); diff --git a/relay/frontend/static/software_ban.js b/relay/frontend/static/software_ban.js new file mode 100644 index 0000000..72e775d --- /dev/null +++ b/relay/frontend/static/software_ban.js @@ -0,0 +1,102 @@ +function create_ban_object(name, reason, note) { + var text = '
\n'; + text += `${name}\n`; + text += '
\n'; + text += `\n`; + text += `\n`; + text += `\n`; + text += `\n`; + text += ``; + text += '
'; + + 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 = ``; + + 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(); +} diff --git a/relay/views/api.py b/relay/views/api.py index 9428d47..db7ac7a 100644 --- a/relay/views/api.py +++ b/relay/views/api.py @@ -359,7 +359,7 @@ class SoftwareBan(View): if not any([data.get('note'), data.get('reason')]): 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')