2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 13:05:42 +00:00

Allow loading of "builtin" plugins, even if "plugins" are not explicitly loaded

This commit is contained in:
Oliver Walters
2022-11-01 23:14:50 +11:00
parent db45b6f9dc
commit 058587d86c
3 changed files with 96 additions and 83 deletions

View File

@ -7,7 +7,6 @@ The main code for plugin special sauce is in the plugin registry in `InvenTree/p
import logging
from django.apps import AppConfig
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from maintenance_mode.core import set_maintenance_mode
@ -26,7 +25,6 @@ class PluginAppConfig(AppConfig):
def ready(self):
"""The ready method is extended to initialize plugins."""
if settings.PLUGINS_ENABLED:
if not canAppAccessDatabase(allow_test=True, allow_plugins=True):
logger.info("Skipping plugin loading sequence") # pragma: no cover
else:
@ -52,8 +50,9 @@ class PluginAppConfig(AppConfig):
# check git version
registry.git_is_modern = check_git_version()
if not registry.git_is_modern: # pragma: no cover # simulating old git seems not worth it for coverage
log_error(_('Your enviroment has an outdated git version. This prevents InvenTree from loading plugin details.'), 'load')
log_error(_('Your environment has an outdated git version. This prevents InvenTree from loading plugin details.'), 'load')
else:
logger.info("Plugins not enabled - skipping loading sequence") # pragma: no cover

View File

@ -106,6 +106,11 @@ class MetaBase:
def is_active(self):
"""Return True if this plugin is currently active."""
# Builtin plugins are always considered "active"
if self.is_builtin():
return True
cfg = self.plugin_config()
if cfg:
@ -300,6 +305,16 @@ class InvenTreePlugin(VersionMixin, MixinBase, MetaBase):
"""Is this plugin part of the samples?"""
return self.check_is_sample()
@classmethod
def check_is_builtin(cls) -> bool:
"""Determine if a particular plugin class is a 'builtin' plugin"""
return str(cls.check_package_path()).startswith('plugin/builtin')
@property
def is_builtin(self) -> bool:
"""Is this plugin is builtin"""
return self.check_is_builtin()
@classmethod
def check_package_path(cls):
"""Path to the plugin."""

View File

@ -108,9 +108,6 @@ class PluginsRegistry:
Args:
full_reload (bool, optional): Reload everything - including plugin mechanism. Defaults to False.
"""
if not settings.PLUGINS_ENABLED:
# Plugins not enabled, do nothing
return # pragma: no cover
logger.info('Start loading plugins')
@ -167,9 +164,6 @@ class PluginsRegistry:
def unload_plugins(self):
"""Unload and deactivate all IntegrationPlugins."""
if not settings.PLUGINS_ENABLED:
# Plugins not enabled, do nothing
return # pragma: no cover
logger.info('Start unloading plugins')
@ -187,6 +181,7 @@ class PluginsRegistry:
# remove maintenance
if not _maintenance:
set_maintenance_mode(False) # pragma: no cover
logger.info('Finished unloading plugins')
def reload_plugins(self, full_reload: bool = False):
@ -210,8 +205,12 @@ class PluginsRegistry:
def plugin_dirs(self):
"""Construct a list of directories from where plugins can be loaded"""
# Builtin plugins are *always* loaded
dirs = ['plugin.builtin', ]
if settings.PLUGINS_ENABLED:
# Any 'external' plugins are only loaded if PLUGINS_ENABLED is set to True
if settings.TESTING or settings.DEBUG:
# If in TEST or DEBUG mode, load plugins from the 'samples' directory
dirs.append('plugin.samples')
@ -263,9 +262,6 @@ class PluginsRegistry:
def collect_plugins(self):
"""Collect plugins from all possible ways of loading. Returned as list."""
if not settings.PLUGINS_ENABLED:
# Plugins not enabled, do nothing
return # pragma: no cover
collected_plugins = []
@ -293,6 +289,9 @@ class PluginsRegistry:
if modules:
[collected_plugins.append(item) for item in modules]
# From this point any plugins are considered "external" and only loaded if plugins are explicitly enabled
if settings.PLUGINS_ENABLED:
# Check if not running in testing mode and apps should be loaded from hooks
if (not settings.PLUGIN_TESTING) or (settings.PLUGIN_TESTING and settings.PLUGIN_TESTING_SETUP):
# Collect plugins from setup entry points