From 0457cfbbedc7c255b276c5d9c3d135b6cf5e9274 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 29 Jul 2022 17:28:41 +0100 Subject: [PATCH] Implement adding relays by number --- app/urls.py | 10 +++- core/lib/manage/threshold.py | 9 +++- .../manage/threshold/irc/network/actions.html | 46 +++++++++++++++++-- .../manage/threshold/irc/network/network.html | 35 +++++++------- .../threshold/irc/overview/overview.html | 14 +++--- core/views/manage/threshold/irc.py | 46 +++++++++++++++++-- 6 files changed, 124 insertions(+), 36 deletions(-) diff --git a/app/urls.py b/app/urls.py index 13c8eed..68cb375 100644 --- a/app/urls.py +++ b/app/urls.py @@ -24,6 +24,9 @@ from core.api.views.threshold import ThresholdChans, ThresholdOnline, ThresholdU from core.views import Billing, Cancel, Home, Order, Portal, Signup from core.views.callbacks import Callback from core.views.manage.threshold.irc import ( + ThresholdIRCAliases, + ThresholdIRCNetworkActions, + ThresholdIRCNetworkActionsAddRelay, ThresholdIRCNetworkChannels, ThresholdIRCNetworkInfo, ThresholdIRCNetworkInfoEdit, @@ -31,8 +34,6 @@ from core.views.manage.threshold.irc import ( ThresholdIRCNetworkRelayStatus, ThresholdIRCNetworks, ThresholdIRCStats, - ThresholdIRCAliases, - ThresholdIRCNetworkActions, ) # Management stuff @@ -153,6 +154,11 @@ urlpatterns = [ ThresholdIRCNetworkActions.as_view(), name="threshold_irc_network_actions", ), + path( + "manage/threshold/irc/network/actions//", + ThresholdIRCNetworkActionsAddRelay.as_view(), + name="threshold_irc_network_actions_add_relay", + ), ## path("api/chans/", ThresholdChans.as_view(), name="chans"), path("api/users/", ThresholdUsers.as_view(), name="users"), diff --git a/core/lib/manage/threshold.py b/core/lib/manage/threshold.py index 61f1ba3..11a67a1 100644 --- a/core/lib/manage/threshold.py +++ b/core/lib/manage/threshold.py @@ -81,9 +81,16 @@ def join_channel(net, channel): return {} return joined + def get_aliases(): url = "aliases" payload = {} aliases = threshold_request(url, payload, method="GET") - print("Aliases", aliases) return aliases + + +def add_relay(net, num): + url = f"irc/network/{net}/{num}" + payload = {} + created = threshold_request(url, payload, method="PUT") + return created diff --git a/core/templates/manage/threshold/irc/network/actions.html b/core/templates/manage/threshold/irc/network/actions.html index 3df22cf..b3b0253 100644 --- a/core/templates/manage/threshold/irc/network/actions.html +++ b/core/templates/manage/threshold/irc/network/actions.html @@ -1,13 +1,21 @@
+ {% if message is not None %} +
+ {{ message }} +
+ {% endif %}
+
+
+
+
+ + + + +
+
+ +
+
+
+
\ No newline at end of file diff --git a/core/templates/manage/threshold/irc/network/network.html b/core/templates/manage/threshold/irc/network/network.html index 30fb4c5..cfa232c 100644 --- a/core/templates/manage/threshold/irc/network/network.html +++ b/core/templates/manage/threshold/irc/network/network.html @@ -29,13 +29,13 @@
-
+ style="display: none;" + hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' + hx-get="{% url 'threshold_irc_network_actions' net %}" + hx-trigger="load" + hx-target="#actions" + hx-swap="outerHTML"> +
@@ -62,27 +62,24 @@
+ Alerts here
+ {# This is the only thing included without HTMX because it isn't interactive #} + {# It needs no dynamic message element because it posts messages to chanels #}
-
-
-
-
- - - - -
-
+
+
+ + + +
-
-
diff --git a/core/views/manage/threshold/irc.py b/core/views/manage/threshold/irc.py index a199f6b..afe75b2 100644 --- a/core/views/manage/threshold/irc.py +++ b/core/views/manage/threshold/irc.py @@ -132,7 +132,7 @@ class ThresholdIRCNetworkChannels(SuperUserRequiredMixin, APIView): :param channel: channel name """ parted = threshold.part_channel(net, channel) - if parted: + if parted["success"]: message = f"Requested part on relays: {', '.join(parted['relays'])}" message_class = "success" else: @@ -158,10 +158,9 @@ class ThresholdIRCNetworkChannels(SuperUserRequiredMixin, APIView): message_class = "danger" else: channel = request.data["channel"] - print("CHANNEL", channel) joined = threshold.join_channel(net, channel) print("JOINED", joined) - if joined: + if joined["success"]: message = f"Requested join on relay: {joined['relays']}" message_class = "success" else: @@ -191,6 +190,7 @@ class ThresholdIRCAliases(SuperUserRequiredMixin, APIView): } return render(request, self.template_name, context) + class ThresholdIRCNetworkActions(SuperUserRequiredMixin, View): template_name = "manage/threshold/irc/network/actions.html" @@ -198,5 +198,43 @@ class ThresholdIRCNetworkActions(SuperUserRequiredMixin, View): """ Get actions page. """ + context = {"net": net} + return render(request, self.template_name, context) - return render(request, self.template_name) \ No newline at end of file + +class ThresholdIRCNetworkActionsAddRelay(SuperUserRequiredMixin, APIView): + template_name = "manage/threshold/irc/network/actions.html" + parser_classes = [FormParser] + + def put(self, request, net): + """ + Create a relay + """ + print("ACTION PUT", request.data, net) + if "num" not in request.data: + message = "No num specified" + message_class = "danger" + else: + num = request.data["num"] + if not num.isdigit(): + message = "Num is not an integer" + message_class = "danger" + else: + num = int(num) + created = threshold.add_relay(net, num) + print("CREATED", created) + if created["success"]: + id = created["id"] + alias = created["alias"] + message = f"Created new relay {id} with alias {alias}" + message_class = "success" + else: + message = created["reason"] + message_class = "danger" + + context = { + "net": net, + "message": message, + "class": message_class, + } + return render(request, self.template_name, context)