mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 02:48:38 -06:00
Misc cleanup
This commit is contained in:
parent
668cfaf025
commit
5c858050d7
@ -1,5 +1,6 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
from django.core.validators import EMPTY_VALUES
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from timezone_field import TimeZoneFormField
|
from timezone_field import TimeZoneFormField
|
||||||
|
|
||||||
@ -498,7 +499,8 @@ class ModuleTypeForm(NetBoxModelForm):
|
|||||||
if self.cleaned_data.get('profile'):
|
if self.cleaned_data.get('profile'):
|
||||||
self.instance.attribute_data = {
|
self.instance.attribute_data = {
|
||||||
name[5:]: self.cleaned_data[name] # Remove the attr_ prefix
|
name[5:]: self.cleaned_data[name] # Remove the attr_ prefix
|
||||||
for name in self.attr_fields if self.cleaned_data[name] is not None
|
for name in self.attr_fields
|
||||||
|
if self.cleaned_data.get(name) not in EMPTY_VALUES
|
||||||
}
|
}
|
||||||
|
|
||||||
return super()._post_clean()
|
return super()._post_clean()
|
||||||
|
@ -361,7 +361,7 @@ class AttributeFiltersMixin:
|
|||||||
if data is not None:
|
if data is not None:
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
if key.startswith(self.attribute_filter_prefix):
|
if key.startswith(self.attribute_filter_prefix):
|
||||||
# Attempt to case the value to a native JSON type
|
# Attempt to cast the value to a native JSON type
|
||||||
try:
|
try:
|
||||||
value = json.loads(value)
|
value = json.loads(value)
|
||||||
except (ValueError, json.JSONDecodeError):
|
except (ValueError, json.JSONDecodeError):
|
||||||
|
@ -87,10 +87,13 @@ class JSONSchemaProperty:
|
|||||||
|
|
||||||
# Choices
|
# Choices
|
||||||
if self.enum:
|
if self.enum:
|
||||||
field_kwargs['choices'] = [(v, v) for v in self.enum]
|
choices = [(v, v) for v in self.enum]
|
||||||
|
if not required:
|
||||||
|
choices = [(None, ''), *choices]
|
||||||
|
field_kwargs['choices'] = choices
|
||||||
|
|
||||||
# String validation
|
# String validation
|
||||||
elif self.type == PropertyTypeEnum.STRING:
|
if self.type == PropertyTypeEnum.STRING.value:
|
||||||
if self.minLength is not None:
|
if self.minLength is not None:
|
||||||
field_kwargs['min_length'] = self.minLength
|
field_kwargs['min_length'] = self.minLength
|
||||||
if self.maxLength is not None:
|
if self.maxLength is not None:
|
||||||
@ -101,16 +104,16 @@ class JSONSchemaProperty:
|
|||||||
]
|
]
|
||||||
|
|
||||||
# Integer/number validation
|
# Integer/number validation
|
||||||
elif self.type in (PropertyTypeEnum.INTEGER, PropertyTypeEnum.NUMBER):
|
elif self.type in (PropertyTypeEnum.INTEGER.value, PropertyTypeEnum.NUMBER.value):
|
||||||
|
field_kwargs['widget'] = forms.NumberInput(attrs={'step': 'any'})
|
||||||
if self.minimum:
|
if self.minimum:
|
||||||
field_kwargs['min_value'] = self.minimum
|
field_kwargs['min_value'] = self.minimum
|
||||||
if self.maximum:
|
if self.maximum:
|
||||||
field_kwargs['min_value'] = self.maximum
|
field_kwargs['max_value'] = self.maximum
|
||||||
if self.multipleOf:
|
if self.multipleOf:
|
||||||
field_kwargs['validators'] = [
|
field_kwargs['validators'] = [
|
||||||
MultipleOfValidator(multiple=self.multipleOf)
|
MultipleOfValidator(multiple=self.multipleOf)
|
||||||
]
|
]
|
||||||
|
|
||||||
return self.field_class(**field_kwargs)
|
return self.field_class(**field_kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
Loading…
Reference in New Issue
Block a user