Access global settings via the API using the <key> of the setting, rather than the <PK>

- Allows us to access settings even if they have not been 'created' yet
- Settings are created with default values if not found
This commit is contained in:
Oliver Walters 2022-05-08 10:39:14 +10:00
parent 64e2234159
commit cd6a6b9196

View File

@ -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<pk>\d+)/', GlobalSettingsDetail.as_view(), name='api-global-setting-detail'),
re_path(r'^(?P<key>\w+)/', GlobalSettingsDetail.as_view(), name='api-global-setting-detail'),
# Global Settings List
re_path(r'^.*$', GlobalSettingsList.as_view(), name='api-global-setting-list'),