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 ( from core.views.dynamic.manage.threshold.irc import (
ThresholdIRCNetworkChannels, ThresholdIRCNetworkChannels,
ThresholdIRCNetworkInfo, ThresholdIRCNetworkInfo,
ThresholdIRCNetworkInfoEdit,
ThresholdIRCNetworkRelays, ThresholdIRCNetworkRelays,
ThresholdIRCNetworks, ThresholdIRCNetworks,
ThresholdIRCStats, ThresholdIRCStats,
@ -103,6 +104,16 @@ urlpatterns = [
ThresholdIRCNetworkInfo.as_view(), ThresholdIRCNetworkInfo.as_view(),
name="threshold_irc_network_info", 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( path(
"manage/threshold/irc/network/<str:net>/relays/", "manage/threshold/irc/network/<str:net>/relays/",
ThresholdIRCNetworkRelays.as_view(), 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://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" /> <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" 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> <script>
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {

View File

@ -19,4 +19,4 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
</div> </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,4 +1,9 @@
<div id="info"> <div id="info">
{% 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 %} {% if network is not None %}
<div class="content" style="max-height: 30em; overflow: auto;"> <div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container"> <div class="table-container">
@ -39,13 +44,22 @@
<i class="fa-solid fa-router"></i> <i class="fa-solid fa-router"></i>
</span> </span>
{% endif %} {% endif %}
{% if item is not None %}
{{ item }} {{ item }}
{% endif %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </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> </div>
</div> </div>
{% endif %} {% endif %}
</div> </div>

View File

@ -4,7 +4,6 @@
<div class="table-container"> <div class="table-container">
<table class="table is-fullwidth is-hoverable"> <table class="table is-fullwidth is-hoverable">
<thead> <thead>
<th>net</th>
<th>id</th> <th>id</th>
<th>registered</th> <th>registered</th>
<th>enabled</th> <th>enabled</th>
@ -18,7 +17,6 @@
<tbody> <tbody>
{% for relay in relays %} {% for relay in relays %}
<tr> <tr>
<th>{{ relay.net }}</th>
<td>{{ relay.id }}</td> <td>{{ relay.id }}</td>
<td> <td>
{% if relay.registered %} {% if relay.registered %}
@ -43,7 +41,6 @@
{% endif %} {% endif %}
</td> </td>
<td> <td>
{{ relay.chans }} {{ relay.chans }}
</td> </td>
<td> <td>
@ -56,4 +53,4 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
</div> </div>

View File

@ -1,5 +1,7 @@
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.views import APIView
from core.lib.manage.threshold import ( from core.lib.manage.threshold import (
get_irc_channels, get_irc_channels,
@ -38,6 +40,34 @@ class ThresholdIRCNetworkInfo(SuperUserRequiredMixin, View):
return render(request, self.template_name, context) 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): class ThresholdIRCNetworkRelays(SuperUserRequiredMixin, View):
template_name = "dynamic/manage/threshold/irc/network/relays.html" template_name = "dynamic/manage/threshold/irc/network/relays.html"
@ -54,3 +84,12 @@ class ThresholdIRCNetworkChannels(SuperUserRequiredMixin, View):
channels = get_irc_channels(net) channels = get_irc_channels(net)
context = {"channels": channels["channels"]} context = {"channels": channels["channels"]}
return render(request, self.template_name, context) return render(request, self.template_name, context)
# CRUD stuff
class ThresholdIRCNetworkEdit(SuperUserRequiredMixin, APIView):
"""
Edit a network's attributes.
"""
parser_classes = [FormParser]