move settings to registry

This commit is contained in:
Matthias 2021-11-20 18:01:19 +01:00
parent 8ac41970ad
commit 3aa40ce3e9
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
8 changed files with 25 additions and 16 deletions

View File

@ -882,8 +882,6 @@ if DEBUG or TESTING:
PLUGIN_DIRS.append('plugin.samples') PLUGIN_DIRS.append('plugin.samples')
PLUGINS = [] PLUGINS = []
INTEGRATION_PLUGINS = {}
INTEGRATION_PLUGINS_INACTIVE = {}
INTEGRATION_PLUGIN_GLOBALSETTING = {} INTEGRATION_PLUGIN_GLOBALSETTING = {}
INTEGRATION_APPS_LOADING = True # Marks if apps were reloaded yet INTEGRATION_APPS_LOADING = True # Marks if apps were reloaded yet

View File

@ -1,7 +1,7 @@
from .registry import plugins from .registry import plugins as plugin_reg
from .integration import IntegrationPluginBase from .integration import IntegrationPluginBase
from .action import ActionPlugin from .action import ActionPlugin
__all__ = [ __all__ = [
'plugins', 'IntegrationPluginBase', 'ActionPlugin', 'plugin_reg', 'IntegrationPluginBase', 'ActionPlugin',
] ]

View File

@ -6,13 +6,15 @@ from django.conf import settings
from django.template.loaders.filesystem import Loader as FilesystemLoader from django.template.loaders.filesystem import Loader as FilesystemLoader
from pathlib import Path from pathlib import Path
from plugin import plugin_reg
class PluginTemplateLoader(FilesystemLoader): class PluginTemplateLoader(FilesystemLoader):
def get_dirs(self): def get_dirs(self):
dirname = 'templates' dirname = 'templates'
template_dirs = [] template_dirs = []
for plugin in settings.INTEGRATION_PLUGINS.values(): for plugin in plugin_reg.plugins.values():
new_path = Path(plugin.path) / dirname new_path = Path(plugin.path) / dirname
if Path(new_path).is_dir(): if Path(new_path).is_dir():
template_dirs.append(new_path) template_dirs.append(new_path)

View File

@ -10,6 +10,8 @@ from django.db import models
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
from plugin import plugin_reg
class PluginConfig(models.Model): class PluginConfig(models.Model):
""" A PluginConfig object holds settings for plugins. """ A PluginConfig object holds settings for plugins.
@ -64,7 +66,7 @@ class PluginConfig(models.Model):
self.__org_active = self.active self.__org_active = self.active
# append settings from registry # 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): def get_plugin_meta(name):
if self.plugin: if self.plugin:

View File

@ -34,6 +34,10 @@ logger = logging.getLogger('inventree')
class Plugins: class Plugins:
def __init__(self) -> None:
self.plugins = {}
self.plugins_inactive = {}
# region public plugin functions # region public plugin functions
def load_plugins(self): def load_plugins(self):
"""load and activate all IntegrationPlugins""" """load and activate all IntegrationPlugins"""
@ -166,7 +170,7 @@ class Plugins:
plugin_db_setting.save() plugin_db_setting.save()
# add to inactive plugins so it shows up in the ui # 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 continue # continue -> the plugin is not loaded
# init package # init package
@ -180,10 +184,10 @@ class Plugins:
plugin.pk = plugin_db_setting.pk plugin.pk = plugin_db_setting.pk
# safe reference # safe reference
settings.INTEGRATION_PLUGINS[plugin.slug] = plugin self.plugins[plugin.slug] = plugin
else: else:
# save for later reference # 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): def _activate_plugins(self, force_reload=False):
"""run integration functions for all plugins """run integration functions for all plugins
@ -192,7 +196,7 @@ class Plugins:
:type force_reload: bool, optional :type force_reload: bool, optional
""" """
# activate integrations # activate integrations
plugins = settings.INTEGRATION_PLUGINS.items() plugins = self.plugins.items()
logger.info(f'Found {len(plugins)} active plugins') logger.info(f'Found {len(plugins)} active plugins')
self.activate_integration_globalsettings(plugins) self.activate_integration_globalsettings(plugins)
@ -366,8 +370,8 @@ class Plugins:
def _clean_registry(self): def _clean_registry(self):
# remove all plugins from registry # remove all plugins from registry
settings.INTEGRATION_PLUGINS = {} self.plugins = {}
settings.INTEGRATION_PLUGINS_INACTIVE = {} self.plugins_inactive = {}
def _update_urls(self): def _update_urls(self):
from InvenTree.urls import urlpatterns from InvenTree.urls import urlpatterns

View File

@ -7,6 +7,7 @@ from django import template
from django.urls import reverse from django.urls import reverse
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
from plugin import plugin_reg
register = template.Library() register = template.Library()
@ -15,13 +16,13 @@ register = template.Library()
@register.simple_tag() @register.simple_tag()
def plugin_list(*args, **kwargs): def plugin_list(*args, **kwargs):
""" Return a list of all installed integration plugins """ """ Return a list of all installed integration plugins """
return djangosettings.INTEGRATION_PLUGINS return plugin_reg.plugins
@register.simple_tag() @register.simple_tag()
def inactive_plugin_list(*args, **kwargs): def inactive_plugin_list(*args, **kwargs):
""" Return a list of all inactive integration plugins """ """ Return a list of all inactive integration plugins """
return djangosettings.INTEGRATION_PLUGINS_INACTIVE return plugin_reg.plugins_inactive
@register.simple_tag() @register.simple_tag()

View File

@ -9,6 +9,7 @@ from plugin.samples.integration.sample import SampleIntegrationPlugin
from plugin.samples.integration.another_sample import WrongIntegrationPlugin, NoIntegrationPlugin from plugin.samples.integration.another_sample import WrongIntegrationPlugin, NoIntegrationPlugin
from plugin.plugins import load_integration_plugins # , load_action_plugins, load_barcode_plugins from plugin.plugins import load_integration_plugins # , load_action_plugins, load_barcode_plugins
import plugin.templatetags.plugin_extras as plugin_tags import plugin.templatetags.plugin_extras as plugin_tags
from plugin import plugin_reg
class InvenTreePluginTests(TestCase): class InvenTreePluginTests(TestCase):
@ -57,7 +58,7 @@ class PluginTagTests(TestCase):
def test_tag_plugin_list(self): def test_tag_plugin_list(self):
"""test that all plugins are listed""" """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): def test_tag_plugin_globalsettings(self):
"""check all plugins are listed""" """check all plugins are listed"""

View File

@ -5,6 +5,7 @@ from django.conf import settings
from django.conf.urls import url, include from django.conf.urls import url, include
from plugin.helpers import get_plugin_error from plugin.helpers import get_plugin_error
from plugin import plugin_reg
PLUGIN_BASE = 'plugin' # Constant for links PLUGIN_BASE = 'plugin' # Constant for links
@ -13,7 +14,7 @@ PLUGIN_BASE = 'plugin' # Constant for links
def get_plugin_urls(): def get_plugin_urls():
"""returns a urlpattern that can be integrated into the global urls""" """returns a urlpattern that can be integrated into the global urls"""
urls = [] urls = []
for plugin in settings.INTEGRATION_PLUGINS.values(): for plugin in plugin_reg.plugins.values():
if plugin.mixin_enabled('urls'): if plugin.mixin_enabled('urls'):
urls.append(plugin.urlpatterns) urls.append(plugin.urlpatterns)
# TODO wrap everything in plugin_url_wrapper # TODO wrap everything in plugin_url_wrapper