mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 13:05:42 +00:00
@ -31,12 +31,8 @@ class ActionPluginView(APIView):
|
||||
action_plugins = registry.with_mixin('action')
|
||||
for plugin in action_plugins:
|
||||
if plugin.action_name() == action:
|
||||
# TODO @matmair use easier syntax once InvenTree 0.7.0 is released
|
||||
plugin.init(request.user, data=data)
|
||||
|
||||
plugin.perform_action()
|
||||
|
||||
return Response(plugin.get_response())
|
||||
plugin.perform_action(request.user, data=data)
|
||||
return Response(plugin.get_response(request.user, data=data))
|
||||
|
||||
# If we got to here, no matching action was found
|
||||
return Response({
|
||||
|
@ -15,10 +15,9 @@ class ActionMixin:
|
||||
"""
|
||||
MIXIN_NAME = 'Actions'
|
||||
|
||||
def __init__(self, user=None, data=None):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('action', True, __class__)
|
||||
self.init(user, data)
|
||||
|
||||
def action_name(self):
|
||||
"""
|
||||
@ -31,19 +30,12 @@ class ActionMixin:
|
||||
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):
|
||||
def perform_action(self, user=None, data=None):
|
||||
"""
|
||||
Override this method to perform the action!
|
||||
"""
|
||||
|
||||
def get_result(self):
|
||||
def get_result(self, user=None, data=None):
|
||||
"""
|
||||
Result of the action?
|
||||
"""
|
||||
@ -51,19 +43,19 @@ class ActionMixin:
|
||||
# Re-implement this for cutsom actions
|
||||
return False
|
||||
|
||||
def get_info(self):
|
||||
def get_info(self, user=None, data=None):
|
||||
"""
|
||||
Extra info? Can be a string / dict / etc
|
||||
"""
|
||||
return None
|
||||
|
||||
def get_response(self):
|
||||
def get_response(self, user=None, data=None):
|
||||
"""
|
||||
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(),
|
||||
"result": self.get_result(user, data),
|
||||
"info": self.get_info(user, data),
|
||||
}
|
||||
|
@ -14,27 +14,27 @@ class ActionMixinTests(TestCase):
|
||||
def setUp(self):
|
||||
class SimplePlugin(ActionMixin, InvenTreePlugin):
|
||||
pass
|
||||
self.plugin = SimplePlugin('user')
|
||||
self.plugin = SimplePlugin()
|
||||
|
||||
class TestActionPlugin(ActionMixin, InvenTreePlugin):
|
||||
"""a action plugin"""
|
||||
ACTION_NAME = 'abc123'
|
||||
|
||||
def perform_action(self):
|
||||
def perform_action(self, user=None, data=None):
|
||||
return ActionMixinTests.ACTION_RETURN + 'action'
|
||||
|
||||
def get_result(self):
|
||||
def get_result(self, user=None, data=None):
|
||||
return ActionMixinTests.ACTION_RETURN + 'result'
|
||||
|
||||
def get_info(self):
|
||||
def get_info(self, user=None, data=None):
|
||||
return ActionMixinTests.ACTION_RETURN + 'info'
|
||||
|
||||
self.action_plugin = TestActionPlugin('user')
|
||||
self.action_plugin = TestActionPlugin()
|
||||
|
||||
class NameActionPlugin(ActionMixin, InvenTreePlugin):
|
||||
NAME = 'Aplugin'
|
||||
|
||||
self.action_name = NameActionPlugin('user')
|
||||
self.action_name = NameActionPlugin()
|
||||
|
||||
def test_action_name(self):
|
||||
"""check the name definition possibilities"""
|
||||
|
@ -63,7 +63,6 @@ class BarcodeScan(APIView):
|
||||
plugin = None
|
||||
|
||||
for current_plugin in plugins:
|
||||
# TODO @matmair make simpler after InvenTree 0.7.0 release
|
||||
current_plugin.init(barcode_data)
|
||||
|
||||
if current_plugin.validate():
|
||||
@ -168,7 +167,6 @@ class BarcodeAssign(APIView):
|
||||
plugin = None
|
||||
|
||||
for current_plugin in plugins:
|
||||
# TODO @matmair make simpler after InvenTree 0.7.0 release
|
||||
current_plugin.init(barcode_data)
|
||||
|
||||
if current_plugin.validate():
|
||||
|
Reference in New Issue
Block a user