better generic schedule validator for impossible input, #209

This commit is contained in:
simon 2022-04-10 15:58:11 +07:00
parent 10385b1414
commit 31378ac756
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -220,18 +220,33 @@ class ScheduleBuilder:
raise ValueError("invalid input") raise ValueError("invalid input")
to_write = dict(zip(keys, values)) to_write = dict(zip(keys, values))
all_hours = [int(i) for i in re.split(r"\D+", to_write["hour"])] self._validate_cron(to_write)
if max(all_hours) > 23:
print("hour can't be greater than 23")
raise ValueError("invalid input")
try:
int(to_write["minute"])
except ValueError as error:
print("too frequent: only number in minutes are supported")
raise ValueError("invalid input") from error
return to_write return to_write
@staticmethod
def _validate_cron(to_write):
"""validate all fields, raise value error for impossible schedule"""
all_hours = list(re.split(r"\D+", to_write["hour"]))
for hour in all_hours:
if hour.isdigit() and int(hour) > 23:
print("hour can not be greater than 23")
raise ValueError("invalid input")
all_days = list(re.split(r"\D+", to_write["day_of_week"]))
for day in all_days:
if day.isdigit() and int(day) > 6:
print("day can not be greater than 6")
raise ValueError("invalid input")
if not to_write["minute"].isdigit():
print("too frequent: only number in minutes are supported")
raise ValueError("invalid input")
if int(to_write["minute"]) > 59:
print("minutes can not be greater than 59")
raise ValueError("invalid input")
def build_schedule(self): def build_schedule(self):
"""build schedule dict as expected by app.conf.beat_schedule""" """build schedule dict as expected by app.conf.beat_schedule"""
schedule_dict = {} schedule_dict = {}