2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 12:35:46 +00:00

Add "ActionPlugin" interface

- Plugin for running a custom action
This commit is contained in:
Oliver Walters
2020-04-15 00:16:42 +10:00
parent 4d7407ee51
commit a58e2e84f8
6 changed files with 161 additions and 7 deletions

View File

View File

@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-
import plugins.plugin as plugin
class ActionPlugin(plugin.InvenTreePlugin):
"""
The ActionPlugin class is used to perform custom actions
"""
ACTION_NAME = ""
@classmethod
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!
"""
pass
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(),
}
class SimpleActionPlugin(ActionPlugin):
"""
An EXTREMELY simple action plugin which demonstrates
the capability of the ActionPlugin class
"""
PLUGIN_NAME = "SimpleActionPlugin"
ACTION_NAME = "simple"
def perform_action(self):
print("Action plugin in action!")
def get_info(self):
return {
"user": self.user.username,
"hello": "world",
}
def get_result(self):
return True

View File

@ -9,7 +9,7 @@ class InvenTreePlugin():
# Override the plugin name for each concrete plugin instance
PLUGIN_NAME = ''
def get_name(self):
def plugin_name(self):
return self.PLUGIN_NAME
def __init__(self):

View File

@ -8,6 +8,10 @@ import pkgutil
import plugins.barcode as barcode
from plugins.barcode.barcode import BarcodePlugin
# Action plugins
import plugins.action as action
from plugins.action.action import ActionPlugin
def iter_namespace(pkg):
@ -16,7 +20,7 @@ def iter_namespace(pkg):
def get_modules(pkg):
# Return all modules in a given package
return [importlib.import_module(name) for finder, name, ispkg in iter_namespace(barcode)]
return [importlib.import_module(name) for finder, name, ispkg in iter_namespace(pkg)]
def get_classes(module):
@ -41,7 +45,7 @@ def get_plugins(pkg, baseclass):
# Iterate through each class in the module
for item in get_classes(mod):
plugin = item[1]
if plugin.__class__ is type(baseclass) and plugin.PLUGIN_NAME:
if issubclass(plugin, baseclass) and plugin.PLUGIN_NAME:
plugins.append(plugin)
return plugins
@ -52,6 +56,8 @@ def load_barcode_plugins():
Return a list of all registered barcode plugins
"""
print("Loading barcode plugins")
plugins = get_plugins(barcode, BarcodePlugin)
if len(plugins) > 0:
@ -61,3 +67,21 @@ def load_barcode_plugins():
print(" - {bp}".format(bp=bp.PLUGIN_NAME))
return plugins
def load_action_plugins():
"""
Return a list of all registered action plugins
"""
print("Loading action plugins")
plugins = get_plugins(action, ActionPlugin)
if len(plugins) > 0:
print("Discovered {n} action plugins:".format(n=len(plugins)))
for ap in plugins:
print(" - {ap}".format(ap=ap.PLUGIN_NAME))
return plugins