Add API channel deletion endpoint
This commit is contained in:
parent
b6d229bbd2
commit
81708ef490
|
@ -48,6 +48,7 @@ from core.views.manage.threshold.irc import (
|
||||||
ThresholdIRCOverviewAlerts,
|
ThresholdIRCOverviewAlerts,
|
||||||
ThresholdIRCSendMessage,
|
ThresholdIRCSendMessage,
|
||||||
ThresholdIRCStats,
|
ThresholdIRCStats,
|
||||||
|
ThresholdIRCNetworkChannelsAPI,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Management stuff
|
# Management stuff
|
||||||
|
@ -199,6 +200,11 @@ urlpatterns = [
|
||||||
ThresholdIRCNetworkChannels.as_view(),
|
ThresholdIRCNetworkChannels.as_view(),
|
||||||
name="threshold_irc_network_channels",
|
name="threshold_irc_network_channels",
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
"manage/threshold/irc/network/<str:net>/channel/json/",
|
||||||
|
ThresholdIRCNetworkChannelsAPI.as_view(),
|
||||||
|
name="threshold_irc_network_channel_json",
|
||||||
|
),
|
||||||
path(
|
path(
|
||||||
"manage/threshold/irc/network/<str:net>/channel/<channel>/",
|
"manage/threshold/irc/network/<str:net>/channel/<channel>/",
|
||||||
ThresholdIRCNetworkChannels.as_view(),
|
ThresholdIRCNetworkChannels.as_view(),
|
||||||
|
|
|
@ -66,9 +66,8 @@ def get_irc_channels(net):
|
||||||
|
|
||||||
|
|
||||||
def part_channel(net, channel):
|
def part_channel(net, channel):
|
||||||
channel = urllib.parse.quote(channel, safe="")
|
url = f"irc/network/{net}/channel"
|
||||||
url = f"irc/network/{net}/channel/{channel}"
|
payload = {"channel": channel}
|
||||||
payload = {}
|
|
||||||
parted = threshold_request(url, payload, method="DELETE")
|
parted = threshold_request(url, payload, method="DELETE")
|
||||||
if not parted:
|
if not parted:
|
||||||
return {}
|
return {}
|
||||||
|
|
|
@ -23,8 +23,9 @@
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<button
|
<button
|
||||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
hx-headers='{"X-CSRFToken": "{{ csrf_token }}", "Content-Type": "application/json"}'
|
||||||
hx-delete="{% url 'threshold_irc_network_channel' net channel.name %}"
|
hx-delete="{% url 'threshold_irc_network_channel_json' net %}"
|
||||||
|
hx-vals='{"channel": "{{ channel.name }}"}'
|
||||||
hx-target="#channels"
|
hx-target="#channels"
|
||||||
hx-swap="outerHTML"
|
hx-swap="outerHTML"
|
||||||
class="button is-danger is-small">
|
class="button is-danger is-small">
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.views import View
|
from django.views import View
|
||||||
from rest_framework.parsers import FormParser
|
from rest_framework.parsers import FormParser, JSONParser
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
from core.lib.manage import threshold
|
from core.lib.manage import threshold
|
||||||
|
@ -208,6 +208,61 @@ class ThresholdIRCNetworkRelayStatus(SuperUserRequiredMixin, APIView):
|
||||||
}
|
}
|
||||||
return render(request, self.template_name, context)
|
return render(request, self.template_name, context)
|
||||||
|
|
||||||
|
class ThresholdIRCNetworkChannelsAPI(SuperUserRequiredMixin, APIView):
|
||||||
|
template_name = "manage/threshold/irc/network/channels.html"
|
||||||
|
parser_classes = [FormParser]
|
||||||
|
|
||||||
|
def delete(self, request, net):
|
||||||
|
"""
|
||||||
|
Part a channel.
|
||||||
|
:param net: network name
|
||||||
|
:param channel: channel name
|
||||||
|
"""
|
||||||
|
channel = request.data["channel"]
|
||||||
|
print("DELETE CHANNEL", channel)
|
||||||
|
parted = threshold.part_channel(net, channel)
|
||||||
|
if parted["success"]:
|
||||||
|
message = f"Requested part on relays: {', '.join(parted['relays'])}"
|
||||||
|
message_class = "success"
|
||||||
|
else:
|
||||||
|
message = parted["reason"]
|
||||||
|
message_class = "danger"
|
||||||
|
|
||||||
|
channels = threshold.get_irc_channels(net)
|
||||||
|
context = {
|
||||||
|
"net": net,
|
||||||
|
"channels": channels["channels"],
|
||||||
|
"message": message,
|
||||||
|
"class": message_class,
|
||||||
|
}
|
||||||
|
return render(request, self.template_name, context)
|
||||||
|
|
||||||
|
def put(self, request, net):
|
||||||
|
"""
|
||||||
|
Join a channel.
|
||||||
|
:param net: network name
|
||||||
|
"""
|
||||||
|
if "channel" not in request.data:
|
||||||
|
message = "No channel specified"
|
||||||
|
message_class = "danger"
|
||||||
|
else:
|
||||||
|
channel = request.data["channel"]
|
||||||
|
joined = threshold.join_channel(net, channel)
|
||||||
|
if joined["success"]:
|
||||||
|
message = f"Requested join on relay: {joined['relays']}"
|
||||||
|
message_class = "success"
|
||||||
|
else:
|
||||||
|
message = joined["reason"]
|
||||||
|
message_class = "danger"
|
||||||
|
|
||||||
|
channels = threshold.get_irc_channels(net)
|
||||||
|
context = {
|
||||||
|
"net": net,
|
||||||
|
"channels": channels["channels"],
|
||||||
|
"message": message,
|
||||||
|
"class": message_class,
|
||||||
|
}
|
||||||
|
return render(request, self.template_name, context)
|
||||||
|
|
||||||
class ThresholdIRCNetworkChannels(SuperUserRequiredMixin, APIView):
|
class ThresholdIRCNetworkChannels(SuperUserRequiredMixin, APIView):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue