From 46c7d9631016f1a8d27fe3eec4530d5a1c33ed6d Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 15 Jan 2023 18:39:57 +0000 Subject: [PATCH] Allow clearing matches --- app/urls.py | 5 +++++ core/views/notifications.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/urls.py b/app/urls.py index 1e8b2fe..2a9b15d 100644 --- a/app/urls.py +++ b/app/urls.py @@ -306,4 +306,9 @@ urlpatterns = [ notifications.RuleDelete.as_view(), name="rule_delete", ), + path( + "rule//clear//", + notifications.RuleClear.as_view(), + name="rule_clear", + ), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/core/views/notifications.py b/core/views/notifications.py index ba923f3..4e36782 100644 --- a/core/views/notifications.py +++ b/core/views/notifications.py @@ -1,4 +1,6 @@ from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin +from django.shortcuts import render +from rest_framework.views import APIView from core.forms import NotificationRuleForm, NotificationSettingsForm from core.models import NotificationRule, NotificationSettings @@ -61,3 +63,24 @@ class RuleUpdate(LoginRequiredMixin, PermissionRequiredMixin, ObjectUpdate): class RuleDelete(LoginRequiredMixin, PermissionRequiredMixin, ObjectDelete): permission_required = "use_rules" model = NotificationRule + + +class RuleClear(LoginRequiredMixin, PermissionRequiredMixin, APIView): + permission_required = "use_rules" + + def post(self, request, type, pk): + template_name = "partials/notify.html" + rule = NotificationRule.objects.get(pk=pk, user=request.user) + if isinstance(rule.match, dict): + for index in rule.match: + rule.match[index] = False + rule.save() + + cleared_indices = ", ".join(rule.match) + context = { + "message": f"Cleared match status for indices: {cleared_indices}", + "class": "success", + } + response = render(request, template_name, context) + response["HX-Trigger"] = "notificationruleEvent" + return response