From a5df02ee382698178c25f0046355894053f8fa25 Mon Sep 17 00:00:00 2001 From: John Russo Date: Sun, 13 Apr 2025 21:13:17 +0900 Subject: [PATCH] Modified enable_plugin() and disable_plugin() methods to ensure the corresponding plugin methods are fired only on transition of the database enabled flag state. This prevents unnecessary firings and seems more in line with the semantics of the app, namely it is enabled and sticks, even between sessions, or it is disabled and so sticks. This is more efficient for plugins that need to configure settings or add or remove files to the app instance. If there is in-memory state that must persist, this should be saved in some persistent storage (like a file) and reloaded on __init__ (although that is another story). --- app/plugins/functions.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/plugins/functions.py b/app/plugins/functions.py index 61811d9d..a7bfcdb5 100644 --- a/app/plugins/functions.py +++ b/app/plugins/functions.py @@ -325,14 +325,33 @@ def get_dynamic_script_handler(script_path, callback=None, **kwargs): return handleRequest def enable_plugin(plugin_name): + db_plugin = Plugin.objects.get(name=plugin_name) # Use name, not pk, per your schema + if db_plugin.enabled: + logger.info(f"Plugin {plugin_name} already enabled in DB—skipping enable") + return get_plugin_by_name(plugin_name, only_active=False) p = get_plugin_by_name(plugin_name, only_active=False) p.register() - Plugin.objects.get(pk=plugin_name).enable() + try: + if hasattr(p, 'enable'): + p.enable() + except Exception as e: + logger.warning(f"Plugin: {plugin_name} enable unsuccessful: {str(e)}") + raise # Propagate error to UI + db_plugin.enable() return p def disable_plugin(plugin_name): + db_plugin = Plugin.objects.get(name=plugin_name) + if not db_plugin.enabled: + logger.info(f"Plugin {plugin_name} already disabled in DB—skipping disable") + return get_plugin_by_name(plugin_name, only_active=False) p = get_plugin_by_name(plugin_name, only_active=False) - Plugin.objects.get(pk=plugin_name).disable() + try: + if hasattr(p, 'disable'): + p.disable() + except: + logger.warning("Plugin: "+plugin_name+" disable unsuccessful.") + db_plugin.disable() return p def delete_plugin(plugin_name):