2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-02 03:30:54 +00:00

Update settings API endpoints: (#5684)

* Update settings API endpoints:

- Use cache to prevent unnecessary hits to db
- Make settings detail endpoints case insensitive
- Update API version

* Remove is_admin and is_superuser

- Contaminated from other commit

* revert seralizers.py

* Revert breaking change to users/api.py
This commit is contained in:
Oliver
2023-10-11 22:58:11 +11:00
committed by GitHub
parent e8e0b57cea
commit 89faf8e59d
5 changed files with 29 additions and 9 deletions

View File

@ -229,9 +229,9 @@ class GlobalSettingsDetail(RetrieveUpdateAPI):
def get_object(self):
"""Attempt to find a global setting object with the provided key."""
key = self.kwargs['key']
key = str(self.kwargs['key']).upper()
if key not in common.models.InvenTreeSetting.SETTINGS.keys():
if key.startswith('_') or key not in common.models.InvenTreeSetting.SETTINGS.keys():
raise NotFound()
return common.models.InvenTreeSetting.get_setting_object(
@ -296,9 +296,9 @@ class UserSettingsDetail(RetrieveUpdateAPI):
def get_object(self):
"""Attempt to find a user setting object with the provided key."""
key = self.kwargs['key']
key = str(self.kwargs['key']).upper()
if key not in common.models.InvenTreeUserSetting.SETTINGS.keys():
if key.startswith('_') or key not in common.models.InvenTreeUserSetting.SETTINGS.keys():
raise NotFound()
return common.models.InvenTreeUserSetting.get_setting_object(

View File

@ -205,6 +205,12 @@ class BaseInvenTreeSetting(models.Model):
If a particular setting is not present, create it with the default value
"""
cache_key = f"BUILD_DEFAULT_VALUES:{str(cls.__name__)}"
if InvenTree.helpers.str2bool(cache.get(cache_key, False)):
# Already built default values
return
try:
existing_keys = cls.objects.filter(**kwargs).values_list('key', flat=True)
settings_keys = cls.SETTINGS.keys()
@ -224,6 +230,8 @@ class BaseInvenTreeSetting(models.Model):
logger.exception("Failed to build default values for %s (%s)", str(cls), str(type(exc)))
pass
cache.set(cache_key, True, timeout=3600)
def _call_settings_function(self, reference: str, args, kwargs):
"""Call a function associated with a particular setting.
@ -1241,7 +1249,7 @@ class InvenTreeSetting(BaseInvenTreeSetting):
'BARCODE_ENABLE': {
'name': _('Barcode Support'),
'description': _('Enable barcode scanner support'),
'description': _('Enable barcode scanner support in the web interface'),
'default': True,
'validator': bool,
},

View File

@ -51,6 +51,8 @@ class SettingsSerializer(InvenTreeModelSerializer):
value = SettingsValueField()
units = serializers.CharField(read_only=True)
def get_choices(self, obj):
"""Returns the choices available for a given item."""
results = []
@ -81,6 +83,7 @@ class GlobalSettingsSerializer(SettingsSerializer):
'name',
'description',
'type',
'units',
'choices',
'model_name',
'api_url',
@ -103,6 +106,7 @@ class UserSettingsSerializer(SettingsSerializer):
'description',
'user',
'type',
'units',
'choices',
'model_name',
'api_url',