Implement editing aliases

This commit is contained in:
Mark Veidemanis 2022-08-01 19:06:56 +01:00
parent 6b0851ab6f
commit 3486f30f37
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 81 additions and 67 deletions

View File

@ -25,6 +25,7 @@ from core.views import Billing, Cancel, Home, Order, Portal, Signup
from core.views.callbacks import Callback
from core.views.manage.threshold.irc import (
ThresholdIRCAliases,
ThresholdIRCAliasesEdit,
ThresholdIRCNetworkActions,
ThresholdIRCNetworkActionsRelay,
ThresholdIRCNetworkChannels,
@ -35,7 +36,6 @@ from core.views.manage.threshold.irc import (
ThresholdIRCNetworkRelayStatus,
ThresholdIRCNetworks,
ThresholdIRCStats,
ThresholdIRCAliasesEdit,
)
# Management stuff

View File

@ -104,3 +104,10 @@ def del_relay(net, num):
payload = {}
deleted = threshold_request(url, payload, method="DELETE")
return deleted
def update_aliases(aliases):
url = "aliases"
payload = aliases
deleted = threshold_request(url, payload, method="POST")
return deleted

View File

@ -51,13 +51,14 @@ def sort_data(data):
data[item] = OrderedDict({k: v for k, v in sorted_item})
def threshold_request(url, data, method="POST"):
def threshold_request(url, data, method="POST", esc=False):
headers = {
"ApiKey": settings.THRESHOLD_API_KEY,
"Token": settings.THRESHOLD_API_TOKEN,
}
for key in data:
data[key] = escape(data[key])
if esc:
for key in data:
data[key] = escape(data[key])
if method == "POST":
method = requests.post
@ -96,7 +97,7 @@ def threshold_request(url, data, method="POST"):
def get_chans(net, query):
url = "chans"
payload = {"net": net, "query": query}
channels = threshold_request(url, payload)
channels = threshold_request(url, payload, esc=True)
if not channels:
return []
return channels["chans"]
@ -105,7 +106,7 @@ def get_chans(net, query):
def get_users(net, query):
url = "users"
payload = {"net": net, "query": query}
users = threshold_request(url, payload)
users = threshold_request(url, payload, esc=True)
if not users:
return []
return users["users"]
@ -114,7 +115,7 @@ def get_users(net, query):
def annotate_online(net, query):
url = "online"
payload = {"net": net, "query": query}
online_info = threshold_request(url, payload)
online_info = threshold_request(url, payload, esc=True)
if not online_info:
return {}
return online_info
@ -123,7 +124,7 @@ def annotate_online(net, query):
def annotate_num_users(net, query):
url = "num_users"
payload = {"net": net, "query": query}
user_num_map = threshold_request(url, payload)
user_num_map = threshold_request(url, payload, esc=True)
if not user_num_map:
return {}
return user_num_map
@ -132,7 +133,7 @@ def annotate_num_users(net, query):
def annotate_num_chans(net, query):
url = "num_chans"
payload = {"net": net, "query": query}
chan_num_map = threshold_request(url, payload)
chan_num_map = threshold_request(url, payload, esc=True)
if not chan_num_map:
return {}
return chan_num_map

View File

@ -1,6 +1,6 @@
{% load index %}
{% load static %}
{% load joinsep %}
{% load nsep %}
<script src="{% static 'modal.js' %}"></script>
<div class="modal is-active is-clipped">
@ -9,55 +9,55 @@
<div class="box">
<h4 class="subtitle is-4">Edit aliases</h4>
<form
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-post="{% url 'threshold_irc_aliases_edit' %}"
hx-target="#aliases"
hx-swap="outerHTML">
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>num</th>
<th>nick</th>
<th>realname</th>
<th>emails</th>
</thead>
<tbody>
{% for alias in aliases %}
<tr>
<td>
{{ alias.num }}
</td>
<td>
<div class="field">
<input class="input" type="text" name="{{ alias.num }}|nick" value="{{ alias.nick }}">
</div>
</td>
<td>
<div class="field">
<input class="input" type="text" name="{{ alias.num }}|realname" value="{{ alias.realname }}">
</div>
</td>
<td>
<div class="field">
<textarea class="textarea" name="{{ alias.num }}|emails">{{ alias.emails|joinsep:'\n' }}</textarea>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-post="{% url 'threshold_irc_aliases_edit' %}"
hx-target="#aliases"
hx-swap="outerHTML">
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>num</th>
<th>nick</th>
<th>realname</th>
<th>emails</th>
</thead>
<tbody>
{% for alias in aliases %}
<tr>
<td>
{{ alias.num }}
</td>
<td>
<div class="field">
<input class="input" type="text" name="{{ alias.num }}|nick" value="{{ alias.nick }}">
</div>
</td>
<td>
<div class="field">
<input class="input" type="text" name="{{ alias.num }}|realname" value="{{ alias.realname }}">
</div>
</td>
<td>
<div class="field">
<textarea class="textarea" name="{{ alias.num }}|emails">{{ alias.emails|nsep }}</textarea>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<button
class="button is-primary modal-close-button">
Cancel
</button>
<button class="button is-primary modal-close-button">Submit</button>
<button
class="button is-primary modal-close-button">
Cancel
</button>
<button class="button is-primary modal-close-button">Submit</button>
</form>
</form>
</div>
<button class="modal-close is-large" aria-label="close"></button>

View File

@ -0,0 +1,7 @@
from django import template
register = template.Library()
@register.filter
def nsep(lst):
return "\n".join(lst)

View File

@ -214,6 +214,7 @@ class ThresholdIRCAliases(SuperUserRequiredMixin, APIView):
}
return render(request, self.template_name, context)
class ThresholdIRCAliasesEdit(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/overview/modals/edit-aliases.html"
parser_classes = [FormParser]
@ -236,7 +237,7 @@ class ThresholdIRCAliasesEdit(SuperUserRequiredMixin, APIView):
message_class = "success"
new_aliases = {}
for item in request.data:
if not "|" in item:
if "|" not in item:
continue
spl = item.split("|")
if len(spl) != 2:
@ -247,7 +248,6 @@ class ThresholdIRCAliasesEdit(SuperUserRequiredMixin, APIView):
value = request.data[item]
if field == "emails":
value = request.data[item].split("\n")
print("EMAILS", )
for email in value:
# Check field isn't just empty
if len(email) != 0:
@ -256,18 +256,16 @@ class ThresholdIRCAliasesEdit(SuperUserRequiredMixin, APIView):
message = f"Invalid email: {email}"
message_class = "danger"
break
# Need to exit here and not run the update
# Or just let threshold do the validation
if num not in new_aliases:
new_aliases[num] = {field: value}
else:
new_aliases[num][field] = value
print("NEW", new_aliases)
#if not message_class = "danger":
# updated = threshold.update_aliases(new_aliases)
# if not updated:
# message = updated["reason"]
# message_class = "danger"
if not message_class == "danger":
updated = threshold.update_aliases(new_aliases)
if not updated:
message = updated["reason"]
message_class = "danger"
template_name = "manage/threshold/irc/overview/aliases.html"
aliases = threshold.get_aliases()
context = {
@ -277,6 +275,7 @@ class ThresholdIRCAliasesEdit(SuperUserRequiredMixin, APIView):
}
return render(request, template_name, context)
class ThresholdIRCNetworkActions(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/network/actions.html"