Implement notification rules and settings
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
from django import forms
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from django.core.exceptions import FieldDoesNotExist
|
||||
from django.forms import ModelForm
|
||||
|
||||
from .models import User
|
||||
from core.db import QueryError
|
||||
from core.lib.rules import NotificationRuleData
|
||||
|
||||
from .models import NotificationRule, NotificationSettings, User
|
||||
|
||||
# from django.forms import ModelForm
|
||||
|
||||
@@ -64,3 +68,51 @@ class CustomUserCreationForm(UserCreationForm):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class NotificationSettingsForm(RestrictedFormMixin, ModelForm):
|
||||
class Meta:
|
||||
model = NotificationSettings
|
||||
fields = (
|
||||
"ntfy_topic",
|
||||
"ntfy_url",
|
||||
)
|
||||
help_texts = {
|
||||
"ntfy_topic": "The topic to send notifications to.",
|
||||
"ntfy_url": "Custom NTFY server. Leave blank to use the default server.",
|
||||
}
|
||||
|
||||
|
||||
class NotificationRuleForm(RestrictedFormMixin, ModelForm):
|
||||
class Meta:
|
||||
model = NotificationRule
|
||||
fields = (
|
||||
"name",
|
||||
"enabled",
|
||||
"data",
|
||||
)
|
||||
help_texts = {
|
||||
"name": "The name of the rule.",
|
||||
"enabled": "Whether the rule is enabled.",
|
||||
"data": "The notification rule definition.",
|
||||
}
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(NotificationRuleForm, self).clean()
|
||||
data = cleaned_data.get("data")
|
||||
try:
|
||||
parsed_data = NotificationRuleData(self.request.user, data)
|
||||
except ValueError as e:
|
||||
self.add_error("data", f"Parsing error: {e}")
|
||||
return
|
||||
except QueryError as e:
|
||||
self.add_error("data", f"Query error: {e}")
|
||||
return
|
||||
|
||||
# Write back the validated data
|
||||
# We need this to populate the index and source variable if
|
||||
# they are not set
|
||||
to_store = str(parsed_data)
|
||||
cleaned_data["data"] = to_store
|
||||
|
||||
return cleaned_data
|
||||
|
||||
Reference in New Issue
Block a user