rename global settings objects

This commit is contained in:
Matthias 2021-11-14 00:24:32 +01:00
parent 11c3ac8bf8
commit b10492f088
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
11 changed files with 58 additions and 58 deletions

View File

@ -863,5 +863,5 @@ if DEBUG or TESTING:
PLUGINS = []
INTEGRATION_PLUGINS = {}
INTEGRATION_PLUGINS_INACTIVE = {}
INTEGRATION_PLUGIN_SETTING = {}
INTEGRATION_PLUGIN_GLOBALSETTING = {}
INTEGRATION_APPS_LOADED = False # Marks if apps were reloaded yet

View File

@ -979,9 +979,9 @@ class InvenTreeSetting(BaseInvenTreeSetting):
'validator': bool,
'requires_restart': True,
},
'ENABLE_PLUGINS_SETTING': {
'name': _('Enable setting integration'),
'description': _('Enable plugins to integrate into inventree settings'),
'ENABLE_PLUGINS_GLOBALSETTING': {
'name': _('Enable global setting integration'),
'description': _('Enable plugins to integrate into inventree global settings'),
'default': False,
'validator': bool,
'requires_restart': True,

View File

@ -92,20 +92,20 @@ class PluginAppConfig(AppConfig):
logger.info(f'Found {len(plugins)} active plugins')
# if plugin settings are enabled enhance the settings
self.activate_integration_settings(plugins)
self.activate_integration_globalsettings(plugins)
# if plugin apps are enabled
self.activate_integration_app(plugins)
def activate_integration_settings(self, plugins):
def activate_integration_globalsettings(self, plugins):
from common.models import InvenTreeSetting
if settings.PLUGIN_TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_SETTING'):
logger.info('Registering IntegrationPlugin settings')
if settings.PLUGIN_TESTING or InvenTreeSetting.get_setting('ENABLE_PLUGINS_GLOBALSETTING'):
logger.info('Registering IntegrationPlugin global settings')
for slug, plugin in plugins:
if plugin.mixin_enabled('settings'):
plugin_setting = plugin.settingspatterns
settings.INTEGRATION_PLUGIN_SETTING[slug] = plugin_setting
if plugin.mixin_enabled('globalsettings'):
plugin_setting = plugin.globalsettingspatterns
settings.INTEGRATION_PLUGIN_GLOBALSETTING[slug] = plugin_setting
# Add to settings dir
InvenTreeSetting.GLOBAL_SETTINGS.update(plugin_setting)

View File

@ -57,53 +57,53 @@ class MixinBase:
return mixins
class SettingsMixin:
"""Mixin that enables settings for the plugin"""
class GlobalSettingsMixin:
"""Mixin that enables global settings for the plugin"""
class Meta:
"""meta options for this mixin"""
MIXIN_NAME = 'Settings'
MIXIN_NAME = 'Global settings'
def __init__(self):
super().__init__()
self.add_mixin('settings', 'has_settings', __class__)
self.settings = self.setup_settings()
self.add_mixin('globalsettings', 'has_globalsettings', __class__)
self.globalsettings = self.setup_globalsettings()
def setup_settings(self):
def setup_globalsettings(self):
"""
setup settings for this plugin
setup global settings for this plugin
"""
return getattr(self, 'SETTINGS', None)
return getattr(self, 'GLOBALSETTINGS', None)
@property
def has_settings(self):
def has_globalsettings(self):
"""
does this plugin use custom settings
does this plugin use custom global settings
"""
return bool(self.settings)
return bool(self.globalsettings)
@property
def settingspatterns(self):
def globalsettingspatterns(self):
"""
get patterns for InvenTreeSetting defintion
"""
if self.has_settings:
return {f'PLUGIN_{self.slug.upper()}_{key}': value for key, value in self.settings.items()}
if self.has_globalsettings:
return {f'PLUGIN_{self.slug.upper()}_{key}': value for key, value in self.globalsettings.items()}
return None
def _setting_name(self, key):
def _globalsetting_name(self, key):
"""get global name of setting"""
return f'PLUGIN_{self.slug.upper()}_{key}'
def get_setting(self, key):
def get_globalsetting(self, key):
"""
get plugin setting by key
get plugin global setting by key
"""
from common.models import InvenTreeSetting
return InvenTreeSetting.get_setting(self._setting_name(key))
return InvenTreeSetting.get_setting(self._globalsetting_name(key))
def set_setting(self, key, value, user):
def set_globalsetting(self, key, value, user):
"""
set plugin setting by key
set plugin global setting by key
"""
from common.models import InvenTreeSetting
return InvenTreeSetting.set_setting(self._setting_name(key), value, user)

View File

@ -1,12 +1,12 @@
"""sample implementations for IntegrationPlugin"""
from plugin.integration import AppMixin, SettingsMixin, UrlsMixin, NavigationMixin, IntegrationPluginBase
from plugin.integration import AppMixin, GlobalSettingsMixin, UrlsMixin, NavigationMixin, IntegrationPluginBase
from django.http import HttpResponse
from django.utils.translation import ugettext_lazy as _
from django.conf.urls import url, include
class SampleIntegrationPlugin(AppMixin, SettingsMixin, UrlsMixin, NavigationMixin, IntegrationPluginBase):
class SampleIntegrationPlugin(AppMixin, GlobalSettingsMixin, UrlsMixin, NavigationMixin, IntegrationPluginBase):
"""
An full integration plugin
"""

View File

@ -25,9 +25,9 @@ def inactive_plugin_list(*args, **kwargs):
@register.simple_tag()
def plugin_settings(plugin, *args, **kwargs):
""" Return a list of all settings for a plugin """
return djangosettings.INTEGRATION_PLUGIN_SETTING.get(plugin)
def plugin_globalsettings(plugin, *args, **kwargs):
""" Return a list of all global settings for a plugin """
return djangosettings.INTEGRATION_PLUGIN_GLOBALSETTING.get(plugin)
@register.simple_tag()

View File

@ -7,7 +7,7 @@ from django.contrib.auth import get_user_model
from datetime import datetime
from plugin.integration import AppMixin, IntegrationPluginBase, SettingsMixin, UrlsMixin, NavigationMixin
from plugin.integration import AppMixin, IntegrationPluginBase, GlobalSettingsMixin, UrlsMixin, NavigationMixin
class BaseMixinDefinition:
@ -18,19 +18,19 @@ class BaseMixinDefinition:
self.assertEqual(self.mixin.registered_mixins[0]['human_name'], self.MIXIN_HUMAN_NAME)
class SettingsMixinTest(BaseMixinDefinition, TestCase):
MIXIN_HUMAN_NAME = 'Settings'
MIXIN_NAME = 'settings'
MIXIN_ENABLE_CHECK = 'has_settings'
class GlobalSettingsMixinTest(BaseMixinDefinition, TestCase):
MIXIN_HUMAN_NAME = 'Global settings'
MIXIN_NAME = 'globalsettings'
MIXIN_ENABLE_CHECK = 'has_globalsettings'
TEST_SETTINGS = {'SETTING1': {'default': '123', }}
def setUp(self):
class SettingsCls(SettingsMixin, IntegrationPluginBase):
class SettingsCls(GlobalSettingsMixin, IntegrationPluginBase):
SETTINGS = self.TEST_SETTINGS
self.mixin = SettingsCls()
class NoSettingsCls(SettingsMixin, IntegrationPluginBase):
class NoSettingsCls(GlobalSettingsMixin, IntegrationPluginBase):
pass
self.mixin_nothing = NoSettingsCls()
@ -40,25 +40,25 @@ class SettingsMixinTest(BaseMixinDefinition, TestCase):
def test_function(self):
# settings variable
self.assertEqual(self.mixin.settings, self.TEST_SETTINGS)
self.assertEqual(self.mixin.globalsettings, self.TEST_SETTINGS)
# settings pattern
target_pattern = {f'PLUGIN_{self.mixin.slug.upper()}_{key}': value for key, value in self.mixin.settings.items()}
self.assertEqual(self.mixin.settingspatterns, target_pattern)
self.assertEqual(self.mixin.globalsettingspatterns, target_pattern)
# no settings
self.assertIsNone(self.mixin_nothing.settings)
self.assertIsNone(self.mixin_nothing.settingspatterns)
self.assertIsNone(self.mixin_nothing.globalsettings)
self.assertIsNone(self.mixin_nothing.globalsettingspatterns)
# calling settings
# not existing
self.assertEqual(self.mixin.get_setting('ABCD'), '')
self.assertEqual(self.mixin_nothing.get_setting('ABCD'), '')
self.assertEqual(self.mixin.get_globalsetting('ABCD'), '')
self.assertEqual(self.mixin_nothing.get_globalsetting('ABCD'), '')
# right setting
self.mixin.set_setting('SETTING1', '12345', self.test_user)
self.assertEqual(self.mixin.get_setting('SETTING1'), '12345')
self.mixin.set_globalsetting('SETTING1', '12345', self.test_user)
self.assertEqual(self.mixin.get_globalsetting('SETTING1'), '12345')
# no setting
self.assertEqual(self.mixin_nothing.get_setting(''), '')
self.assertEqual(self.mixin_nothing.get_globalsetting(''), '')
class UrlsMixinTest(BaseMixinDefinition, TestCase):

View File

@ -59,9 +59,9 @@ class PluginTagTests(TestCase):
"""test that all plugins are listed"""
self.assertEqual(plugin_tags.plugin_list(), settings.INTEGRATION_PLUGINS)
def test_tag_plugin_settings(self):
def test_tag_plugin_globalsettings(self):
"""check all plugins are listed"""
self.assertEqual(plugin_tags.plugin_settings(self.sample), settings.INTEGRATION_PLUGIN_SETTING.get(self.sample))
self.assertEqual(plugin_tags.plugin_globalsettings(self.sample), settings.INTEGRATION_PLUGIN_GLOBALSETTING.get(self.sample))
def test_tag_mixin_enabled(self):
"""check that mixin enabled functions work"""

View File

@ -2,7 +2,7 @@
{% load plugin_extras %}
<h4>{% trans "Settings" %}</h4>
{% plugin_settings plugin_key as plugin_settings %}
{% plugin_globalsettings plugin_key as plugin_settings %}
<table class='table table-striped table-condensed'>
<tbody>

View File

@ -20,7 +20,7 @@
<tbody>
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_URL" %}
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_NAVIGATION" %}
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_SETTING"%}
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_GLOBALSETTING"%}
{% include "InvenTree/settings/setting.html" with key="ENABLE_PLUGINS_APP"%}
</tbody>
</table>

View File

@ -120,8 +120,8 @@
</div>
</div>
{% mixin_enabled plugin 'settings' as settings %}
{% if settings %}
{% mixin_enabled plugin 'globalsettings' as globalsettings %}
{% if globalsettings %}
{% include 'InvenTree/settings/mixins/settings.html' %}
{% endif %}