From 99e4b6f6a54669904e0c7a31dfc45f8b84eb3b93 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 16 Oct 2021 17:47:05 +0200 Subject: [PATCH] settting to control app loading --- InvenTree/InvenTree/settings.py | 6 +---- InvenTree/common/models.py | 6 +++++ InvenTree/plugin/apps.py | 26 ++++++++++++++++++- .../templates/InvenTree/settings/plugin.html | 1 + 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index ba17ed6815..e5458f978d 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -759,13 +759,9 @@ INTEGRATION_PLUGINS = [] INTEGRATION_PLUGIN_SETTINGS = {} INTEGRATION_PLUGIN_SETTING = {} INTEGRATION_PLUGIN_LIST = {} +INTEGRATION_APPS_LOADED = False # Marks if apps were reloaded yet for plugin in inventree_plugins.load_integration_plugins(): plugin = plugin() - INTEGRATION_PLUGINS.append(plugin) INTEGRATION_PLUGIN_LIST[plugin.slug] = plugin - - if plugin.mixin_enabled('app'): - plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(BASE_DIR).parts) - INSTALLED_APPS += [plugin_path] diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 1415319b58..3ddad8ff5d 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -872,6 +872,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'ENABLE_PLUGINS_APP': { + 'name': _('Enable app integration'), + 'description': _('Enable plugins to add apps'), + 'default': False, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index 197834c373..f4f2966d0a 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import pathlib +from typing import OrderedDict +from allauth.socialaccount import app_settings -from django.apps import AppConfig +from django.apps import AppConfig, apps from django.conf import settings @@ -11,9 +14,30 @@ class PluginConfig(AppConfig): def ready(self): from common.models import InvenTreeSetting + # if plugin settings are enabled enhance the settings if InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'): for slug, plugin in settings.INTEGRATION_PLUGIN_LIST.items(): if plugin.mixin_enabled('settings'): plugin_setting = plugin.settingspatterns settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting settings.INTEGRATION_PLUGIN_SETTINGS.update(plugin_setting) + + # if plugin apps are enabled + if (not settings.INTEGRATION_APPS_LOADED) and InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP'): + settings.INTEGRATION_APPS_LOADED = True # ensure this section will not run again + apps_changed = False + + # add them to the INSTALLED_APPS + for slug, plugin in settings.INTEGRATION_PLUGIN_LIST.items(): + if plugin.mixin_enabled('app'): + plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(settings.BASE_DIR).parts) + settings.INSTALLED_APPS += [plugin_path] + apps_changed = True + + # if apps were changed reload + # TODO this is a bit jankey to be honest + if apps_changed: + apps.app_configs = OrderedDict() + apps.apps_ready = apps.models_ready = apps.loading = apps.ready = False + apps.clear_cache() + apps.populate(settings.INSTALLED_APPS) diff --git a/InvenTree/templates/InvenTree/settings/plugin.html b/InvenTree/templates/InvenTree/settings/plugin.html index 69c9bb719a..05ebd27816 100644 --- a/InvenTree/templates/InvenTree/settings/plugin.html +++ b/InvenTree/templates/InvenTree/settings/plugin.html @@ -19,6 +19,7 @@ {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_URL" %} {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_NAVIGATION" %} {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_SETTING"%} + {% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_APP"%}