From 4af422f841f69d5afc4a0a3158ce15be3d4bc9c3 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Mon, 13 Feb 2023 21:47:12 +0100 Subject: [PATCH] switch to classmethods for loading --- InvenTree/plugin/base/integration/AppMixin.py | 53 +++++++++++-------- .../plugin/base/integration/ScheduleMixin.py | 10 +--- .../plugin/base/integration/SettingsMixin.py | 12 +++-- .../plugin/base/integration/UrlsMixin.py | 6 ++- InvenTree/plugin/registry.py | 2 +- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/InvenTree/plugin/base/integration/AppMixin.py b/InvenTree/plugin/base/integration/AppMixin.py index b3e4e83f59..de7e0afcf4 100644 --- a/InvenTree/plugin/base/integration/AppMixin.py +++ b/InvenTree/plugin/base/integration/AppMixin.py @@ -25,10 +25,12 @@ class AppMixin: super().__init__() 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. Args: + registry (PluginRegistry): The registry that should be used plugins (dict): List of IntegrationPlugins that should be installed force_reload (bool, optional): Only reload base apps. 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 for _key, plugin in plugins: 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: settings.INSTALLED_APPS += [plugin_path] - self.installed_apps += [plugin_path] + registry.installed_apps += [plugin_path] apps_changed = True # if apps were changed or force loading base apps -> reload if apps_changed or force_reload: # first startup or force loading of base apps -> registry is prob false - if self.apps_loading or force_reload: - self.apps_loading = False - self._reload_apps(force_reload=True, full_reload=full_reload) + if registry.apps_loading or force_reload: + registry.apps_loading = False + cls._reload_apps(force_reload=True, full_reload=full_reload) else: - self._reload_apps(full_reload=full_reload) + cls._reload_apps(full_reload=full_reload) # 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 - self._update_urls() + registry._update_urls() - def _deactivate_mixin(self): + @classmethod + def _deactivate_mixin(cls, registry): """Deactivate AppMixin plugins - some magic required.""" # 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 app_name = plugin_path.split('.')[-1] try: @@ -96,23 +99,24 @@ class AppMixin: apps.all_models.pop(app_name) # remove plugin from installed_apps - self._clean_installed_apps() + registry._clean_installed_apps() # reset load flag and reload apps settings.INTEGRATION_APPS_LOADED = False - self._reload_apps() + cls._reload_apps() # update urls to remove the apps from the site admin - self._update_urls() + registry._update_urls() # region helpers - def _reregister_contrib_apps(self): + @classmethod + def _reregister_contrib_apps(cls): """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. 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: app_name = plugin_path.split('.')[-1] app_config = apps.get_app_config(app_name) @@ -139,7 +143,8 @@ class AppMixin: if model_not_reg and hasattr(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. The input can be eiter: @@ -154,7 +159,8 @@ class AppMixin: plugin_path = plugin.__module__.split('.')[0] return plugin_path - def _try_reload(self, cmd, *args, **kwargs): + @classmethod + def _try_reload(cls, cmd, *args, **kwargs): """Wrapper to try reloading the apps. Throws an custom error that gets handled by the loading function. @@ -165,7 +171,8 @@ class AppMixin: except Exception as error: # pragma: no cover 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. Args: @@ -174,16 +181,16 @@ class AppMixin: """ # If full_reloading is set to true we do not want to set the flag 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: # we can not use the built in functions as we need to brute force the registry apps.app_configs = OrderedDict() apps.apps_ready = apps.models_ready = apps.loading = apps.ready = False apps.clear_cache() - self._try_reload(apps.populate, settings.INSTALLED_APPS) + cls._try_reload(apps.populate, settings.INSTALLED_APPS) else: - self._try_reload(apps.set_installed_apps, settings.INSTALLED_APPS) - self.is_loading = False + cls._try_reload(apps.set_installed_apps, settings.INSTALLED_APPS) + cls.is_loading = False # endregion @property diff --git a/InvenTree/plugin/base/integration/ScheduleMixin.py b/InvenTree/plugin/base/integration/ScheduleMixin.py index 6bbfe936ed..29ece250b2 100644 --- a/InvenTree/plugin/base/integration/ScheduleMixin.py +++ b/InvenTree/plugin/base/integration/ScheduleMixin.py @@ -54,7 +54,8 @@ class ScheduleMixin: 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.""" logger.info('Activating plugin tasks') @@ -97,13 +98,6 @@ class ScheduleMixin: # Database might not yet be 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): """Returns `SCHEDULED_TASKS` context. diff --git a/InvenTree/plugin/base/integration/SettingsMixin.py b/InvenTree/plugin/base/integration/SettingsMixin.py index 35eb9c4169..e76adf2b05 100644 --- a/InvenTree/plugin/base/integration/SettingsMixin.py +++ b/InvenTree/plugin/base/integration/SettingsMixin.py @@ -19,7 +19,8 @@ class SettingsMixin: self.add_mixin('settings', 'has_settings', __class__) self.settings = getattr(self, 'SETTINGS', {}) - def _activate_mixin(self, plugins, *args, **kwargs): + @classmethod + def _activate_mixin(cls, registry, plugins, *args, **kwargs): """Activate plugin settings. 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') - self.mixins_settings = {} + registry.mixins_settings = {} for slug, plugin in plugins: if plugin.mixin_enabled('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.""" logger.info('Deactivating plugin settings') # clear settings cache - self.mixins_settings = {} + registry.mixins_settings = {} @property def has_settings(self): diff --git a/InvenTree/plugin/base/integration/UrlsMixin.py b/InvenTree/plugin/base/integration/UrlsMixin.py index b4c91bc683..67f87455d1 100644 --- a/InvenTree/plugin/base/integration/UrlsMixin.py +++ b/InvenTree/plugin/base/integration/UrlsMixin.py @@ -23,10 +23,12 @@ class UrlsMixin: self.add_mixin('urls', 'has_urls', __class__) 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 . Args: + registry (PluginRegistry): The registry that should be used plugins (dict): List of IntegrationPlugins that should be installed force_reload (bool, optional): Only reload base apps. 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 urls_changed or force_reload or full_reload: # update urls - must be last as models must be registered for creating admin routes - self._update_urls() + registry._update_urls() def setup_urls(self): """Setup url endpoints for this plugin.""" diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index f32a2d64f0..c2f63b9bf0 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -470,7 +470,7 @@ class PluginsRegistry: logger.info(f'Found {len(plugins)} active plugins') 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): """Run deactivation functions for all plugins."""