Allow scheduling notification rules

This commit is contained in:
2023-01-14 16:36:22 +00:00
parent 9ee9c7abde
commit 2a1e6b3292
7 changed files with 137 additions and 8 deletions

View File

@@ -16,6 +16,10 @@ from core.util import logs
log = logs.get_logger("rules")
SECONDS_PER_UNIT = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}
MAX_WINDOW = 2592000
class RuleParseError(Exception):
def __init__(self, message, field):
@@ -111,10 +115,19 @@ class NotificationRuleData(object):
self.data = self.cleaned_data.get("data")
self.parsed = None
self.validate_user_permissions()
self.parse_data()
self.validate_permissions()
self.validate_time_fields()
def validate_user_permissions(self):
"""
Ensure the user can use notification rules.
"""
if not self.user.has_perm("core.use_rules"):
raise RuleParseError("User does not have permission to use rules", "data")
def validate_time_fields(self):
"""
Validate the interval and window fields.
@@ -122,11 +135,35 @@ class NotificationRuleData(object):
"""
interval = self.cleaned_data.get("interval")
window = self.cleaned_data.get("window")
if interval == "ondemand" and window is not None:
if interval == 0 and window is not None:
raise RuleParseError(
"Window cannot be specified with ondemand interval", "window"
"Window cannot be specified with on-demand interval", "window"
)
if interval is not None and window is None:
raise RuleParseError(
"Window must be specified with non-on-demand interval", "window"
)
if window is not None:
window_number = window[:-1]
if not window_number.isdigit():
raise RuleParseError("Window prefix must be a number", "window")
window_number = int(window_number)
window_unit = window[-1]
if window_unit not in SECONDS_PER_UNIT:
raise RuleParseError(
f"Window unit must be one of {', '.join(SECONDS_PER_UNIT.keys())}, not '{window_unit}'",
"window",
)
window_seconds = window_number * SECONDS_PER_UNIT[window_unit]
print("Window seconds", window_seconds)
if window_seconds > MAX_WINDOW:
raise RuleParseError(
f"Window cannot be larger than {MAX_WINDOW} seconds (30 days)",
"window",
)
def validate_permissions(self):
"""
Validate permissions for the source and index variables.