2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 04:55:44 +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:
"""

View File

@ -94,6 +94,10 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
def slug(self):
return self.plugin_slug()
@property
def name(self):
return self.plugin_name()
@property
def human_name(self):
"""human readable name for labels etc."""

View File

@ -1,9 +1,13 @@
"""utility class to enable simpler imports"""
from ..builtin.integration.mixins import AppMixin, SettingsMixin, UrlsMixin, NavigationMixin
"""
Utility class to enable simpler imports
"""
from ..builtin.integration.mixins import AppMixin, SettingsMixin, ScheduleMixin, UrlsMixin, NavigationMixin
__all__ = [
'AppMixin',
'NavigationMixin',
'ScheduleMixin',
'SettingsMixin',
'UrlsMixin',
]

View File

@ -6,6 +6,15 @@ from plugin import IntegrationPluginBase
from plugin.mixins import ScheduleMixin
# Define some simple tasks to perform
def print_hello():
print("Hello")
def print_world():
print("World")
class ScheduledTaskPlugin(ScheduleMixin, IntegrationPluginBase):
"""
A sample plugin which provides support for scheduled tasks
@ -13,4 +22,16 @@ class ScheduledTaskPlugin(ScheduleMixin, IntegrationPluginBase):
PLUGIN_NAME = "ScheduledTasksPlugin"
PLUGIN_SLUG = "schedule"
PLUGIN_TITLE = "A plugin which provides scheduled task support"
PLUGIN_TITLE = "Scheduled Tasks"
SCHEDULED_TASKS = {
'hello': {
'func': 'plugin.builtin.integration.mixins.ScheduleMixin.print_hello',
'schedule': 'I',
'minutes': 5,
},
'world': {
'func': 'plugin.builtin.integration.mixins.ScheduleMixin.print_world',
'schedule': 'H',
}
}