From 8b9c80d2a4c13e5c0ff9e377085c97a5db5ce282 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 17:55:05 +0200 Subject: [PATCH 01/11] remove todo that is not fitting any more --- InvenTree/common/models.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 8b50d05413..36f20c42a2 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -1802,10 +1802,8 @@ class WebhookEndpoint(models.Model): def process_webhook(self): if self.token: self.verify = VerificationMethod.TOKEN - # TODO make a object-setting if self.secret: self.verify = VerificationMethod.HMAC - # TODO make a object-setting return True def validate_token(self, payload, headers, request): From c7a2d11893e123ff46f92b71be494b8855331f6c Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 18:02:12 +0200 Subject: [PATCH 02/11] refactor to make simpler --- InvenTree/plugin/base/action/api.py | 6 +----- InvenTree/plugin/base/action/mixins.py | 12 ++---------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/InvenTree/plugin/base/action/api.py b/InvenTree/plugin/base/action/api.py index 998e410dce..944d921487 100644 --- a/InvenTree/plugin/base/action/api.py +++ b/InvenTree/plugin/base/action/api.py @@ -31,11 +31,7 @@ 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() - + plugin.perform_action(request.user, data=data) return Response(plugin.get_response()) # If we got to here, no matching action was found diff --git a/InvenTree/plugin/base/action/mixins.py b/InvenTree/plugin/base/action/mixins.py index 70fea86a7e..0896055e19 100644 --- a/InvenTree/plugin/base/action/mixins.py +++ b/InvenTree/plugin/base/action/mixins.py @@ -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,14 +30,7 @@ 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! """ From 9e590c1bc6802d6273af13677c5ba43a52cb71f9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 18:06:32 +0200 Subject: [PATCH 03/11] this seems fine - just keep it that way --- InvenTree/plugin/base/barcodes/api.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/InvenTree/plugin/base/barcodes/api.py b/InvenTree/plugin/base/barcodes/api.py index 296986b2d1..edd9fc3dcc 100644 --- a/InvenTree/plugin/base/barcodes/api.py +++ b/InvenTree/plugin/base/barcodes/api.py @@ -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(): From 45d70737bd7536221042a8c426db12e43e5f95be Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 18:08:42 +0200 Subject: [PATCH 04/11] we do not have server stats checks so this does not make sende --- InvenTree/plugin/registry.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 6f8c9e0442..311772c795 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -133,7 +133,6 @@ class PluginsRegistry: if retry_counter <= 0: # pragma: no cover if settings.PLUGIN_TESTING: print('[PLUGIN] Max retries, breaking loading') - # TODO error for server status break if settings.PLUGIN_TESTING: print(f'[PLUGIN] Above error occured during testing - {retry_counter}/{settings.PLUGIN_RETRY} retries left') From f4fbd57e6e515c534826aadb891a48842d9bcaa7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 18:09:05 +0200 Subject: [PATCH 05/11] errors fail whole plugins now so no saving --- InvenTree/plugin/registry.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 311772c795..b2769f87aa 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -300,7 +300,6 @@ class PluginsRegistry: # Errors are bad so disable the plugin in the database if not settings.PLUGIN_TESTING: # pragma: no cover plugin_db_setting.active = False - # TODO save the error to the plugin plugin_db_setting.save(no_reload=True) # Add to inactive plugins so it shows up in the ui From d9fe7ac27286e51b99664cb895461a3cc9c783ae Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 18:09:39 +0200 Subject: [PATCH 06/11] general Todo - no specific task removed therefore --- InvenTree/plugin/registry.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index b2769f87aa..3d58634340 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -308,8 +308,6 @@ class PluginsRegistry: # Initialize package # now we can be sure that an admin has activated the plugin - # TODO check more stuff -> as of Nov 2021 there are not many checks in place - # but we could enhance those to check signatures, run the plugin against a whitelist etc. logger.info(f'Loading plugin {plug_name}') try: From 23608e69332017a08c5672fef2678c7957f5827a Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 18:36:33 +0200 Subject: [PATCH 07/11] remove unneeded args --- InvenTree/plugin/base/action/test_action.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/InvenTree/plugin/base/action/test_action.py b/InvenTree/plugin/base/action/test_action.py index 9de672cd6f..9484c6687d 100644 --- a/InvenTree/plugin/base/action/test_action.py +++ b/InvenTree/plugin/base/action/test_action.py @@ -14,7 +14,7 @@ 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""" @@ -29,12 +29,12 @@ class ActionMixinTests(TestCase): def get_info(self): 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""" From cced30c081d289e91174622a31bb2f9cf307712f Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 18:41:39 +0200 Subject: [PATCH 08/11] pass through request data --- InvenTree/plugin/base/action/api.py | 2 +- InvenTree/plugin/base/action/mixins.py | 10 +++++----- InvenTree/plugin/builtin/action/simpleactionplugin.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/InvenTree/plugin/base/action/api.py b/InvenTree/plugin/base/action/api.py index 944d921487..607f7dd9e4 100644 --- a/InvenTree/plugin/base/action/api.py +++ b/InvenTree/plugin/base/action/api.py @@ -32,7 +32,7 @@ class ActionPluginView(APIView): for plugin in action_plugins: if plugin.action_name() == action: plugin.perform_action(request.user, data=data) - return Response(plugin.get_response()) + return Response(plugin.get_response(request.user, data=data)) # If we got to here, no matching action was found return Response({ diff --git a/InvenTree/plugin/base/action/mixins.py b/InvenTree/plugin/base/action/mixins.py index 0896055e19..9c6de306e5 100644 --- a/InvenTree/plugin/base/action/mixins.py +++ b/InvenTree/plugin/base/action/mixins.py @@ -35,7 +35,7 @@ class ActionMixin: Override this method to perform the action! """ - def get_result(self): + def get_result(self, user=None, data=None): """ Result of the action? """ @@ -43,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), } diff --git a/InvenTree/plugin/builtin/action/simpleactionplugin.py b/InvenTree/plugin/builtin/action/simpleactionplugin.py index d2a321789d..97043d5223 100644 --- a/InvenTree/plugin/builtin/action/simpleactionplugin.py +++ b/InvenTree/plugin/builtin/action/simpleactionplugin.py @@ -16,9 +16,9 @@ class SimpleActionPlugin(ActionMixin, InvenTreePlugin): def perform_action(self): print("Action plugin in action!") - def get_info(self): + def get_info(self, user, data): return { - "user": self.user.username, + "user": user.username, "hello": "world", } From a8d3ee15bfe43c4e3be1296e006b5ddf1574399f Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 20:05:56 +0200 Subject: [PATCH 09/11] fix func definition --- InvenTree/plugin/base/action/test_action.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/InvenTree/plugin/base/action/test_action.py b/InvenTree/plugin/base/action/test_action.py index 9484c6687d..16790ccd62 100644 --- a/InvenTree/plugin/base/action/test_action.py +++ b/InvenTree/plugin/base/action/test_action.py @@ -20,13 +20,13 @@ class ActionMixinTests(TestCase): """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() From 9c342e1fe361fe73ffc29b1d32f2880370ac7c88 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 20:06:07 +0200 Subject: [PATCH 10/11] fix definition --- InvenTree/plugin/builtin/action/test_simpleactionplugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/plugin/builtin/action/test_simpleactionplugin.py b/InvenTree/plugin/builtin/action/test_simpleactionplugin.py index 6406810894..92e1affa67 100644 --- a/InvenTree/plugin/builtin/action/test_simpleactionplugin.py +++ b/InvenTree/plugin/builtin/action/test_simpleactionplugin.py @@ -15,7 +15,7 @@ class SimpleActionPluginTests(TestCase): self.test_user = user.objects.create_user('testuser', 'test@testing.com', 'password') self.client.login(username='testuser', password='password') - self.plugin = SimpleActionPlugin(user=self.test_user) + self.plugin = SimpleActionPlugin() def test_name(self): """check plugn names """ From 74a3abc4a2221a6e15c94c860deae2738497fb08 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 15 May 2022 23:26:46 +0200 Subject: [PATCH 11/11] make args wider --- InvenTree/plugin/builtin/action/simpleactionplugin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/InvenTree/plugin/builtin/action/simpleactionplugin.py b/InvenTree/plugin/builtin/action/simpleactionplugin.py index 97043d5223..54598e72cb 100644 --- a/InvenTree/plugin/builtin/action/simpleactionplugin.py +++ b/InvenTree/plugin/builtin/action/simpleactionplugin.py @@ -13,14 +13,14 @@ class SimpleActionPlugin(ActionMixin, InvenTreePlugin): NAME = "SimpleActionPlugin" ACTION_NAME = "simple" - def perform_action(self): + def perform_action(self, user=None, data=None): print("Action plugin in action!") - def get_info(self, user, data): + def get_info(self, user, data=None): return { "user": user.username, "hello": "world", } - def get_result(self): + def get_result(self, user=None, data=None): return True