mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 11:10:54 +00:00
Implement new approach for plugin settings
- URL specifies plugin slug and setting key
This commit is contained in:
@ -10,6 +10,7 @@ from django.urls import include, re_path
|
||||
from rest_framework import generics
|
||||
from rest_framework import status
|
||||
from rest_framework import permissions
|
||||
from rest_framework.exceptions import NotFound
|
||||
from rest_framework.response import Response
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
@ -17,6 +18,7 @@ from django_filters.rest_framework import DjangoFilterBackend
|
||||
from common.api import GlobalSettingsPermissions
|
||||
from plugin.models import PluginConfig, PluginSetting
|
||||
import plugin.serializers as PluginSerializers
|
||||
from plugin.registry import registry
|
||||
|
||||
|
||||
class PluginList(generics.ListAPIView):
|
||||
@ -120,6 +122,34 @@ class PluginSettingDetail(generics.RetrieveUpdateAPIView):
|
||||
queryset = PluginSetting.objects.all()
|
||||
serializer_class = PluginSerializers.PluginSettingSerializer
|
||||
|
||||
def get_object(self):
|
||||
"""
|
||||
Lookup the plugin setting object, based on the URL.
|
||||
The URL provides the 'slug' of the plugin, and the 'key' of the setting.
|
||||
|
||||
Both the 'slug' and 'key' must be valid, else a 404 error is raised
|
||||
"""
|
||||
|
||||
plugin_slug = self.kwargs['plugin']
|
||||
key = self.kwargs['key']
|
||||
|
||||
# Check that the 'plugin' specified is valid!
|
||||
if not PluginConfig.objects.filter(key=plugin_slug).exists():
|
||||
raise NotFound(detail=f"Plugin '{plugin_slug}' not installed")
|
||||
|
||||
# Get the list of settings available for the specified plugin
|
||||
plugin = registry.get_plugin(plugin_slug)
|
||||
|
||||
if plugin is None:
|
||||
raise NotFound(detail=f"Plugin '{plugin_slug}' not found")
|
||||
|
||||
settings = getattr(plugin, 'SETTINGS', {})
|
||||
|
||||
if key not in settings:
|
||||
raise NotFound(detail=f"Plugin '{plugin_slug}' has no setting matching '{key}'")
|
||||
|
||||
return PluginSetting.get_setting_object(key, plugin=plugin)
|
||||
|
||||
# Staff permission required
|
||||
permission_classes = [
|
||||
GlobalSettingsPermissions,
|
||||
@ -130,7 +160,7 @@ plugin_api_urls = [
|
||||
|
||||
# Plugin settings URLs
|
||||
re_path(r'^settings/', include([
|
||||
re_path(r'^(?P<pk>\d+)/', PluginSettingDetail.as_view(), name='api-plugin-setting-detail'),
|
||||
re_path(r'^(?P<plugin>\w+)/(?P<key>\w+)/', PluginSettingDetail.as_view(), name='api-plugin-setting-detail'),
|
||||
re_path(r'^.*$', PluginSettingList.as_view(), name='api-plugin-setting-list'),
|
||||
])),
|
||||
|
||||
|
@ -63,6 +63,17 @@ class PluginsRegistry:
|
||||
# mixins
|
||||
self.mixins_settings = {}
|
||||
|
||||
def get_plugin(self, slug):
|
||||
"""
|
||||
Lookup plugin by slug (unique key).
|
||||
"""
|
||||
|
||||
if slug not in self.plugins:
|
||||
logger.warning(f"Plugin registry has no record of plugin '{slug}'")
|
||||
return None
|
||||
|
||||
return self.plugins[slug]
|
||||
|
||||
def call_plugin_function(self, slug, func, *args, **kwargs):
|
||||
"""
|
||||
Call a member function (named by 'func') of the plugin named by 'slug'.
|
||||
@ -73,11 +84,19 @@ class PluginsRegistry:
|
||||
Instead, any error messages are returned to the worker.
|
||||
"""
|
||||
|
||||
plugin = self.plugins[slug]
|
||||
plugin = self.get_plugin(slug)
|
||||
|
||||
plugin_func = getattr(plugin, func)
|
||||
if not plugin:
|
||||
return
|
||||
|
||||
return plugin_func(*args, **kwargs)
|
||||
# Check that the plugin is enabled
|
||||
config = plugin.plugin_config()
|
||||
|
||||
if config and config.active:
|
||||
|
||||
plugin_func = getattr(plugin, func)
|
||||
|
||||
return plugin_func(*args, **kwargs)
|
||||
|
||||
# region public functions
|
||||
# region loading / unloading
|
||||
|
Reference in New Issue
Block a user