mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 20:46:47 +00:00
refactor ActionPlugin to use mixin
This commit is contained in:
parent
a2b88dd0fa
commit
8088bf28fe
@ -2,69 +2,22 @@
|
|||||||
"""Class for ActionPlugin"""
|
"""Class for ActionPlugin"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import warnings
|
||||||
|
|
||||||
import plugin.plugin as plugin
|
import plugin.plugin as plugin
|
||||||
|
from plugin.builtin.action.mixins import ActionMixin
|
||||||
|
import plugin.integration
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger("inventree")
|
logger = logging.getLogger("inventree")
|
||||||
|
|
||||||
|
|
||||||
class ActionPlugin(plugin.InvenTreePlugin):
|
class ActionPlugin(ActionMixin, plugin.integration.IntegrationPluginBase):
|
||||||
"""
|
"""
|
||||||
The ActionPlugin class is used to perform custom actions
|
Legacy action definition - will be replaced
|
||||||
|
Please use the new Integration Plugin API and the Action mixin
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, user=None, data=None):
|
||||||
ACTION_NAME = ""
|
warnings.warn("using the ActionPlugin is depreceated", DeprecationWarning)
|
||||||
|
super().__init__()
|
||||||
@classmethod
|
self.init(user, data)
|
||||||
def action_name(cls):
|
|
||||||
"""
|
|
||||||
Return the action name for this plugin.
|
|
||||||
If the ACTION_NAME parameter is empty,
|
|
||||||
look at the PLUGIN_NAME instead.
|
|
||||||
"""
|
|
||||||
action = cls.ACTION_NAME
|
|
||||||
|
|
||||||
if not action:
|
|
||||||
action = cls.PLUGIN_NAME
|
|
||||||
|
|
||||||
return action
|
|
||||||
|
|
||||||
def __init__(self, user, data=None):
|
|
||||||
"""
|
|
||||||
An action plugin takes a user reference, and an optional dataset (dict)
|
|
||||||
"""
|
|
||||||
plugin.InvenTreePlugin.__init__(self)
|
|
||||||
|
|
||||||
self.user = user
|
|
||||||
self.data = data
|
|
||||||
|
|
||||||
def perform_action(self):
|
|
||||||
"""
|
|
||||||
Override this method to perform the action!
|
|
||||||
"""
|
|
||||||
|
|
||||||
def get_result(self):
|
|
||||||
"""
|
|
||||||
Result of the action?
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Re-implement this for cutsom actions
|
|
||||||
return False
|
|
||||||
|
|
||||||
def get_info(self):
|
|
||||||
"""
|
|
||||||
Extra info? Can be a string / dict / etc
|
|
||||||
"""
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_response(self):
|
|
||||||
"""
|
|
||||||
Return a response. Default implementation is a simple response
|
|
||||||
which can be overridden.
|
|
||||||
"""
|
|
||||||
return {
|
|
||||||
"action": self.action_name(),
|
|
||||||
"result": self.get_result(),
|
|
||||||
"info": self.get_info(),
|
|
||||||
}
|
|
||||||
|
73
InvenTree/plugin/builtin/action/mixins.py
Normal file
73
InvenTree/plugin/builtin/action/mixins.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
"""
|
||||||
|
Plugin mixin classes for action plugin
|
||||||
|
"""
|
||||||
|
|
||||||
|
class ActionMixin:
|
||||||
|
"""
|
||||||
|
Mixin that enables custom actions
|
||||||
|
"""
|
||||||
|
ACTION_NAME = ""
|
||||||
|
|
||||||
|
class MixinMeta:
|
||||||
|
"""
|
||||||
|
meta options for this mixin
|
||||||
|
"""
|
||||||
|
MIXIN_NAME = 'Actions'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.add_mixin('action', 'has_action', __class__)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def has_action(self):
|
||||||
|
"""
|
||||||
|
Does this plugin have everything needed for an action?
|
||||||
|
"""
|
||||||
|
return True
|
||||||
|
|
||||||
|
def action_name(self):
|
||||||
|
"""
|
||||||
|
Return the action name for this plugin.
|
||||||
|
If the ACTION_NAME parameter is empty,
|
||||||
|
look at the PLUGIN_NAME instead.
|
||||||
|
"""
|
||||||
|
if self.ACTION_NAME:
|
||||||
|
return self.ACTION_NAME
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def init(self, user, data=None):
|
||||||
|
"""
|
||||||
|
An action plugin takes a user reference, and an optional dataset (dict)
|
||||||
|
"""
|
||||||
|
self.user = user
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
def perform_action(self):
|
||||||
|
"""
|
||||||
|
Override this method to perform the action!
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get_result(self):
|
||||||
|
"""
|
||||||
|
Result of the action?
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Re-implement this for cutsom actions
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_info(self):
|
||||||
|
"""
|
||||||
|
Extra info? Can be a string / dict / etc
|
||||||
|
"""
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_response(self):
|
||||||
|
"""
|
||||||
|
Return a response. Default implementation is a simple response
|
||||||
|
which can be overridden.
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
"action": self.action_name(),
|
||||||
|
"result": self.get_result(),
|
||||||
|
"info": self.get_info(),
|
||||||
|
}
|
@ -301,3 +301,75 @@ class AppMixin:
|
|||||||
this plugin is always an app with this plugin
|
this plugin is always an app with this plugin
|
||||||
"""
|
"""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class ActionMixin:
|
||||||
|
"""
|
||||||
|
Mixin that enables custom actions
|
||||||
|
"""
|
||||||
|
ACTION_NAME = ""
|
||||||
|
|
||||||
|
class MixinMeta:
|
||||||
|
"""
|
||||||
|
meta options for this mixin
|
||||||
|
"""
|
||||||
|
MIXIN_NAME = 'Action'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.add_mixin('action', 'has_action', __class__)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def has_action(self):
|
||||||
|
"""
|
||||||
|
Does this plugin have everything needed for an action?
|
||||||
|
"""
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def action_name(self):
|
||||||
|
"""
|
||||||
|
Return the action name for this plugin.
|
||||||
|
If the ACTION_NAME parameter is empty,
|
||||||
|
look at the PLUGIN_NAME instead.
|
||||||
|
"""
|
||||||
|
if self.ACTION_NAME:
|
||||||
|
return self.ACTION_NAME
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def init(self, user, data=None):
|
||||||
|
"""
|
||||||
|
An action plugin takes a user reference, and an optional dataset (dict)
|
||||||
|
"""
|
||||||
|
self.user = user
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
def perform_action(self):
|
||||||
|
"""
|
||||||
|
Override this method to perform the action!
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get_result(self):
|
||||||
|
"""
|
||||||
|
Result of the action?
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Re-implement this for custom actions
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_info(self):
|
||||||
|
"""
|
||||||
|
Extra info? Can be a string / dict / etc
|
||||||
|
"""
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_response(self):
|
||||||
|
"""
|
||||||
|
Return a response. Default implementation is a simple response
|
||||||
|
which can be overridden.
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
"action": self.action_name(),
|
||||||
|
"result": self.get_result(),
|
||||||
|
"info": self.get_info(),
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ Utility class to enable simpler imports
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from ..builtin.integration.mixins import AppMixin, SettingsMixin, ScheduleMixin, UrlsMixin, NavigationMixin
|
from ..builtin.integration.mixins import AppMixin, SettingsMixin, ScheduleMixin, UrlsMixin, NavigationMixin
|
||||||
|
from ..builtin.action.mixins import ActionMixin
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'AppMixin',
|
'AppMixin',
|
||||||
@ -10,4 +11,5 @@ __all__ = [
|
|||||||
'ScheduleMixin',
|
'ScheduleMixin',
|
||||||
'SettingsMixin',
|
'SettingsMixin',
|
||||||
'UrlsMixin',
|
'UrlsMixin',
|
||||||
|
'ActionMixin',
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user