mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-01 04:56:45 +00:00
Add list of 'choices' (read only) to serializer
- Check that the specified value is one of the valid options (if provided)
This commit is contained in:
parent
07851f0b2c
commit
4433befbdc
@ -353,6 +353,11 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
except (ValueError):
|
except (ValueError):
|
||||||
raise ValidationError(_('Must be an integer value'))
|
raise ValidationError(_('Must be an integer value'))
|
||||||
|
|
||||||
|
options = self.valid_options()
|
||||||
|
|
||||||
|
if options and self.value not in options:
|
||||||
|
raise ValidationError(_("Chosen value is not a valid option"))
|
||||||
|
|
||||||
if validator is not None:
|
if validator is not None:
|
||||||
self.run_validator(validator)
|
self.run_validator(validator)
|
||||||
|
|
||||||
@ -419,6 +424,19 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
|
|
||||||
return self.__class__.get_setting_choices(self.key)
|
return self.__class__.get_setting_choices(self.key)
|
||||||
|
|
||||||
|
def valid_options(self):
|
||||||
|
"""
|
||||||
|
Return a list of valid options for this setting
|
||||||
|
"""
|
||||||
|
|
||||||
|
choices = self.choices()
|
||||||
|
|
||||||
|
if not choices:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return [opt[0] for opt in choices]
|
||||||
|
|
||||||
|
|
||||||
def is_bool(self):
|
def is_bool(self):
|
||||||
"""
|
"""
|
||||||
Check if this setting is required to be a boolean value
|
Check if this setting is required to be a boolean value
|
||||||
|
@ -24,6 +24,15 @@ class GlobalSettingsSerializer(InvenTreeModelSerializer):
|
|||||||
|
|
||||||
type = serializers.CharField(source='setting_type', read_only=True)
|
type = serializers.CharField(source='setting_type', read_only=True)
|
||||||
|
|
||||||
|
choices = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
def get_choices(self, obj: InvenTreeUserSetting):
|
||||||
|
"""
|
||||||
|
Returns the choices available for a given item
|
||||||
|
"""
|
||||||
|
|
||||||
|
return obj.choices()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = InvenTreeSetting
|
model = InvenTreeSetting
|
||||||
fields = [
|
fields = [
|
||||||
@ -33,6 +42,7 @@ class GlobalSettingsSerializer(InvenTreeModelSerializer):
|
|||||||
'name',
|
'name',
|
||||||
'description',
|
'description',
|
||||||
'type',
|
'type',
|
||||||
|
'choices',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -51,6 +61,15 @@ class UserSettingsSerializer(InvenTreeModelSerializer):
|
|||||||
|
|
||||||
type = serializers.CharField(source='setting_type', read_only=True)
|
type = serializers.CharField(source='setting_type', read_only=True)
|
||||||
|
|
||||||
|
choices = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
def get_choices(self, obj: InvenTreeUserSetting):
|
||||||
|
"""
|
||||||
|
Returns the choices available for a given item
|
||||||
|
"""
|
||||||
|
|
||||||
|
return obj.choices()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = InvenTreeUserSetting
|
model = InvenTreeUserSetting
|
||||||
fields = [
|
fields = [
|
||||||
@ -61,4 +80,5 @@ class UserSettingsSerializer(InvenTreeModelSerializer):
|
|||||||
'description',
|
'description',
|
||||||
'user',
|
'user',
|
||||||
'type',
|
'type',
|
||||||
|
'choices',
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user