neptune/core/views/notifications.py

91 lines
2.9 KiB
Python
Raw Permalink Normal View History

2023-01-14 16:36:22 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
2023-01-15 18:39:57 +00:00
from django.shortcuts import render
from mixins.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
2023-01-15 18:39:57 +00:00
from rest_framework.views import APIView
2023-01-12 07:20:48 +00:00
from core.db.storage import db
from core.forms import NotificationRuleForm, NotificationSettingsForm
from core.lib.rules import NotificationRuleData
from core.models import NotificationRule, NotificationSettings
# Notifications - we create a new notification settings object if there isn't one
# Hence, there is only an update view, not a create view.
class NotificationsUpdate(LoginRequiredMixin, ObjectUpdate):
model = NotificationSettings
form_class = NotificationSettingsForm
page_title = "Update your notification settings"
page_subtitle = (
"At least the topic must be set if you want to receive notifications."
)
submit_url_name = "notifications_update"
submit_url_args = ["type"]
pk_required = False
hide_cancel = True
def get_object(self, **kwargs):
notification_settings, _ = NotificationSettings.objects.get_or_create(
user=self.request.user
)
return notification_settings
2023-01-12 07:20:48 +00:00
class RuleList(LoginRequiredMixin, ObjectList):
list_template = "partials/rule-list.html"
model = NotificationRule
page_title = "List of notification rules"
list_url_name = "rules"
list_url_args = ["type"]
submit_url_name = "rule_create"
2023-01-14 16:36:22 +00:00
class RuleCreate(LoginRequiredMixin, PermissionRequiredMixin, ObjectCreate):
permission_required = "use_rules"
model = NotificationRule
form_class = NotificationRuleForm
submit_url_name = "rule_create"
2023-01-14 16:36:22 +00:00
class RuleUpdate(LoginRequiredMixin, PermissionRequiredMixin, ObjectUpdate):
permission_required = "use_rules"
model = NotificationRule
form_class = NotificationRuleForm
submit_url_name = "rule_update"
2023-01-14 16:36:22 +00:00
class RuleDelete(LoginRequiredMixin, PermissionRequiredMixin, ObjectDelete):
permission_required = "use_rules"
2023-01-12 07:20:48 +00:00
model = NotificationRule
2023-01-15 18:39:57 +00:00
class RuleClear(LoginRequiredMixin, PermissionRequiredMixin, APIView):
permission_required = "use_rules"
def post(self, request, type, pk):
template_name = "mixins/partials/notify.html"
2023-01-15 18:39:57 +00:00
rule = NotificationRule.objects.get(pk=pk, user=request.user)
if isinstance(rule.match, dict):
for index in rule.match:
2023-02-09 20:28:34 +00:00
rule.match[index] = None
2023-01-15 18:39:57 +00:00
rule.save()
rule_data = NotificationRuleData(rule.user, rule, db=db)
rule_data.clear_database_matches()
2023-01-15 18:39:57 +00:00
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