From 12538c9e83c69caf54a1244c3568f751048b2742 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 17 Nov 2024 11:01:12 +0000 Subject: [PATCH] Revert "Cleanup" This reverts commit a40c85d47d9446cf4181b9865d2fce6a63efba92. --- src/backend/InvenTree/plugin/apps.py | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/backend/InvenTree/plugin/apps.py b/src/backend/InvenTree/plugin/apps.py index 8b481a304b..f0dd345561 100644 --- a/src/backend/InvenTree/plugin/apps.py +++ b/src/backend/InvenTree/plugin/apps.py @@ -5,11 +5,15 @@ The main code for plugin special sauce is in the plugin registry in `InvenTree/p """ import logging +import pathlib +import sys from django.apps import AppConfig +from django.conf import settings from maintenance_mode.core import set_maintenance_mode +from InvenTree.config import get_plugin_dir from InvenTree.ready import canAppAccessDatabase, isInMainThread, isInWorkerThread from plugin import registry @@ -23,6 +27,8 @@ class PluginAppConfig(AppConfig): def ready(self): """The ready method is extended to initialize plugins.""" + self.update_python_path() + if not isInMainThread() and not isInWorkerThread(): return @@ -42,3 +48,29 @@ class PluginAppConfig(AppConfig): # drop out of maintenance # makes sure we did not have an error in reloading and maintenance is still active set_maintenance_mode(False) + + def update_python_path(self): + """Update the PYTHONPATH to include the plugin directory.""" + if not settings.PLUGINS_ENABLED: + # Custom plugins disabled - no need to update PYTHONPATH + logger.info('Custom plugins are not enabled') + return + + plugin_dir = get_plugin_dir() + + if not plugin_dir: + logger.warning('Plugins directory not specified') + return + + if plugin_dir := get_plugin_dir(): + path = pathlib.Path(plugin_dir) + + if not path.exists(): + try: + path.mkdir(parents=True, exist_ok=True) + except Exception: + logger.error(f'Failed to create plugin directory: {plugin_dir}') + return + + if plugin_dir not in sys.path: + sys.path.append(plugin_dir)