catch db not loaded

This commit is contained in:
Matthias 2021-10-19 20:59:14 +02:00
parent 23558e235b
commit f86bd4dd6b
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -5,6 +5,7 @@ from typing import OrderedDict
from django.apps import AppConfig, apps from django.apps import AppConfig, apps
from django.conf import settings from django.conf import settings
from django.db.utils import OperationalError, ProgrammingError
class PluginConfig(AppConfig): class PluginConfig(AppConfig):
@ -14,33 +15,37 @@ class PluginConfig(AppConfig):
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
plugins = settings.INTEGRATION_PLUGINS.items() plugins = settings.INTEGRATION_PLUGINS.items()
# if plugin settings are enabled enhance the settings try:
if settings.TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'): # if plugin settings are enabled enhance the settings
for slug, plugin in plugins: if settings.TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'):
if plugin.mixin_enabled('settings'): for slug, plugin in plugins:
plugin_setting = plugin.settingspatterns if plugin.mixin_enabled('settings'):
settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting plugin_setting = plugin.settingspatterns
settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting
# Add to settings dir # Add to settings dir
InvenTreeSetting.GLOBAL_SETTINGS.update(plugin_setting) InvenTreeSetting.GLOBAL_SETTINGS.update(plugin_setting)
# if plugin apps are enabled # if plugin apps are enabled
if settings.TESTING or ((not settings.INTEGRATION_APPS_LOADED) and InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP')): if settings.TESTING or ((not settings.INTEGRATION_APPS_LOADED) and InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP')):
settings.INTEGRATION_APPS_LOADED = True # ensure this section will not run again settings.INTEGRATION_APPS_LOADED = True # ensure this section will not run again
apps_changed = False apps_changed = False
# add them to the INSTALLED_APPS # add them to the INSTALLED_APPS
for slug, plugin in plugins: for slug, plugin in plugins:
if plugin.mixin_enabled('app'): if plugin.mixin_enabled('app'):
plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(settings.BASE_DIR).parts) plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(settings.BASE_DIR).parts)
if plugin_path not in settings.INSTALLED_APPS: if plugin_path not in settings.INSTALLED_APPS:
settings.INSTALLED_APPS += [plugin_path] settings.INSTALLED_APPS += [plugin_path]
apps_changed = True apps_changed = True
# if apps were changed reload # if apps were changed reload
# TODO this is a bit jankey to be honest # TODO this is a bit jankey to be honest
if apps_changed: if apps_changed:
apps.app_configs = OrderedDict() apps.app_configs = OrderedDict()
apps.apps_ready = apps.models_ready = apps.loading = apps.ready = False apps.apps_ready = apps.models_ready = apps.loading = apps.ready = False
apps.clear_cache() apps.clear_cache()
apps.populate(settings.INSTALLED_APPS) apps.populate(settings.INSTALLED_APPS)
except (OperationalError, ProgrammingError):
# Exception if the database has not been migrated yet
pass