Use JSON for sending messages

This commit is contained in:
Mark Veidemanis 2022-08-14 16:45:48 +01:00
parent 555bcb4c09
commit e7b7695efd
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 17 additions and 11 deletions

View File

@ -253,12 +253,12 @@ urlpatterns = [
name="threshold_irc_network_list", name="threshold_irc_network_list",
), ),
path( path(
"manage/threshold/irc/msg/<str:net>/<int:num>/<str:channel>/", "manage/threshold/irc/msg/<str:net>/<int:num>/",
ThresholdIRCSendMessage.as_view(), ThresholdIRCSendMessage.as_view(),
name="threshold_irc_msg", name="threshold_irc_msg",
), ),
path( path(
"manage/threshold/irc/msg/<str:net>/<int:num>/<str:channel>/<str:nick>/", "manage/threshold/irc/msg/<str:net>/<int:num>/<str:nick>/",
ThresholdIRCSendMessage.as_view(), ThresholdIRCSendMessage.as_view(),
name="threshold_irc_msg", name="threshold_irc_msg",
), ),

View File

@ -1,5 +1,3 @@
import urllib.parse
from django.conf import settings from django.conf import settings
from core.lib.opensearch import client, run_main_query from core.lib.opensearch import client, run_main_query
@ -194,9 +192,8 @@ def get_irc_alerts(user):
def send_irc_message(net, num, channel, msg, nick=None): def send_irc_message(net, num, channel, msg, nick=None):
channel = urllib.parse.quote(channel, safe="") url = f"irc/msg/{net}/{num}"
url = f"irc/msg/{net}/{num}/{channel}" payload = {"msg": msg, "channel": channel}
payload = {"msg": msg}
if nick: if nick:
payload["nick"] = nick payload["nick"] = nick
messaged = threshold_request(url, payload, method="PUT") messaged = threshold_request(url, payload, method="PUT")

View File

@ -80,7 +80,8 @@
id="search" id="search"
class="button is-info is-fullwidth" class="button is-info is-fullwidth"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-put="{% url 'threshold_irc_msg' net num channel nick %}" hx-put="{% url 'threshold_irc_msg' net num %}"
hx-vals='{"channel": "{{ channel }}", "nick": "{{ nick }}"}'
hx-trigger="click" hx-trigger="click"
hx-target="#context-input" hx-target="#context-input"
hx-swap="outerHTML"> hx-swap="outerHTML">

View File

@ -657,7 +657,7 @@ class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
parser_classes = [FormParser] parser_classes = [FormParser]
template_name = "partials/context-input.html" template_name = "partials/context-input.html"
def put(self, request, net, num, channel, nick=None): def put(self, request, net, num, nick=None):
""" """
Send a message Send a message
""" """
@ -669,13 +669,21 @@ class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
self.template_name, self.template_name,
{"message": message, "class": message_class}, {"message": message, "class": message_class},
) )
if "channel" not in request.data:
message = "No channel"
message_class = "danger"
return render(
request,
self.template_name,
{"message": message, "class": message_class},
)
if nick: if nick:
messaged = threshold.send_irc_message( messaged = threshold.send_irc_message(
net, num, channel, request.data["msg"], nick=nick net, num, request.data["channel"], request.data["msg"], nick=nick
) )
else: else:
messaged = threshold.send_irc_message( messaged = threshold.send_irc_message(
net, num, channel, request.data["msg"] net, num, request.data["channel"], request.data["msg"]
) )
if not messaged: if not messaged:
message = "Failed to send message" message = "Failed to send message"