mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Clean up boolean fields
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
from django import forms
|
||||
|
||||
from netbox.config.parameters import PARAMS
|
||||
from netbox.config import get_config, PARAMS
|
||||
|
||||
__all__ = (
|
||||
'ConfigRevisionForm',
|
||||
@ -13,18 +13,20 @@ EMPTY_VALUES = ('', None, [], ())
|
||||
class FormMetaclass(forms.models.ModelFormMetaclass):
|
||||
|
||||
def __new__(mcs, name, bases, attrs):
|
||||
config = get_config()
|
||||
|
||||
# Emulate a declared field for each supported configuration parameter
|
||||
param_fields = {}
|
||||
for param in PARAMS:
|
||||
help_text = f'{param.description}<br />' if param.description else ''
|
||||
# help_text += f'Current value: <strong>{getattr(settings, param.name)}</strong>'
|
||||
param_fields[param.name] = param.field(
|
||||
required=False,
|
||||
label=param.label,
|
||||
help_text=help_text,
|
||||
**param.field_kwargs
|
||||
)
|
||||
help_text += f'Current value: <strong>{getattr(config, param.name)}</strong>'
|
||||
field_kwargs = {
|
||||
'required': False,
|
||||
'label': param.label,
|
||||
'help_text': help_text,
|
||||
}
|
||||
field_kwargs.update(**param.field_kwargs)
|
||||
param_fields[param.name] = param.field(**field_kwargs)
|
||||
attrs.update(param_fields)
|
||||
|
||||
return super().__new__(mcs, name, bases, attrs)
|
||||
@ -39,12 +41,6 @@ class ConfigRevisionForm(forms.BaseModelForm, metaclass=FormMetaclass):
|
||||
'comment': forms.Textarea(),
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# Bugfix for django-timezone-field: Add empty choice to default options
|
||||
# self.fields['TIME_ZONE'].choices = [('', ''), *self.fields['TIME_ZONE'].choices]
|
||||
|
||||
def save(self, commit=True):
|
||||
instance = super().save(commit=False)
|
||||
|
||||
|
@ -2,23 +2,6 @@ from django import forms
|
||||
from django.contrib.postgres.forms import SimpleArrayField
|
||||
|
||||
|
||||
class OptionalBooleanSelect(forms.Select):
|
||||
"""
|
||||
An optional boolean field (yes/no/default).
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
choices = (
|
||||
('', 'Default'),
|
||||
(True, 'Yes'),
|
||||
(False, 'No'),
|
||||
)
|
||||
super().__init__(attrs, choices)
|
||||
|
||||
|
||||
class OptionalBooleanField(forms.NullBooleanField):
|
||||
widget = OptionalBooleanSelect
|
||||
|
||||
|
||||
class ConfigParam:
|
||||
|
||||
def __init__(self, name, label, default, description=None, field=None, field_kwargs=None):
|
||||
@ -43,14 +26,14 @@ PARAMS = (
|
||||
label='Globally unique IP space',
|
||||
default=False,
|
||||
description="Enforce unique IP addressing within the global table",
|
||||
field=OptionalBooleanField
|
||||
field=forms.BooleanField
|
||||
),
|
||||
ConfigParam(
|
||||
name='PREFER_IPV4',
|
||||
label='Prefer IPv4',
|
||||
default=False,
|
||||
description="Prefer IPv4 addresses over IPv6",
|
||||
field=OptionalBooleanField
|
||||
field=forms.BooleanField
|
||||
),
|
||||
|
||||
# Racks
|
||||
@ -127,7 +110,7 @@ PARAMS = (
|
||||
label='Maintenance mode',
|
||||
default=False,
|
||||
description="Enable maintenance mode",
|
||||
field=OptionalBooleanField
|
||||
field=forms.BooleanField
|
||||
),
|
||||
ConfigParam(
|
||||
name='MAPS_URL',
|
||||
|
Reference in New Issue
Block a user