mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-02 11:40:58 +00:00
[FR] Simplify / optimize setting loading (#4152)
* [FR] Simplify / optimize setting loading Cache config.yaml data on load and use cached for get_settings Fixes #4149 * move the cache setting to config * add docstring * spell fix * Add lookup where settings come from Fixes #3982 * Fix spelling
This commit is contained in:
@ -18,9 +18,11 @@ from rest_framework.views import APIView
|
||||
import common.models
|
||||
import common.serializers
|
||||
from InvenTree.api import BulkDeleteMixin
|
||||
from InvenTree.config import CONFIG_LOOKUPS
|
||||
from InvenTree.helpers import inheritors
|
||||
from InvenTree.mixins import (ListAPI, RetrieveAPI, RetrieveUpdateAPI,
|
||||
RetrieveUpdateDestroyAPI)
|
||||
from InvenTree.permissions import IsSuperuser
|
||||
from plugin.models import NotificationUserSetting
|
||||
from plugin.serializers import NotificationUserSettingSerializer
|
||||
|
||||
@ -360,6 +362,29 @@ class NewsFeedEntryDetail(NewsFeedMixin, RetrieveUpdateDestroyAPI):
|
||||
"""Detail view for an individual news feed object."""
|
||||
|
||||
|
||||
class ConfigList(ListAPI):
|
||||
"""List view for all accessed configurations."""
|
||||
|
||||
queryset = CONFIG_LOOKUPS
|
||||
serializer_class = common.serializers.ConfigSerializer
|
||||
permission_classes = [IsSuperuser, ]
|
||||
|
||||
|
||||
class ConfigDetail(RetrieveAPI):
|
||||
"""Detail view for an individual configuration."""
|
||||
|
||||
serializer_class = common.serializers.ConfigSerializer
|
||||
permission_classes = [IsSuperuser, ]
|
||||
|
||||
def get_object(self):
|
||||
"""Attempt to find a config object with the provided key."""
|
||||
key = self.kwargs['key']
|
||||
value = CONFIG_LOOKUPS.get(key, None)
|
||||
if not value:
|
||||
raise NotFound()
|
||||
return {key: value}
|
||||
|
||||
|
||||
settings_api_urls = [
|
||||
# User settings
|
||||
re_path(r'^user/', include([
|
||||
@ -415,3 +440,9 @@ common_api_urls = [
|
||||
])),
|
||||
|
||||
]
|
||||
|
||||
admin_api_urls = [
|
||||
# Admin
|
||||
path('config/', ConfigList.as_view(), name='api-config-list'),
|
||||
path('config/<str:key>/', ConfigDetail.as_view(), name='api-config-detail'),
|
||||
]
|
||||
|
@ -222,3 +222,16 @@ class NewsFeedEntrySerializer(InvenTreeModelSerializer):
|
||||
'summary',
|
||||
'read',
|
||||
]
|
||||
|
||||
|
||||
class ConfigSerializer(serializers.Serializer):
|
||||
"""Serializer for the InvenTree configuration.
|
||||
|
||||
This is a read-only serializer.
|
||||
"""
|
||||
|
||||
def to_representation(self, instance):
|
||||
"""Return the configuration data as a dictionary."""
|
||||
if not isinstance(instance, str):
|
||||
instance = list(instance.keys())[0]
|
||||
return {'key': instance, **self.instance[instance]}
|
||||
|
@ -827,7 +827,7 @@ class NotificationTest(InvenTreeAPITestCase):
|
||||
self.assertEqual(NotificationMessage.objects.filter(user=self.user).count(), 3)
|
||||
|
||||
|
||||
class LoadingTest(TestCase):
|
||||
class CommonTest(InvenTreeAPITestCase):
|
||||
"""Tests for the common config."""
|
||||
|
||||
def test_restart_flag(self):
|
||||
@ -844,6 +844,30 @@ class LoadingTest(TestCase):
|
||||
# now it should be false again
|
||||
self.assertFalse(common.models.InvenTreeSetting.get_setting('SERVER_RESTART_REQUIRED'))
|
||||
|
||||
def test_config_api(self):
|
||||
"""Test config URLs."""
|
||||
# Not superuser
|
||||
self.get(reverse('api-config-list'), expected_code=403)
|
||||
|
||||
# Turn into superuser
|
||||
self.user.is_superuser = True
|
||||
self.user.save()
|
||||
|
||||
# Successfull checks
|
||||
data = [
|
||||
self.get(reverse('api-config-list'), expected_code=200).data[0], # list endpoint
|
||||
self.get(reverse('api-config-detail', kwargs={'key': 'INVENTREE_DEBUG'}), expected_code=200).data, # detail endpoint
|
||||
]
|
||||
|
||||
for item in data:
|
||||
self.assertEqual(item['key'], 'INVENTREE_DEBUG')
|
||||
self.assertEqual(item['env_var'], 'INVENTREE_DEBUG')
|
||||
self.assertEqual(item['config_key'], 'debug')
|
||||
|
||||
# Turn into normal user again
|
||||
self.user.is_superuser = False
|
||||
self.user.save()
|
||||
|
||||
|
||||
class ColorThemeTest(TestCase):
|
||||
"""Tests for ColorTheme."""
|
||||
|
Reference in New Issue
Block a user