Validate interval and window fields in form
This commit is contained in:
parent
fbe5607899
commit
dbf581245b
|
@ -4,7 +4,7 @@ from django.core.exceptions import FieldDoesNotExist
|
||||||
from django.forms import ModelForm
|
from django.forms import ModelForm
|
||||||
|
|
||||||
from core.db import QueryError
|
from core.db import QueryError
|
||||||
from core.lib.rules import NotificationRuleData
|
from core.lib.rules import NotificationRuleData, RuleParseError
|
||||||
|
|
||||||
from .models import NotificationRule, NotificationSettings, User
|
from .models import NotificationRule, NotificationSettings, User
|
||||||
|
|
||||||
|
@ -106,11 +106,10 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm):
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
cleaned_data = super(NotificationRuleForm, self).clean()
|
cleaned_data = super(NotificationRuleForm, self).clean()
|
||||||
data = cleaned_data.get("data")
|
|
||||||
try:
|
try:
|
||||||
parsed_data = NotificationRuleData(self.request.user, data)
|
parsed_data = NotificationRuleData(self.request.user, cleaned_data)
|
||||||
except ValueError as e:
|
except RuleParseError as e:
|
||||||
self.add_error("data", f"Parsing error: {e}")
|
self.add_error(e.field, f"Parsing error: {e}")
|
||||||
return
|
return
|
||||||
except QueryError as e:
|
except QueryError as e:
|
||||||
self.add_error("data", f"Query error: {e}")
|
self.add_error("data", f"Query error: {e}")
|
||||||
|
|
|
@ -17,6 +17,12 @@ from core.util import logs
|
||||||
log = logs.get_logger("rules")
|
log = logs.get_logger("rules")
|
||||||
|
|
||||||
|
|
||||||
|
class RuleParseError(Exception):
|
||||||
|
def __init__(self, message, field):
|
||||||
|
super().__init__(message)
|
||||||
|
self.field = field
|
||||||
|
|
||||||
|
|
||||||
def rule_matched(rule, message, matched):
|
def rule_matched(rule, message, matched):
|
||||||
title = f"Rule {rule.name} matched"
|
title = f"Rule {rule.name} matched"
|
||||||
|
|
||||||
|
@ -99,13 +105,27 @@ def process_rules(data):
|
||||||
|
|
||||||
|
|
||||||
class NotificationRuleData(object):
|
class NotificationRuleData(object):
|
||||||
def __init__(self, user, data):
|
def __init__(self, user, cleaned_data):
|
||||||
self.user = user
|
self.user = user
|
||||||
self.data = data
|
self.cleaned_data = cleaned_data
|
||||||
|
self.data = self.cleaned_data.get("data")
|
||||||
self.parsed = None
|
self.parsed = None
|
||||||
|
|
||||||
self.parse_data()
|
self.parse_data()
|
||||||
self.validate_permissions()
|
self.validate_permissions()
|
||||||
|
self.validate_time_fields()
|
||||||
|
|
||||||
|
def validate_time_fields(self):
|
||||||
|
"""
|
||||||
|
Validate the interval and window fields.
|
||||||
|
Prohibit window being specified with an ondemand interval.
|
||||||
|
"""
|
||||||
|
interval = self.cleaned_data.get("interval")
|
||||||
|
window = self.cleaned_data.get("window")
|
||||||
|
if interval == "ondemand" and window is not None:
|
||||||
|
raise RuleParseError(
|
||||||
|
"Window cannot be specified with ondemand interval", "window"
|
||||||
|
)
|
||||||
|
|
||||||
def validate_permissions(self):
|
def validate_permissions(self):
|
||||||
"""
|
"""
|
||||||
|
@ -142,7 +162,7 @@ class NotificationRuleData(object):
|
||||||
try:
|
try:
|
||||||
self.parsed = load(self.data, Loader=Loader)
|
self.parsed = load(self.data, Loader=Loader)
|
||||||
except (ScannerError, ParserError) as e:
|
except (ScannerError, ParserError) as e:
|
||||||
raise ValueError(f"Invalid YAML: {e}")
|
raise RuleParseError("data", f"Invalid YAML: {e}")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return dump(self.parsed, Dumper=Dumper)
|
return dump(self.parsed, Dumper=Dumper)
|
||||||
|
|
Loading…
Reference in New Issue