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

@ -1,10 +1,10 @@
<div id="channels"> <div id="channels">
{% if channels is not None %} {% if channels 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">
<table class="table is-fullwidth is-hoverable"> <table class="table is-fullwidth is-hoverable">
<tbody> <tbody>
{% for channel, info in channels.items %} {% for channel, info in channels.items %}
<tr> <tr>
<td> <td>
{{ channel }} {{ channel }}
@ -13,10 +13,10 @@
</span> </span>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
</div>
</div> </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"> <div id="info">
{% if network is not None %} {% if message is not None %}
<div class="content" style="max-height: 30em; overflow: auto;"> <div class="notification is-{{ class }}" hx-ext="remove-me" remove-me="3s">
<div class="table-container"> {{ message }}
<table class="table is-fullwidth is-hoverable"> </div>
<thead> {% endif %}
<th>attribute</th> {% if network is not None %}
<th>value</th> <div class="content" style="max-height: 30em; overflow: auto;">
</thead> <div class="table-container">
<tbody> <table class="table is-fullwidth is-hoverable">
{% for key, item in network.items %} <thead>
<tr> <th>attribute</th>
<th>{{ key }}</th> <th>value</th>
<td> </thead>
{% if key == 'security' %} <tbody>
{% if item == 'none' %} {% for key, item in network.items %}
<span class="icon"> <tr>
<i class="fa-solid fa-lock-open" aria-hidden="true"></i> <th>{{ key }}</th>
</span> <td>
{% elif item == 'ssl' %} {% if key == 'security' %}
<span class="icon"> {% if item == 'none' %}
<i class="fa-solid fa-lock" aria-hidden="true"></i>
</span>
{% endif %}
{% elif key == 'relays' %}
<span class="icon"> <span class="icon">
<i class="fa-brands fa-unity"></i> <i class="fa-solid fa-lock-open" aria-hidden="true"></i>
</span> </span>
{% elif key == 'channels' %} {% elif item == 'ssl' %}
<span class="icon"> <span class="icon">
<i class="fa-solid fa-hashtag"></i> <i class="fa-solid fa-lock" aria-hidden="true"></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> </span>
{% endif %} {% 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 }} {{ item }}
</td> {% endif %}
</tr> </td>
{% endfor %} </tr>
</tbody> {% endfor %}
</table> </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>
</div> </div>
{% endif %} </div>
</div> {% endif %}
</div>

View File

@ -1,59 +1,56 @@
<div id="relays"> <div id="relays">
{% if relays is not None %} {% if relays 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">
<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> <th>
<th> <span class="icon">
<span class="icon"> <i class="fa-solid fa-hashtag"></i>
<i class="fa-solid fa-hashtag"></i> </span>
</span> </th>
</th> <th>nick</th>
<th>nick</th> </thead>
</thead> <tbody>
<tbody> {% for relay in relays %}
{% for relay in relays %} <tr>
<tr> <td>{{ relay.id }}</td>
<th>{{ relay.net }}</th> <td>
<td>{{ relay.id }}</td> {% if relay.registered %}
<td> <span class="icon">
{% if relay.registered %} <i class="fa-solid fa-check" aria-hidden="true"></i>
<span class="icon"> </span>
<i class="fa-solid fa-check" aria-hidden="true"></i> {% else %}
</span>
{% else %}
<span class="icon"> <span class="icon">
<i class="fa-solid fa-xmark" aria-hidden="true"></i> <i class="fa-solid fa-xmark" aria-hidden="true"></i>
</span> </span>
{% endif %} {% endif %}
</td> </td>
<td> <td>
{% if relay.enabled %} {% if relay.enabled %}
<span class="icon"> <span class="icon">
<i class="fa-solid fa-check" aria-hidden="true"></i> <i class="fa-solid fa-check" aria-hidden="true"></i>
</span> </span>
{% else %} {% else %}
<span class="icon"> <span class="icon">
<i class="fa-solid fa-xmark" aria-hidden="true"></i> <i class="fa-solid fa-xmark" aria-hidden="true"></i>
</span> </span>
{% endif %} {% endif %}
</td> </td>
<td> <td>
{{ relay.chans }}
{{ relay.chans }} </td>
</td> <td>
<td> {{ relay.nick }}
{{ relay.nick }} </td>
</td> </tr>
</tr> {% endfor %}
{% endfor %} </tbody>
</tbody> </table>
</table>
</div>
</div> </div>
{% endif %} </div>
</div> {% endif %}
</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]