Implement form for editing network

This commit is contained in:
Mark Veidemanis 2022-07-27 08:29:20 +01:00
parent 729f6beea5
commit b7411fd664
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
7 changed files with 201 additions and 105 deletions

View File

@ -40,6 +40,7 @@ from core.views.dynamic.insights import (
from core.views.dynamic.manage.threshold.irc import (
ThresholdIRCNetworkChannels,
ThresholdIRCNetworkInfo,
ThresholdIRCNetworkInfoEdit,
ThresholdIRCNetworkRelays,
ThresholdIRCNetworks,
ThresholdIRCStats,
@ -103,6 +104,16 @@ urlpatterns = [
ThresholdIRCNetworkInfo.as_view(),
name="threshold_irc_network_info",
),
path(
"manage/threshold/irc/network/<str:net>/info/edit/",
ThresholdIRCNetworkInfoEdit.as_view(),
name="threshold_irc_network_info_edit",
),
path(
"manage/threshold/irc/network/<str:net>/edit/",
ThresholdIRCNetworkInfoEdit.as_view(),
name="threshold_irc_network_edit",
),
path(
"manage/threshold/irc/network/<str:net>/relays/",
ThresholdIRCNetworkRelays.as_view(),

View File

@ -12,6 +12,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@creativebulma/bulma-tooltip@1.2.0/dist/bulma-tooltip.min.css">
<link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.1.1/css/all.css" />
<script src="https://unpkg.com/htmx.org@1.8.0" integrity="sha384-cZuAZ+ZbwkNRnrKi05G/fjBX+azI9DNOkNYysZ0I/X5ZFgsmMiBXgDZof30F5ofc" crossorigin="anonymous"></script>
<script src="https://unpkg.com/htmx.org@1.8.0/dist/ext/remove-me.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {

View File

@ -1,10 +1,10 @@
<div id="channels">
{% if channels is not None %}
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<tbody>
{% for channel, info in channels.items %}
{% if channels is not None %}
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<tbody>
{% for channel, info in channels.items %}
<tr>
<td>
{{ channel }}
@ -13,10 +13,10 @@
</span>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</div>
</div>
{% endif %}
</div>

View File

@ -0,0 +1,34 @@
<form
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-post="{% url 'threshold_irc_network_edit' net %}"
hx-target="this"
hx-swap="outerHTML">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>attribute</th>
<th>value</th>
</thead>
<tbody>
{% for key, item in network.items %}
<tr>
<th>{{ key }}</th>
<td>
<div class="field">
<input type="text" name="{{ key }}" value="{{ item }}">
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button class="button is-primary">Submit</button>
<button
class="button is-primary"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-post="{% url 'threshold_irc_network_info' net %}">
Cancel
</button>
</div>
</form>

View File

@ -1,51 +1,65 @@
<div id="info">
{% if network is not None %}
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>attribute</th>
<th>value</th>
</thead>
<tbody>
{% for key, item in network.items %}
<tr>
<th>{{ key }}</th>
<td>
{% if key == 'security' %}
{% if item == 'none' %}
<span class="icon">
<i class="fa-solid fa-lock-open" aria-hidden="true"></i>
</span>
{% elif item == 'ssl' %}
<span class="icon">
<i class="fa-solid fa-lock" aria-hidden="true"></i>
</span>
{% endif %}
{% elif key == 'relays' %}
{% if message is not None %}
<div class="notification is-{{ class }}" hx-ext="remove-me" remove-me="3s">
{{ message }}
</div>
{% endif %}
{% if network is not None %}
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>attribute</th>
<th>value</th>
</thead>
<tbody>
{% for key, item in network.items %}
<tr>
<th>{{ key }}</th>
<td>
{% if key == 'security' %}
{% if item == 'none' %}
<span class="icon">
<i class="fa-brands fa-unity"></i>
<i class="fa-solid fa-lock-open" aria-hidden="true"></i>
</span>
{% elif key == 'channels' %}
{% elif item == 'ssl' %}
<span class="icon">
<i class="fa-solid fa-hashtag"></i>
</span>
{% elif key == 'records' %}
<span class="icon">
<i class="fa-solid fa-album"></i>
</span>
{% elif key == 'host' %}
<span class="icon">
<i class="fa-solid fa-router"></i>
<i class="fa-solid fa-lock" aria-hidden="true"></i>
</span>
{% endif %}
{% elif key == 'relays' %}
<span class="icon">
<i class="fa-brands fa-unity"></i>
</span>
{% elif key == 'channels' %}
<span class="icon">
<i class="fa-solid fa-hashtag"></i>
</span>
{% elif key == 'records' %}
<span class="icon">
<i class="fa-solid fa-album"></i>
</span>
{% elif key == 'host' %}
<span class="icon">
<i class="fa-solid fa-router"></i>
</span>
{% endif %}
{% if item is not None %}
{{ item }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div hx-target="#info" hx-swap="outerHTML">
<button hx-get="{% url 'threshold_irc_network_info_edit' network.net %}" class="button is-primary is-fullwidth">
<span class="icon" data-tooltip="Edit">
<i class="fa-solid fa-pencil" aria-hidden="true"></i>
</span>
</button>
</div>
</div>
{% endif %}
</div>
</div>
{% endif %}
</div>

View File

@ -1,59 +1,56 @@
<div id="relays">
{% if relays is not None %}
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>net</th>
<th>id</th>
<th>registered</th>
<th>enabled</th>
<th>
<span class="icon">
<i class="fa-solid fa-hashtag"></i>
</span>
</th>
<th>nick</th>
</thead>
<tbody>
{% for relay in relays %}
<tr>
<th>{{ relay.net }}</th>
<td>{{ relay.id }}</td>
<td>
{% if relay.registered %}
<span class="icon">
<i class="fa-solid fa-check" aria-hidden="true"></i>
</span>
{% else %}
{% if relays is not None %}
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>id</th>
<th>registered</th>
<th>enabled</th>
<th>
<span class="icon">
<i class="fa-solid fa-hashtag"></i>
</span>
</th>
<th>nick</th>
</thead>
<tbody>
{% for relay in relays %}
<tr>
<td>{{ relay.id }}</td>
<td>
{% if relay.registered %}
<span class="icon">
<i class="fa-solid fa-check" aria-hidden="true"></i>
</span>
{% else %}
<span class="icon">
<i class="fa-solid fa-xmark" aria-hidden="true"></i>
</span>
{% endif %}
</td>
<td>
{% if relay.enabled %}
<span class="icon">
<i class="fa-solid fa-check" aria-hidden="true"></i>
</span>
{% else %}
{% endif %}
</td>
<td>
{% if relay.enabled %}
<span class="icon">
<i class="fa-solid fa-check" aria-hidden="true"></i>
</span>
{% else %}
<span class="icon">
<i class="fa-solid fa-xmark" aria-hidden="true"></i>
</span>
{% endif %}
</td>
<td>
{{ relay.chans }}
</td>
<td>
{{ relay.nick }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</td>
<td>
{{ relay.chans }}
</td>
<td>
{{ relay.nick }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</div>
</div>
{% endif %}
</div>

View File

@ -1,5 +1,7 @@
from django.shortcuts import render
from django.views import View
from rest_framework.parsers import FormParser
from rest_framework.views import APIView
from core.lib.manage.threshold import (
get_irc_channels,
@ -38,6 +40,34 @@ class ThresholdIRCNetworkInfo(SuperUserRequiredMixin, View):
return render(request, self.template_name, context)
class ThresholdIRCNetworkInfoEdit(SuperUserRequiredMixin, APIView):
template_name = "dynamic/manage/threshold/irc/network/edit-network.html"
parser_classes = [FormParser]
def get(self, request, net):
"""
Return the form to edit a network.
"""
network = get_irc_network(net)
editable = ["auth", "host", "last", "port", "security"]
context = {
"net": net,
"network": {k: v for k, v in network.items() if k in editable},
}
return render(request, self.template_name, context)
def post(self, request, net):
"""
Actually edit the network.
Returns the info pane with a message about the success.
"""
template_name = "dynamic/manage/threshold/irc/network/info.html"
network = get_irc_network(net)
context = {"network": network, "message": "Edited!", "class": "info"}
print("REQUEST DATA", net, request.data)
return render(request, template_name, context)
class ThresholdIRCNetworkRelays(SuperUserRequiredMixin, View):
template_name = "dynamic/manage/threshold/irc/network/relays.html"
@ -54,3 +84,12 @@ class ThresholdIRCNetworkChannels(SuperUserRequiredMixin, View):
channels = get_irc_channels(net)
context = {"channels": channels["channels"]}
return render(request, self.template_name, context)
# CRUD stuff
class ThresholdIRCNetworkEdit(SuperUserRequiredMixin, APIView):
"""
Edit a network's attributes.
"""
parser_classes = [FormParser]