2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 11:10:54 +00:00

Add validation for scheduled tasks defined by a plugin

This commit is contained in:
Oliver Walters
2022-01-07 15:37:43 +11:00
parent 326b897d14
commit 794a9e75e8
6 changed files with 62 additions and 7 deletions

View File

@ -69,8 +69,12 @@ class ScheduleMixin:
'repeats': 5, # Number of repeats (leave blank for 'forever')
}
}
Note: 'schedule' parameter must be one of ['I', 'H', 'D', 'W', 'M', 'Q', 'Y']
"""
ALLOWABLE_SCHEDULE_TYPES = ['I', 'H', 'D', 'W', 'M', 'Q', 'Y']
SCHEDULED_TASKS = {}
class MixinMeta:
@ -92,11 +96,25 @@ class ScheduleMixin:
Check that the provided scheduled tasks are valid
"""
if not self.has_scheduled_tasks():
raise ValueError(f"SCHEDULED_TASKS not defined for plugin '{__class__}'")
if not self.has_scheduled_tasks:
raise ValueError(f"SCHEDULED_TASKS not defined")
for key, task in self.scheduled_tasks.items():
print(key, task)
if 'func' not in task:
raise ValueError(f"Task '{key}' is missing 'func' parameter")
if 'schedule' not in task:
raise ValueError(f"Task '{key}' is missing 'schedule' parameter")
schedule = task['schedule'].upper().strip()
if schedule not in self.ALLOWABLE_SCHEDULE_TYPES:
raise ValueError(f"Task '{key}': Schedule '{schedule}' is not a valid option")
# If 'minutes' is selected, it must be provided!
if schedule == 'I' and 'minutes' not in task:
raise ValueError(f"Task '{key}' is missing 'minutes' parameter")
class UrlsMixin:
"""