diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index 4278ed56c8..52e04e6cfc 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -29,56 +29,28 @@ class PluginAppConfig(AppConfig): try: # we are using the db from here - so for migrations etc we need to try this block self.init_plugins() - self.activate_plugins() + self.activate_integration() except (OperationalError, ProgrammingError): # Exception if the database has not been migrated yet pass - def activate_plugins(self): - """fullfill integrations for all activated plugins""" - from common.models import InvenTreeSetting + def collect_plugins(self): + """collect integration plugins from all possible ways of loading""" + # Collect plugins from paths + for plugin in settings.PLUGIN_DIRS: + modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase, True) + if modules: + [settings.PLUGINS.append(item) for item in modules] - # activate integrations - plugins = settings.INTEGRATION_PLUGINS.items() - logger.info(f'Found {len(plugins)} active plugins') + # Collect plugins from setup entry points + for entry in metadata.entry_points().get('inventree_plugins', []): + plugin = entry.load() + plugin.is_package = True + settings.PLUGINS.append(plugin) - # if plugin settings are enabled enhance the settings - if settings.TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'): - logger.info('Registering IntegrationPlugin settings') - for slug, plugin in plugins: - if plugin.mixin_enabled('settings'): - plugin_setting = plugin.settingspatterns - settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting - - # Add to settings dir - InvenTreeSetting.GLOBAL_SETTINGS.update(plugin_setting) - - # if plugin apps are enabled - if settings.TESTING or ((not settings.INTEGRATION_APPS_LOADED) and InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP')): - logger.info('Registering IntegrationPlugin apps') - 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 plugins: - if plugin.mixin_enabled('app'): - try: - # for local path plugins - plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(settings.BASE_DIR).parts) - except ValueError: - # plugin is shipped as package - plugin_path = plugin.PLUGIN_NAME - if plugin_path not in settings.INSTALLED_APPS: - 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) + # Log found plugins + logger.info(f'Found {len(settings.PLUGINS)} plugins!') + logger.info(", ".join([a.__module__ for a in settings.PLUGINS])) def init_plugins(self): """initialise all found plugins""" @@ -110,20 +82,56 @@ class PluginAppConfig(AppConfig): # save for later reference settings.INTEGRATION_PLUGINS_INACTIVE[plug_key] = plugin_db_setting - def collect_plugins(self): - """collect integration plugins from all possible ways of loading""" - # Collect plugins from paths - for plugin in settings.PLUGIN_DIRS: - modules = inventree_plugins.get_plugins(importlib.import_module(plugin), IntegrationPluginBase, True) - if modules: - [settings.PLUGINS.append(item) for item in modules] + def activate_integration(self): + """fullfill integrations for all activated plugins""" + # activate integrations + plugins = settings.INTEGRATION_PLUGINS.items() + logger.info(f'Found {len(plugins)} active plugins') - # Collect plugins from setup entry points - for entry in metadata.entry_points().get('inventree_plugins', []): - plugin = entry.load() - plugin.is_package = True - settings.PLUGINS.append(plugin) + # if plugin settings are enabled enhance the settings + self.activate_integration_settings(plugins) - # Log found plugins - logger.info(f'Found {len(settings.PLUGINS)} plugins!') - logger.info(", ".join([a.__module__ for a in settings.PLUGINS])) + # if plugin apps are enabled + self.activate_integration_app(plugins) + + def activate_integration_settings(self, plugins): + from common.models import InvenTreeSetting + + if settings.TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'): + logger.info('Registering IntegrationPlugin settings') + for slug, plugin in plugins: + if plugin.mixin_enabled('settings'): + plugin_setting = plugin.settingspatterns + settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting + + # Add to settings dir + InvenTreeSetting.GLOBAL_SETTINGS.update(plugin_setting) + + def activate_integration_app(self, plugins): + from common.models import InvenTreeSetting + + if settings.TESTING or ((not settings.INTEGRATION_APPS_LOADED) and InvenTreeSetting.get_setting('ENABLE_PLUGINS_APP')): + logger.info('Registering IntegrationPlugin apps') + 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 plugins: + if plugin.mixin_enabled('app'): + try: + # for local path plugins + plugin_path = '.'.join(pathlib.Path(plugin.path).relative_to(settings.BASE_DIR).parts) + except ValueError: + # plugin is shipped as package + plugin_path = plugin.PLUGIN_NAME + if plugin_path not in settings.INSTALLED_APPS: + 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)