From 8aec055e6c5166e173197ead17254d501b03e5ad Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 11 Jan 2022 13:39:47 +1100 Subject: [PATCH] Only load plugins if PLUGINS_ENABLED is true - Hide plugin settings - Add plugin support status to "stats" dialog --- .../part/templatetags/inventree_extras.py | 8 ++++++ InvenTree/plugin/apps.py | 25 +++++++++++++------ InvenTree/plugin/events.py | 4 +++ InvenTree/plugin/registry.py | 19 +++++++++++--- .../InvenTree/settings/settings.html | 6 ++++- .../templates/InvenTree/settings/sidebar.html | 9 ++++--- InvenTree/templates/stats.html | 12 +++++++++ 7 files changed, 68 insertions(+), 15 deletions(-) diff --git a/InvenTree/part/templatetags/inventree_extras.py b/InvenTree/part/templatetags/inventree_extras.py index 9c0b0e691f..fb3a142a9a 100644 --- a/InvenTree/part/templatetags/inventree_extras.py +++ b/InvenTree/part/templatetags/inventree_extras.py @@ -7,6 +7,7 @@ over and above the built-in Django tags. import os import sys +import django from django.utils.html import format_html from django.utils.translation import ugettext_lazy as _ @@ -107,6 +108,13 @@ def inventree_docker_mode(*args, **kwargs): return djangosettings.DOCKER +@register.simple_tag() +def plugins_enabled(*args, **kwargs): + """ Return True if plugins are enabled for the server instance """ + + return djangosettings.PLUGINS_ENABLED + + @register.simple_tag() def inventree_db_engine(*args, **kwargs): """ Return the InvenTree database backend e.g. 'postgresql' """ diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index cca9dee91c..47b5e9f024 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -1,21 +1,32 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import logging + from django.apps import AppConfig +from django.conf import settings + from maintenance_mode.core import set_maintenance_mode from plugin import plugin_registry +logger = logging.getLogger('inventree') + + class PluginAppConfig(AppConfig): name = 'plugin' def ready(self): - if not plugin_registry.is_loading: - # this is the first startup - plugin_registry.collect_plugins() - plugin_registry.load_plugins() - # drop out of maintenance - # makes sure we did not have an error in reloading and maintenance is still active - set_maintenance_mode(False) + if settings.PLUGINS_ENABLED: + logger.info('Loading InvenTree plugins') + + if not plugin_registry.is_loading: + # this is the first startup + plugin_registry.collect_plugins() + plugin_registry.load_plugins() + + # drop out of maintenance + # makes sure we did not have an error in reloading and maintenance is still active + set_maintenance_mode(False) diff --git a/InvenTree/plugin/events.py b/InvenTree/plugin/events.py index f777621a45..bd1226626e 100644 --- a/InvenTree/plugin/events.py +++ b/InvenTree/plugin/events.py @@ -31,6 +31,10 @@ def trigger_event(event, *args, **kwargs): and the worker will respond to it later on. """ + if not settings.PLUGINS_ENABLED: + # Do nothing if plugins are not enabled + return + if not canAppAccessDatabase(): logger.debug(f"Ignoring triggered event '{event}' - database not ready") return diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 8d04620120..448365b338 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -66,6 +66,10 @@ class PluginsRegistry: Load and activate all IntegrationPlugins """ + if not settings.PLUGINS_ENABLED: + # Plugins not enabled, do nothing + return + from plugin.helpers import log_plugin_error logger.info('Start loading plugins') @@ -75,15 +79,16 @@ class PluginsRegistry: if not _maintenance: set_maintenance_mode(True) - registered_sucessfull = False + registered_successful = False blocked_plugin = None retry_counter = settings.PLUGIN_RETRY - while not registered_sucessfull: + + while not registered_successful: try: # We are using the db so for migrations etc we need to try this block self._init_plugins(blocked_plugin) self._activate_plugins() - registered_sucessfull = True + registered_successful = True except (OperationalError, ProgrammingError): # Exception if the database has not been migrated yet logger.info('Database not accessible while loading plugins') @@ -122,6 +127,10 @@ class PluginsRegistry: Unload and deactivate all IntegrationPlugins """ + if not settings.PLUGINS_ENABLED: + # Plugins not enabled, do nothing + return + logger.info('Start unloading plugins') # Set maintanace mode @@ -162,6 +171,10 @@ class PluginsRegistry: Collect integration plugins from all possible ways of loading """ + if not settings.PLUGINS_ENABLED: + # Plugins not enabled, do nothing + return + self.plugin_modules = [] # clear # Collect plugins from paths diff --git a/InvenTree/templates/InvenTree/settings/settings.html b/InvenTree/templates/InvenTree/settings/settings.html index 9b3d2d21de..e559f6ff7f 100644 --- a/InvenTree/templates/InvenTree/settings/settings.html +++ b/InvenTree/templates/InvenTree/settings/settings.html @@ -39,14 +39,16 @@ {% include "InvenTree/settings/build.html" %} {% include "InvenTree/settings/po.html" %} {% include "InvenTree/settings/so.html" %} -{% include "InvenTree/settings/plugin.html" %} +{% if plugins_enabled %} +{% include "InvenTree/settings/plugin.html" %} {% plugin_list as pl_list %} {% for plugin_key, plugin in pl_list.items %} {% if plugin.registered_mixins %} {% include "InvenTree/settings/plugin_settings.html" %} {% endif %} {% endfor %} +{% endif %} {% endif %} @@ -333,9 +335,11 @@ $("#import-part").click(function() { launchModalForm("{% url 'api-part-import' %}?reset", {}); }); +{% if plugins_enabled %} $("#install-plugin").click(function() { installPlugin(); }); +{% endif %} enableSidebar('settings'); diff --git a/InvenTree/templates/InvenTree/settings/sidebar.html b/InvenTree/templates/InvenTree/settings/sidebar.html index 24f62f1e1c..281555ff5c 100644 --- a/InvenTree/templates/InvenTree/settings/sidebar.html +++ b/InvenTree/templates/InvenTree/settings/sidebar.html @@ -47,15 +47,16 @@ {% trans "Sales Orders" as text %} {% include "sidebar_item.html" with label='sales-order' text=text icon="fa-truck" %} +{% if plugins_enabled %} {% include "sidebar_header.html" with text="Plugin Settings" %} - {% include "sidebar_item.html" with label='plugin' text="Plugins" icon="fa-plug" %} {% plugin_list as pl_list %} {% for plugin_key, plugin in pl_list.items %} - {% if plugin.registered_mixins %} - {% include "sidebar_item.html" with label='plugin-'|add:plugin_key text=plugin.human_name %} - {% endif %} +{% if plugin.registered_mixins %} +{% include "sidebar_item.html" with label='plugin-'|add:plugin_key text=plugin.human_name %} +{% endif %} {% endfor %} +{% endif %} {% endif %} \ No newline at end of file diff --git a/InvenTree/templates/stats.html b/InvenTree/templates/stats.html index cf3d225cd2..7a22f88023 100644 --- a/InvenTree/templates/stats.html +++ b/InvenTree/templates/stats.html @@ -34,6 +34,18 @@ {% trans "Server is deployed using docker" %} {% endif %} + + + {% trans "Plugin Support" %} + + {% plugins_enabled as p_en %} + {% if p_en %} + {% trans "Plugin support enabled" %} + {% else %} + {% trans "Plugin support disabled" %} + {% endif %} + + {% if user.is_staff %}