From d4a8b7d0217adf1fbad11c379817b080c06ecec7 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Tue, 18 Apr 2023 23:38:43 +0200 Subject: [PATCH] move app mixin deactivation --- InvenTree/plugin/base/integration/AppMixin.py | 11 ++-- .../plugin/base/integration/SettingsMixin.py | 2 +- InvenTree/plugin/registry.py | 51 +------------------ 3 files changed, 10 insertions(+), 54 deletions(-) diff --git a/InvenTree/plugin/base/integration/AppMixin.py b/InvenTree/plugin/base/integration/AppMixin.py index bee971f16e..a07b588b34 100644 --- a/InvenTree/plugin/base/integration/AppMixin.py +++ b/InvenTree/plugin/base/integration/AppMixin.py @@ -62,8 +62,13 @@ class AppMixin: registry._update_urls() @classmethod - def _deactivate_mixin(cls, registry): - """Deactivate AppMixin plugins - some magic required.""" + def _deactivate_mixin(cls, registry, force_reload: bool = False): + """Deactivate AppMixin plugins - some magic required. + + Args: + registry (PluginRegistry): The registry that should be used + force_reload (bool, optional): Also reload base apps. Defaults to False. + """ # unregister models from admin for plugin_path in registry.installed_apps: models = [] # the modelrefs need to be collected as poping an item in a iter is not welcomed @@ -100,7 +105,7 @@ class AppMixin: # reset load flag and reload apps settings.INTEGRATION_APPS_LOADED = False - cls._reload_apps() + cls._reload_apps(force_reload=force_reload) # update urls to remove the apps from the site admin registry._update_urls() diff --git a/InvenTree/plugin/base/integration/SettingsMixin.py b/InvenTree/plugin/base/integration/SettingsMixin.py index e76adf2b05..d7937e9087 100644 --- a/InvenTree/plugin/base/integration/SettingsMixin.py +++ b/InvenTree/plugin/base/integration/SettingsMixin.py @@ -36,7 +36,7 @@ class SettingsMixin: registry.mixins_settings[slug] = plugin_setting @classmethod - def _deactivate_mixin(cls, registry): + def _deactivate_mixin(cls, registry, **kwargs): """Deactivate all plugin settings.""" logger.info('Deactivating plugin settings') # clear settings cache diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 8639e687f5..1e86f1ff9b 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -492,11 +492,9 @@ class PluginsRegistry: """ for mixin in self.mixin_order: if hasattr(mixin, '_deactivate_mixin'): - mixin._deactivate_mixin(self) + mixin._deactivate_mixin(self, force_reload=force_reload) logger.info('Done deactivating') - - # self.deactivate_plugin_app(force_reload=force_reload) # endregion # region mixin specific loading ... @@ -531,53 +529,6 @@ class PluginsRegistry: self._try_reload(apps.set_installed_apps, settings.INSTALLED_APPS) self.is_loading = False - def deactivate_plugin_app(self, force_reload: bool = False): - """Deactivate AppMixin plugins - some magic required. - - Args: - force_reload (bool, optional): Also reload base apps. Defaults to False. - """ - # unregister models from admin - for plugin_path in self.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: - app_config = apps.get_app_config(app_name) - - # check all models - for model in app_config.get_models(): - # remove model from admin site - try: - admin.site.unregister(model) - except Exception: # pragma: no cover - pass - models += [model._meta.model_name] - except LookupError: # pragma: no cover - # if an error occurs the app was never loaded right -> so nothing to do anymore - logger.debug(f'{app_name} App was not found during deregistering') - break - - # unregister the models (yes, models are just kept in multilevel dicts) - for model in models: - # remove model from general registry - apps.all_models[plugin_path].pop(model) - - # clear the registry for that app - # so that the import trick will work on reloading the same plugin - # -> the registry is kept for the whole lifecycle - if models and app_name in apps.all_models: - apps.all_models.pop(app_name) - - # remove plugin from installed_apps - self._clean_installed_apps() - - # reset load flag and reload apps - settings.INTEGRATION_APPS_LOADED = False - self._reload_apps(force_reload=force_reload) - - # update urls to remove the apps from the site admin - self._update_urls() - def _clean_installed_apps(self): for plugin in self.installed_apps: if plugin in settings.INSTALLED_APPS: