From 5a66236828f85c7796456fa7c5dbdd0a39c949dd Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 1 Aug 2022 18:47:12 +0100 Subject: [PATCH] Implement alias editing form --- core/views/manage/threshold/irc.py | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/core/views/manage/threshold/irc.py b/core/views/manage/threshold/irc.py index 3555363..bc960da 100644 --- a/core/views/manage/threshold/irc.py +++ b/core/views/manage/threshold/irc.py @@ -214,6 +214,68 @@ 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] + + def get(self, request): + """ + Get aliases edit form. + """ + aliases = threshold.get_aliases() + context = { + "aliases": aliases["aliases"], + } + return render(request, self.template_name, context) + + def post(self, request): + """ + Set the aliases + """ + message = "Successfully updated aliases" + message_class = "success" + new_aliases = {} + for item in request.data: + if not "|" in item: + continue + spl = item.split("|") + if len(spl) != 2: + message = f"Invalid item: {item}" + message_class = "danger" + break + num, field = spl + 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: + # Simple check for valid email + if "@" not in email: + 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" + template_name = "manage/threshold/irc/overview/aliases.html" + aliases = threshold.get_aliases() + context = { + "aliases": aliases["aliases"], + "message": message, + "class": message_class, + } + return render(request, template_name, context) class ThresholdIRCNetworkActions(SuperUserRequiredMixin, View): template_name = "manage/threshold/irc/network/actions.html"