mirror of
				https://github.com/netbox-community/netbox.git
				synced 2024-05-10 07:54:54 +00:00 
			
		
		
		
	Closes #10543: Introduce get_plugin_config() utility function
This commit is contained in:
		@@ -117,11 +117,11 @@ NetBox looks for the `config` variable within a plugin's `__init__.py` to load i
 | 
			
		||||
All required settings must be configured by the user. If a configuration parameter is listed in both `required_settings` and `default_settings`, the default setting will be ignored.
 | 
			
		||||
 | 
			
		||||
!!! tip "Accessing Config Parameters"
 | 
			
		||||
    Plugin configuration parameters can be accessed in `settings.PLUGINS_CONFIG`, mapped by plugin name. For example:
 | 
			
		||||
    Plugin configuration parameters can be accessed using the `get_plugin_config()` function. For example:
 | 
			
		||||
    
 | 
			
		||||
    ```python
 | 
			
		||||
    from django.conf import settings
 | 
			
		||||
    settings.PLUGINS_CONFIG['myplugin']['verbose_name']
 | 
			
		||||
    from extras.plugins import get_plugin_config
 | 
			
		||||
    get_plugin_config('my_plugin', 'verbose_name')
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
#### Important Notes About `django_apps`
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
import collections
 | 
			
		||||
 | 
			
		||||
from django.apps import AppConfig
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
from django.core.exceptions import ImproperlyConfigured
 | 
			
		||||
from django.utils.module_loading import import_string
 | 
			
		||||
from packaging import version
 | 
			
		||||
@@ -140,3 +141,23 @@ class PluginConfig(AppConfig):
 | 
			
		||||
        for setting, value in cls.default_settings.items():
 | 
			
		||||
            if setting not in user_config:
 | 
			
		||||
                user_config[setting] = value
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#
 | 
			
		||||
# Utilities
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
def get_plugin_config(plugin_name, parameter, default=None):
 | 
			
		||||
    """
 | 
			
		||||
    Return the value of the specified plugin configuration parameter.
 | 
			
		||||
 | 
			
		||||
    Args:
 | 
			
		||||
        plugin_name: The name of the plugin
 | 
			
		||||
        parameter: The name of the configuration parameter
 | 
			
		||||
        default: The value to return if the parameter is not defined (default: None)
 | 
			
		||||
    """
 | 
			
		||||
    try:
 | 
			
		||||
        plugin_config = settings.PLUGINS_CONFIG[plugin_name]
 | 
			
		||||
        return plugin_config.get(parameter, default)
 | 
			
		||||
    except KeyError:
 | 
			
		||||
        raise ImproperlyConfigured(f"Plugin {plugin_name} is not registered.")
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ from django.core.exceptions import ImproperlyConfigured
 | 
			
		||||
from django.test import Client, TestCase, override_settings
 | 
			
		||||
from django.urls import reverse
 | 
			
		||||
 | 
			
		||||
from extras.plugins import PluginMenu
 | 
			
		||||
from extras.plugins import PluginMenu, get_plugin_config
 | 
			
		||||
from extras.tests.dummy_plugin import config as dummy_config
 | 
			
		||||
from netbox.graphql.schema import Query
 | 
			
		||||
from netbox.registry import registry
 | 
			
		||||
@@ -173,3 +173,13 @@ class PluginTest(TestCase):
 | 
			
		||||
 | 
			
		||||
        self.assertIn(DummyQuery, registry['plugins']['graphql_schemas'])
 | 
			
		||||
        self.assertTrue(issubclass(Query, DummyQuery))
 | 
			
		||||
 | 
			
		||||
    @override_settings(PLUGINS_CONFIG={'extras.tests.dummy_plugin': {'foo': 123}})
 | 
			
		||||
    def test_get_plugin_config(self):
 | 
			
		||||
        """
 | 
			
		||||
        Validate that get_plugin_config() returns config parameters correctly.
 | 
			
		||||
        """
 | 
			
		||||
        plugin = 'extras.tests.dummy_plugin'
 | 
			
		||||
        self.assertEqual(get_plugin_config(plugin, 'foo'), 123)
 | 
			
		||||
        self.assertEqual(get_plugin_config(plugin, 'bar'), None)
 | 
			
		||||
        self.assertEqual(get_plugin_config(plugin, 'bar', default=456), 456)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user