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.callbacks import Callback
from core.views.manage.threshold.irc import ( from core.views.manage.threshold.irc import (
ThresholdIRCAliases, ThresholdIRCAliases,
ThresholdIRCAliasesEdit,
ThresholdIRCNetworkActions, ThresholdIRCNetworkActions,
ThresholdIRCNetworkActionsRelay, ThresholdIRCNetworkActionsRelay,
ThresholdIRCNetworkChannels, ThresholdIRCNetworkChannels,
@ -35,7 +36,6 @@ from core.views.manage.threshold.irc import (
ThresholdIRCNetworkRelayStatus, ThresholdIRCNetworkRelayStatus,
ThresholdIRCNetworks, ThresholdIRCNetworks,
ThresholdIRCStats, ThresholdIRCStats,
ThresholdIRCAliasesEdit,
) )
# Management stuff # Management stuff

View File

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

View File

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

View File

@ -1,6 +1,6 @@
{% load index %} {% load index %}
{% load static %} {% load static %}
{% load joinsep %} {% load nsep %}
<script src="{% static 'modal.js' %}"></script> <script src="{% static 'modal.js' %}"></script>
<div class="modal is-active is-clipped"> <div class="modal is-active is-clipped">
@ -40,7 +40,7 @@
</td> </td>
<td> <td>
<div class="field"> <div class="field">
<textarea class="textarea" name="{{ alias.num }}|emails">{{ alias.emails|joinsep:'\n' }}</textarea> <textarea class="textarea" name="{{ alias.num }}|emails">{{ alias.emails|nsep }}</textarea>
</div> </div>
</td> </td>
</tr> </tr>

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