mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-02 21:38:48 +00:00
Add validation for scheduled tasks defined by a plugin
This commit is contained in:
parent
326b897d14
commit
794a9e75e8
@ -978,6 +978,13 @@ class InvenTreeSetting(BaseInvenTreeSetting):
|
|||||||
'validator': bool,
|
'validator': bool,
|
||||||
'requires_restart': True,
|
'requires_restart': True,
|
||||||
},
|
},
|
||||||
|
'ENABLE_PLUGINS_SCHEDULE': {
|
||||||
|
'name': _('Enable schedule integration'),
|
||||||
|
'description': _('Enable plugins to run scheduled tasks'),
|
||||||
|
'default': False,
|
||||||
|
'validator': bool,
|
||||||
|
'requires_restart': True,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -69,8 +69,12 @@ class ScheduleMixin:
|
|||||||
'repeats': 5, # Number of repeats (leave blank for 'forever')
|
'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 = {}
|
SCHEDULED_TASKS = {}
|
||||||
|
|
||||||
class MixinMeta:
|
class MixinMeta:
|
||||||
@ -92,11 +96,25 @@ class ScheduleMixin:
|
|||||||
Check that the provided scheduled tasks are valid
|
Check that the provided scheduled tasks are valid
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not self.has_scheduled_tasks():
|
if not self.has_scheduled_tasks:
|
||||||
raise ValueError(f"SCHEDULED_TASKS not defined for plugin '{__class__}'")
|
raise ValueError(f"SCHEDULED_TASKS not defined")
|
||||||
|
|
||||||
for key, task in self.scheduled_tasks.items():
|
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:
|
class UrlsMixin:
|
||||||
"""
|
"""
|
||||||
|
@ -94,6 +94,10 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
|
|||||||
def slug(self):
|
def slug(self):
|
||||||
return self.plugin_slug()
|
return self.plugin_slug()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return self.plugin_name()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def human_name(self):
|
def human_name(self):
|
||||||
"""human readable name for labels etc."""
|
"""human readable name for labels etc."""
|
||||||
|
@ -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__ = [
|
__all__ = [
|
||||||
'AppMixin',
|
'AppMixin',
|
||||||
'NavigationMixin',
|
'NavigationMixin',
|
||||||
|
'ScheduleMixin',
|
||||||
'SettingsMixin',
|
'SettingsMixin',
|
||||||
'UrlsMixin',
|
'UrlsMixin',
|
||||||
]
|
]
|
||||||
|
@ -6,6 +6,15 @@ from plugin import IntegrationPluginBase
|
|||||||
from plugin.mixins import ScheduleMixin
|
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):
|
class ScheduledTaskPlugin(ScheduleMixin, IntegrationPluginBase):
|
||||||
"""
|
"""
|
||||||
A sample plugin which provides support for scheduled tasks
|
A sample plugin which provides support for scheduled tasks
|
||||||
@ -13,4 +22,16 @@ class ScheduledTaskPlugin(ScheduleMixin, IntegrationPluginBase):
|
|||||||
|
|
||||||
PLUGIN_NAME = "ScheduledTasksPlugin"
|
PLUGIN_NAME = "ScheduledTasksPlugin"
|
||||||
PLUGIN_SLUG = "schedule"
|
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',
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@
|
|||||||
<div class='table-responsive'>
|
<div class='table-responsive'>
|
||||||
<table class='table table-striped table-condensed'>
|
<table class='table table-striped table-condensed'>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_SCHEDULE" icon="fa-calendar-alt" %}
|
||||||
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_URL" icon="fa-link" %}
|
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_URL" icon="fa-link" %}
|
||||||
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_NAVIGATION" icon="fa-sitemap" %}
|
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_NAVIGATION" icon="fa-sitemap" %}
|
||||||
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_APP" icon="fa-rocket" %}
|
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_APP" icon="fa-rocket" %}
|
||||||
@ -28,7 +29,7 @@
|
|||||||
|
|
||||||
<div class='panel-heading'>
|
<div class='panel-heading'>
|
||||||
<div class='d-flex flex-wrap'>
|
<div class='d-flex flex-wrap'>
|
||||||
<h4>{% trans "Plugin list" %}</h4>
|
<h4>{% trans "Plugins" %}</h4>
|
||||||
{% include "spacer.html" %}
|
{% include "spacer.html" %}
|
||||||
<div class='btn-group' role='group'>
|
<div class='btn-group' role='group'>
|
||||||
{% url 'admin:plugin_pluginconfig_changelist' as url %}
|
{% url 'admin:plugin_pluginconfig_changelist' as url %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user