mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-09 21:30:54 +00:00
switch to classmethods for loading
This commit is contained in:
@@ -25,10 +25,12 @@ class AppMixin:
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.add_mixin('app', 'has_app', __class__)
|
self.add_mixin('app', 'has_app', __class__)
|
||||||
|
|
||||||
def _activate_mixin(self, plugins, force_reload=False, full_reload: bool = False):
|
@classmethod
|
||||||
|
def _activate_mixin(cls, registry, plugins, force_reload=False, full_reload: bool = False):
|
||||||
"""Activate AppMixin plugins - add custom apps and reload.
|
"""Activate AppMixin plugins - add custom apps and reload.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
registry (PluginRegistry): The registry that should be used
|
||||||
plugins (dict): List of IntegrationPlugins that should be installed
|
plugins (dict): List of IntegrationPlugins that should be installed
|
||||||
force_reload (bool, optional): Only reload base apps. Defaults to False.
|
force_reload (bool, optional): Only reload base apps. Defaults to False.
|
||||||
full_reload (bool, optional): Reload everything - including plugin mechanism. Defaults to False.
|
full_reload (bool, optional): Reload everything - including plugin mechanism. Defaults to False.
|
||||||
@@ -42,30 +44,31 @@ class AppMixin:
|
|||||||
# add them to the INSTALLED_APPS
|
# add them to the INSTALLED_APPS
|
||||||
for _key, plugin in plugins:
|
for _key, plugin in plugins:
|
||||||
if plugin.mixin_enabled('app'):
|
if plugin.mixin_enabled('app'):
|
||||||
plugin_path = self._get_plugin_path(plugin)
|
plugin_path = cls._get_plugin_path(plugin)
|
||||||
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]
|
||||||
self.installed_apps += [plugin_path]
|
registry.installed_apps += [plugin_path]
|
||||||
apps_changed = True
|
apps_changed = True
|
||||||
# if apps were changed or force loading base apps -> reload
|
# if apps were changed or force loading base apps -> reload
|
||||||
if apps_changed or force_reload:
|
if apps_changed or force_reload:
|
||||||
# first startup or force loading of base apps -> registry is prob false
|
# first startup or force loading of base apps -> registry is prob false
|
||||||
if self.apps_loading or force_reload:
|
if registry.apps_loading or force_reload:
|
||||||
self.apps_loading = False
|
registry.apps_loading = False
|
||||||
self._reload_apps(force_reload=True, full_reload=full_reload)
|
cls._reload_apps(force_reload=True, full_reload=full_reload)
|
||||||
else:
|
else:
|
||||||
self._reload_apps(full_reload=full_reload)
|
cls._reload_apps(full_reload=full_reload)
|
||||||
|
|
||||||
# rediscover models/ admin sites
|
# rediscover models/ admin sites
|
||||||
self._reregister_contrib_apps()
|
cls._reregister_contrib_apps()
|
||||||
|
|
||||||
# update urls - must be last as models must be registered for creating admin routes
|
# update urls - must be last as models must be registered for creating admin routes
|
||||||
self._update_urls()
|
registry._update_urls()
|
||||||
|
|
||||||
def _deactivate_mixin(self):
|
@classmethod
|
||||||
|
def _deactivate_mixin(cls, registry):
|
||||||
"""Deactivate AppMixin plugins - some magic required."""
|
"""Deactivate AppMixin plugins - some magic required."""
|
||||||
# unregister models from admin
|
# unregister models from admin
|
||||||
for plugin_path in self.installed_apps:
|
for plugin_path in registry.installed_apps:
|
||||||
models = [] # the modelrefs need to be collected as poping an item in a iter is not welcomed
|
models = [] # the modelrefs need to be collected as poping an item in a iter is not welcomed
|
||||||
app_name = plugin_path.split('.')[-1]
|
app_name = plugin_path.split('.')[-1]
|
||||||
try:
|
try:
|
||||||
@@ -96,23 +99,24 @@ class AppMixin:
|
|||||||
apps.all_models.pop(app_name)
|
apps.all_models.pop(app_name)
|
||||||
|
|
||||||
# remove plugin from installed_apps
|
# remove plugin from installed_apps
|
||||||
self._clean_installed_apps()
|
registry._clean_installed_apps()
|
||||||
|
|
||||||
# reset load flag and reload apps
|
# reset load flag and reload apps
|
||||||
settings.INTEGRATION_APPS_LOADED = False
|
settings.INTEGRATION_APPS_LOADED = False
|
||||||
self._reload_apps()
|
cls._reload_apps()
|
||||||
|
|
||||||
# update urls to remove the apps from the site admin
|
# update urls to remove the apps from the site admin
|
||||||
self._update_urls()
|
registry._update_urls()
|
||||||
|
|
||||||
# region helpers
|
# region helpers
|
||||||
def _reregister_contrib_apps(self):
|
@classmethod
|
||||||
|
def _reregister_contrib_apps(cls):
|
||||||
"""Fix reloading of contrib apps - models and admin.
|
"""Fix reloading of contrib apps - models and admin.
|
||||||
|
|
||||||
This is needed if plugins were loaded earlier and then reloaded as models and admins rely on imports.
|
This is needed if plugins were loaded earlier and then reloaded as models and admins rely on imports.
|
||||||
Those register models and admin in their respective objects (e.g. admin.site for admin).
|
Those register models and admin in their respective objects (e.g. admin.site for admin).
|
||||||
"""
|
"""
|
||||||
for plugin_path in self.installed_apps:
|
for plugin_path in cls.installed_apps:
|
||||||
try:
|
try:
|
||||||
app_name = plugin_path.split('.')[-1]
|
app_name = plugin_path.split('.')[-1]
|
||||||
app_config = apps.get_app_config(app_name)
|
app_config = apps.get_app_config(app_name)
|
||||||
@@ -139,7 +143,8 @@ class AppMixin:
|
|||||||
if model_not_reg and hasattr(app_config.module, 'admin'):
|
if model_not_reg and hasattr(app_config.module, 'admin'):
|
||||||
reload(app_config.module.admin)
|
reload(app_config.module.admin)
|
||||||
|
|
||||||
def _get_plugin_path(self, plugin):
|
@classmethod
|
||||||
|
def _get_plugin_path(cls, plugin):
|
||||||
"""Parse plugin path.
|
"""Parse plugin path.
|
||||||
|
|
||||||
The input can be eiter:
|
The input can be eiter:
|
||||||
@@ -154,7 +159,8 @@ class AppMixin:
|
|||||||
plugin_path = plugin.__module__.split('.')[0]
|
plugin_path = plugin.__module__.split('.')[0]
|
||||||
return plugin_path
|
return plugin_path
|
||||||
|
|
||||||
def _try_reload(self, cmd, *args, **kwargs):
|
@classmethod
|
||||||
|
def _try_reload(cls, cmd, *args, **kwargs):
|
||||||
"""Wrapper to try reloading the apps.
|
"""Wrapper to try reloading the apps.
|
||||||
|
|
||||||
Throws an custom error that gets handled by the loading function.
|
Throws an custom error that gets handled by the loading function.
|
||||||
@@ -165,7 +171,8 @@ class AppMixin:
|
|||||||
except Exception as error: # pragma: no cover
|
except Exception as error: # pragma: no cover
|
||||||
handle_error(error)
|
handle_error(error)
|
||||||
|
|
||||||
def _reload_apps(self, force_reload: bool = False, full_reload: bool = False):
|
@classmethod
|
||||||
|
def _reload_apps(cls, force_reload: bool = False, full_reload: bool = False):
|
||||||
"""Internal: reload apps using django internal functions.
|
"""Internal: reload apps using django internal functions.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -174,16 +181,16 @@ class AppMixin:
|
|||||||
"""
|
"""
|
||||||
# If full_reloading is set to true we do not want to set the flag
|
# If full_reloading is set to true we do not want to set the flag
|
||||||
if not full_reload:
|
if not full_reload:
|
||||||
self.is_loading = True # set flag to disable loop reloading
|
cls.is_loading = True # set flag to disable loop reloading
|
||||||
if force_reload:
|
if force_reload:
|
||||||
# we can not use the built in functions as we need to brute force the registry
|
# we can not use the built in functions as we need to brute force the registry
|
||||||
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()
|
||||||
self._try_reload(apps.populate, settings.INSTALLED_APPS)
|
cls._try_reload(apps.populate, settings.INSTALLED_APPS)
|
||||||
else:
|
else:
|
||||||
self._try_reload(apps.set_installed_apps, settings.INSTALLED_APPS)
|
cls._try_reload(apps.set_installed_apps, settings.INSTALLED_APPS)
|
||||||
self.is_loading = False
|
cls.is_loading = False
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@@ -54,7 +54,8 @@ class ScheduleMixin:
|
|||||||
|
|
||||||
self.add_mixin('schedule', 'has_scheduled_tasks', __class__)
|
self.add_mixin('schedule', 'has_scheduled_tasks', __class__)
|
||||||
|
|
||||||
def _activate_mixin(self, plugins, *args, **kwargs):
|
@classmethod
|
||||||
|
def _activate_mixin(cls, registry, plugins, *args, **kwargs):
|
||||||
"""Activate scheudles from plugins with the ScheduleMixin."""
|
"""Activate scheudles from plugins with the ScheduleMixin."""
|
||||||
logger.info('Activating plugin tasks')
|
logger.info('Activating plugin tasks')
|
||||||
|
|
||||||
@@ -97,13 +98,6 @@ class ScheduleMixin:
|
|||||||
# Database might not yet be ready
|
# Database might not yet be ready
|
||||||
logger.warning("activate_integration_schedule failed, database not ready")
|
logger.warning("activate_integration_schedule failed, database not ready")
|
||||||
|
|
||||||
def _deactivate_mixin(self):
|
|
||||||
"""Deactivate ScheduleMixin.
|
|
||||||
|
|
||||||
Currently nothing is done here.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def get_scheduled_tasks(self):
|
def get_scheduled_tasks(self):
|
||||||
"""Returns `SCHEDULED_TASKS` context.
|
"""Returns `SCHEDULED_TASKS` context.
|
||||||
|
|
||||||
|
@@ -19,7 +19,8 @@ class SettingsMixin:
|
|||||||
self.add_mixin('settings', 'has_settings', __class__)
|
self.add_mixin('settings', 'has_settings', __class__)
|
||||||
self.settings = getattr(self, 'SETTINGS', {})
|
self.settings = getattr(self, 'SETTINGS', {})
|
||||||
|
|
||||||
def _activate_mixin(self, plugins, *args, **kwargs):
|
@classmethod
|
||||||
|
def _activate_mixin(cls, registry, plugins, *args, **kwargs):
|
||||||
"""Activate plugin settings.
|
"""Activate plugin settings.
|
||||||
|
|
||||||
Add all defined settings form the plugins to a unified dict in the registry.
|
Add all defined settings form the plugins to a unified dict in the registry.
|
||||||
@@ -27,18 +28,19 @@ class SettingsMixin:
|
|||||||
"""
|
"""
|
||||||
logger.info('Activating plugin settings')
|
logger.info('Activating plugin settings')
|
||||||
|
|
||||||
self.mixins_settings = {}
|
registry.mixins_settings = {}
|
||||||
|
|
||||||
for slug, plugin in plugins:
|
for slug, plugin in plugins:
|
||||||
if plugin.mixin_enabled('settings'):
|
if plugin.mixin_enabled('settings'):
|
||||||
plugin_setting = plugin.settings
|
plugin_setting = plugin.settings
|
||||||
self.mixins_settings[slug] = plugin_setting
|
registry.mixins_settings[slug] = plugin_setting
|
||||||
|
|
||||||
def _deactivate_mixin(self):
|
@classmethod
|
||||||
|
def _deactivate_mixin(cls, registry):
|
||||||
"""Deactivate all plugin settings."""
|
"""Deactivate all plugin settings."""
|
||||||
logger.info('Deactivating plugin settings')
|
logger.info('Deactivating plugin settings')
|
||||||
# clear settings cache
|
# clear settings cache
|
||||||
self.mixins_settings = {}
|
registry.mixins_settings = {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_settings(self):
|
def has_settings(self):
|
||||||
|
@@ -23,10 +23,12 @@ class UrlsMixin:
|
|||||||
self.add_mixin('urls', 'has_urls', __class__)
|
self.add_mixin('urls', 'has_urls', __class__)
|
||||||
self.urls = self.setup_urls()
|
self.urls = self.setup_urls()
|
||||||
|
|
||||||
def _activate_mixin(self, plugins, force_reload=False, full_reload: bool = False):
|
@classmethod
|
||||||
|
def _activate_mixin(cls, registry, plugins, force_reload=False, full_reload: bool = False):
|
||||||
"""Activate UrlsMixin plugins - add custom urls .
|
"""Activate UrlsMixin plugins - add custom urls .
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
registry (PluginRegistry): The registry that should be used
|
||||||
plugins (dict): List of IntegrationPlugins that should be installed
|
plugins (dict): List of IntegrationPlugins that should be installed
|
||||||
force_reload (bool, optional): Only reload base apps. Defaults to False.
|
force_reload (bool, optional): Only reload base apps. Defaults to False.
|
||||||
full_reload (bool, optional): Reload everything - including plugin mechanism. Defaults to False.
|
full_reload (bool, optional): Reload everything - including plugin mechanism. Defaults to False.
|
||||||
@@ -42,7 +44,7 @@ class UrlsMixin:
|
|||||||
# if apps were changed or force loading base apps -> reload
|
# if apps were changed or force loading base apps -> reload
|
||||||
if urls_changed or force_reload or full_reload:
|
if urls_changed or force_reload or full_reload:
|
||||||
# update urls - must be last as models must be registered for creating admin routes
|
# update urls - must be last as models must be registered for creating admin routes
|
||||||
self._update_urls()
|
registry._update_urls()
|
||||||
|
|
||||||
def setup_urls(self):
|
def setup_urls(self):
|
||||||
"""Setup url endpoints for this plugin."""
|
"""Setup url endpoints for this plugin."""
|
||||||
|
@@ -470,7 +470,7 @@ class PluginsRegistry:
|
|||||||
logger.info(f'Found {len(plugins)} active plugins')
|
logger.info(f'Found {len(plugins)} active plugins')
|
||||||
|
|
||||||
for mixin in self.mixin_order:
|
for mixin in self.mixin_order:
|
||||||
mixin._activate_mixin(mixin, plugins, force_reload=force_reload, full_reload=full_reload)
|
mixin._activate_mixin(self, plugins, force_reload=force_reload, full_reload=full_reload)
|
||||||
|
|
||||||
def _deactivate_plugins(self):
|
def _deactivate_plugins(self):
|
||||||
"""Run deactivation functions for all plugins."""
|
"""Run deactivation functions for all plugins."""
|
||||||
|
Reference in New Issue
Block a user