From cd6a6b91964d7603a9b92a8e6ce3313816e398f6 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 8 May 2022 10:39:14 +1000 Subject: [PATCH] Access global settings via the API using the of the setting, rather than the - Allows us to access settings even if they have not been 'created' yet - Settings are created with default values if not found --- InvenTree/common/api.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/InvenTree/common/api.py b/InvenTree/common/api.py index f8aaa2ded9..d4986cb3ca 100644 --- a/InvenTree/common/api.py +++ b/InvenTree/common/api.py @@ -158,9 +158,22 @@ class GlobalSettingsDetail(generics.RetrieveUpdateAPIView): - User must have 'staff' status to view / edit """ + lookup_field = 'key' queryset = common.models.InvenTreeSetting.objects.all() serializer_class = common.serializers.GlobalSettingsSerializer + def get_object(self): + """ + Attempt to find a global setting object with the provided key. + """ + + key = self.kwargs['key'] + + if key not in common.models.InvenTreeSetting.SETTINGS.keys(): + raise NotFound() + + return common.models.InvenTreeSetting.get_setting_object(key) + permission_classes = [ GlobalSettingsPermissions, ] @@ -396,7 +409,7 @@ settings_api_urls = [ # Global settings re_path(r'^global/', include([ # Global Settings Detail - re_path(r'^(?P\d+)/', GlobalSettingsDetail.as_view(), name='api-global-setting-detail'), + re_path(r'^(?P\w+)/', GlobalSettingsDetail.as_view(), name='api-global-setting-detail'), # Global Settings List re_path(r'^.*$', GlobalSettingsList.as_view(), name='api-global-setting-list'),