Validate interval and window fields in form
This commit is contained in:
@@ -17,6 +17,12 @@ from core.util import logs
|
||||
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):
|
||||
title = f"Rule {rule.name} matched"
|
||||
|
||||
@@ -99,13 +105,27 @@ def process_rules(data):
|
||||
|
||||
|
||||
class NotificationRuleData(object):
|
||||
def __init__(self, user, data):
|
||||
def __init__(self, user, cleaned_data):
|
||||
self.user = user
|
||||
self.data = data
|
||||
self.cleaned_data = cleaned_data
|
||||
self.data = self.cleaned_data.get("data")
|
||||
self.parsed = None
|
||||
|
||||
self.parse_data()
|
||||
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):
|
||||
"""
|
||||
@@ -142,7 +162,7 @@ class NotificationRuleData(object):
|
||||
try:
|
||||
self.parsed = load(self.data, Loader=Loader)
|
||||
except (ScannerError, ParserError) as e:
|
||||
raise ValueError(f"Invalid YAML: {e}")
|
||||
raise RuleParseError("data", f"Invalid YAML: {e}")
|
||||
|
||||
def __str__(self):
|
||||
return dump(self.parsed, Dumper=Dumper)
|
||||
|
||||
Reference in New Issue
Block a user