From 3aa40ce3e94d40e9ecb4110089604cc2a80bd949 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 20 Nov 2021 18:01:19 +0100 Subject: [PATCH] move settings to registry --- InvenTree/InvenTree/settings.py | 2 -- InvenTree/plugin/__init__.py | 4 ++-- InvenTree/plugin/loader.py | 4 +++- InvenTree/plugin/models.py | 4 +++- InvenTree/plugin/registry.py | 16 ++++++++++------ InvenTree/plugin/templatetags/plugin_extras.py | 5 +++-- InvenTree/plugin/test_plugin.py | 3 ++- InvenTree/plugin/urls.py | 3 ++- 8 files changed, 25 insertions(+), 16 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 6027e60bb2..88cd09072e 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -882,8 +882,6 @@ if DEBUG or TESTING: PLUGIN_DIRS.append('plugin.samples') PLUGINS = [] -INTEGRATION_PLUGINS = {} -INTEGRATION_PLUGINS_INACTIVE = {} INTEGRATION_PLUGIN_GLOBALSETTING = {} INTEGRATION_APPS_LOADING = True # Marks if apps were reloaded yet diff --git a/InvenTree/plugin/__init__.py b/InvenTree/plugin/__init__.py index 97b4b9d47f..973d341171 100644 --- a/InvenTree/plugin/__init__.py +++ b/InvenTree/plugin/__init__.py @@ -1,7 +1,7 @@ -from .registry import plugins +from .registry import plugins as plugin_reg from .integration import IntegrationPluginBase from .action import ActionPlugin __all__ = [ - 'plugins', 'IntegrationPluginBase', 'ActionPlugin', + 'plugin_reg', 'IntegrationPluginBase', 'ActionPlugin', ] diff --git a/InvenTree/plugin/loader.py b/InvenTree/plugin/loader.py index 21938f81c6..04b6fc8b8a 100644 --- a/InvenTree/plugin/loader.py +++ b/InvenTree/plugin/loader.py @@ -6,13 +6,15 @@ from django.conf import settings from django.template.loaders.filesystem import Loader as FilesystemLoader from pathlib import Path +from plugin import plugin_reg + class PluginTemplateLoader(FilesystemLoader): def get_dirs(self): dirname = 'templates' template_dirs = [] - for plugin in settings.INTEGRATION_PLUGINS.values(): + for plugin in plugin_reg.plugins.values(): new_path = Path(plugin.path) / dirname if Path(new_path).is_dir(): template_dirs.append(new_path) diff --git a/InvenTree/plugin/models.py b/InvenTree/plugin/models.py index ab53ed52d8..f12a69ecac 100644 --- a/InvenTree/plugin/models.py +++ b/InvenTree/plugin/models.py @@ -10,6 +10,8 @@ from django.db import models from django.apps import apps from django.conf import settings +from plugin import plugin_reg + class PluginConfig(models.Model): """ A PluginConfig object holds settings for plugins. @@ -64,7 +66,7 @@ class PluginConfig(models.Model): self.__org_active = self.active # append settings from registry - self.plugin = settings.INTEGRATION_PLUGINS.get(self.key, None) + self.plugin = plugin_reg.plugins.get(self.key, None) def get_plugin_meta(name): if self.plugin: diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 2e7b19baec..8bb18acaac 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -34,6 +34,10 @@ logger = logging.getLogger('inventree') class Plugins: + def __init__(self) -> None: + self.plugins = {} + self.plugins_inactive = {} + # region public plugin functions def load_plugins(self): """load and activate all IntegrationPlugins""" @@ -166,7 +170,7 @@ class Plugins: plugin_db_setting.save() # add to inactive plugins so it shows up in the ui - settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting + self.plugins_inactive[plug_key] = plugin_db_setting continue # continue -> the plugin is not loaded # init package @@ -180,10 +184,10 @@ class Plugins: plugin.pk = plugin_db_setting.pk # safe reference - settings.INTEGRATION_PLUGINS[plugin.slug] = plugin + self.plugins[plugin.slug] = plugin else: # save for later reference - settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting + self.plugins_inactive[plug_key] = plugin_db_setting def _activate_plugins(self, force_reload=False): """run integration functions for all plugins @@ -192,7 +196,7 @@ class Plugins: :type force_reload: bool, optional """ # activate integrations - plugins = settings.INTEGRATION_PLUGINS.items() + plugins = self.plugins.items() logger.info(f'Found {len(plugins)} active plugins') self.activate_integration_globalsettings(plugins) @@ -366,8 +370,8 @@ class Plugins: def _clean_registry(self): # remove all plugins from registry - settings.INTEGRATION_PLUGINS = {} - settings.INTEGRATION_PLUGINS_INACTIVE = {} + self.plugins = {} + self.plugins_inactive = {} def _update_urls(self): from InvenTree.urls import urlpatterns diff --git a/InvenTree/plugin/templatetags/plugin_extras.py b/InvenTree/plugin/templatetags/plugin_extras.py index 480e6cd5d3..140d2d5298 100644 --- a/InvenTree/plugin/templatetags/plugin_extras.py +++ b/InvenTree/plugin/templatetags/plugin_extras.py @@ -7,6 +7,7 @@ from django import template from django.urls import reverse from common.models import InvenTreeSetting +from plugin import plugin_reg register = template.Library() @@ -15,13 +16,13 @@ register = template.Library() @register.simple_tag() def plugin_list(*args, **kwargs): """ Return a list of all installed integration plugins """ - return djangosettings.INTEGRATION_PLUGINS + return plugin_reg.plugins @register.simple_tag() def inactive_plugin_list(*args, **kwargs): """ Return a list of all inactive integration plugins """ - return djangosettings.INTEGRATION_PLUGINS_INACTIVE + return plugin_reg.plugins_inactive @register.simple_tag() diff --git a/InvenTree/plugin/test_plugin.py b/InvenTree/plugin/test_plugin.py index 45d91d70d2..d127bd5be8 100644 --- a/InvenTree/plugin/test_plugin.py +++ b/InvenTree/plugin/test_plugin.py @@ -9,6 +9,7 @@ from plugin.samples.integration.sample import SampleIntegrationPlugin from plugin.samples.integration.another_sample import WrongIntegrationPlugin, NoIntegrationPlugin from plugin.plugins import load_integration_plugins # , load_action_plugins, load_barcode_plugins import plugin.templatetags.plugin_extras as plugin_tags +from plugin import plugin_reg class InvenTreePluginTests(TestCase): @@ -57,7 +58,7 @@ class PluginTagTests(TestCase): def test_tag_plugin_list(self): """test that all plugins are listed""" - self.assertEqual(plugin_tags.plugin_list(), settings.INTEGRATION_PLUGINS) + self.assertEqual(plugin_tags.plugin_list(), plugin_reg.plugins) def test_tag_plugin_globalsettings(self): """check all plugins are listed""" diff --git a/InvenTree/plugin/urls.py b/InvenTree/plugin/urls.py index 8daa5041e2..cf55e8d6e8 100644 --- a/InvenTree/plugin/urls.py +++ b/InvenTree/plugin/urls.py @@ -5,6 +5,7 @@ from django.conf import settings from django.conf.urls import url, include from plugin.helpers import get_plugin_error +from plugin import plugin_reg PLUGIN_BASE = 'plugin' # Constant for links @@ -13,7 +14,7 @@ PLUGIN_BASE = 'plugin' # Constant for links def get_plugin_urls(): """returns a urlpattern that can be integrated into the global urls""" urls = [] - for plugin in settings.INTEGRATION_PLUGINS.values(): + for plugin in plugin_reg.plugins.values(): if plugin.mixin_enabled('urls'): urls.append(plugin.urlpatterns) # TODO wrap everything in plugin_url_wrapper