Only load plugins if PLUGINS_ENABLED is true

- Hide plugin settings
- Add plugin support status to "stats" dialog
This commit is contained in:
Oliver 2022-01-11 13:39:47 +11:00
parent 31df4eae83
commit 8aec055e6c
7 changed files with 68 additions and 15 deletions

View File

@ -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' """

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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');

View File

@ -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 %}

View File

@ -34,6 +34,18 @@
<td>{% trans "Server is deployed using docker" %}</td>
</tr>
{% endif %}
<tr>
<td><span class='fas fa-plug'></span></td>
<td>{% trans "Plugin Support" %}</td>
<td>
{% plugins_enabled as p_en %}
{% if p_en %}
<span class='badge rounded-pill bg-success'>{% trans "Plugin support enabled" %}</span>
{% else %}
<span class='badge rounded-pill bg-warning'>{% trans "Plugin support disabled" %}</span>
{% endif %}
</td>
</tr>
{% if user.is_staff %}
<tr>
<td><span class='fas fa-server'></span></td>