Properly implement queries

This commit is contained in:
Mark Veidemanis 2022-08-03 07:20:30 +01:00
parent 1d2f37f588
commit 6af8e94336
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
7 changed files with 31 additions and 9 deletions

View File

@ -220,6 +220,11 @@ urlpatterns = [
ThresholdIRCSendMessage.as_view(),
name="threshold_irc_msg",
),
path(
"manage/threshold/irc/msg/<str:net>/<int:num>/<str:channel>/<str:nick>/",
ThresholdIRCSendMessage.as_view(),
name="threshold_irc_msg",
),
##
path("api/chans/", ThresholdChans.as_view(), name="chans"),
path("api/users/", ThresholdUsers.as_view(), name="users"),

View File

@ -195,9 +195,11 @@ def get_irc_alerts(user):
return results_parsed
def send_irc_message(net, num, channel, msg):
def send_irc_message(net, num, channel, msg, nick=None):
channel = urllib.parse.quote(channel, safe="")
url = f"irc/msg/{net}/{num}/{channel}"
payload = {"msg": msg}
if nick:
payload["nick"] = nick
messaged = threshold_request(url, payload, method="PUT")
return messaged

View File

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

View File

@ -30,7 +30,8 @@
"date": "{{ date }}",
"index": "{{ index }}",
"type": "{{ type }}",
"mtype": "{{ mtype }}"}'
"mtype": "{{ mtype }}",
"nick": "{{ nick }}"}'
hx-target="#modal-context-table"
hx-trigger="every 5s">
</div>

View File

@ -225,7 +225,8 @@
"date": "{{ row.cells.date|escapejs }}",
"index": "{{ params.index }}",
"type": "{{ row.cells.type }}",
"mtype": "{{ row.cells.mtype }}"}'
"mtype": "{{ row.cells.mtype }}",
"nick": "{{ row.cells.nick|escapejs }}"}'
hx-target="#modals-here"
hx-trigger="click">
{{ row.cells.msg }}

View File

@ -448,7 +448,7 @@ class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
parser_classes = [FormParser]
template_name = "partials/context-input.html"
def put(self, request, net, num, channel):
def put(self, request, net, num, channel, nick=None):
"""
Send a message
"""
@ -460,8 +460,15 @@ class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
self.template_name,
{"message": message, "class": message_class},
)
messaged = threshold.send_irc_message(net, num, channel, request.data["msg"])
print("IRC", nick)
if nick:
messaged = threshold.send_irc_message(
net, num, channel, request.data["msg"], nick=nick
)
else:
messaged = threshold.send_irc_message(
net, num, channel, request.data["msg"]
)
if not messaged:
message = "Failed to send message"
message_class = "danger"
@ -471,6 +478,7 @@ class ThresholdIRCSendMessage(SuperUserRequiredMixin, APIView):
else:
message = messaged["reason"]
message_class = "danger"
print("ERROR", message)
context = {
"net": net,

View File

@ -245,7 +245,7 @@ class DrilldownContextModal(APIView):
size = 20
# Create the query params from the POST arguments
mandatory = ["net", "channel", "num", "src", "index"]
mandatory = ["net", "channel", "num", "src", "index", "nick"]
invalid = [None, False, "", "None"]
query_params = {k: v for k, v in request.data.items() if v}
for key in query_params:
@ -258,7 +258,11 @@ class DrilldownContextModal(APIView):
if "mtype" not in query_params:
query_params["mtype"] = None
query_params["sorting"] = "desc"
if query_params["index"] == "int" and query_params["mtype"] == "msg":
if (
query_params["index"] == "int"
and query_params["mtype"] == "msg"
and not query_params["type"] == "query"
):
query_params["index"] = "main"
# Create the query with the context helper
search_query = construct_query(
@ -292,6 +296,7 @@ class DrilldownContextModal(APIView):
"num": query_params["num"],
"type": query_params["type"],
"mtype": query_params["mtype"],
"nick": query_params["nick"],
}
return render(request, self.template_name, context)