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 django import forms
|
||||||
|
|
||||||
from netbox.config.parameters import PARAMS
|
from netbox.config import get_config, PARAMS
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'ConfigRevisionForm',
|
'ConfigRevisionForm',
|
||||||
@ -13,18 +13,20 @@ EMPTY_VALUES = ('', None, [], ())
|
|||||||
class FormMetaclass(forms.models.ModelFormMetaclass):
|
class FormMetaclass(forms.models.ModelFormMetaclass):
|
||||||
|
|
||||||
def __new__(mcs, name, bases, attrs):
|
def __new__(mcs, name, bases, attrs):
|
||||||
|
config = get_config()
|
||||||
|
|
||||||
# Emulate a declared field for each supported configuration parameter
|
# Emulate a declared field for each supported configuration parameter
|
||||||
param_fields = {}
|
param_fields = {}
|
||||||
for param in PARAMS:
|
for param in PARAMS:
|
||||||
help_text = f'{param.description}<br />' if param.description else ''
|
help_text = f'{param.description}<br />' if param.description else ''
|
||||||
# help_text += f'Current value: <strong>{getattr(settings, param.name)}</strong>'
|
help_text += f'Current value: <strong>{getattr(config, param.name)}</strong>'
|
||||||
param_fields[param.name] = param.field(
|
field_kwargs = {
|
||||||
required=False,
|
'required': False,
|
||||||
label=param.label,
|
'label': param.label,
|
||||||
help_text=help_text,
|
'help_text': help_text,
|
||||||
**param.field_kwargs
|
}
|
||||||
)
|
field_kwargs.update(**param.field_kwargs)
|
||||||
|
param_fields[param.name] = param.field(**field_kwargs)
|
||||||
attrs.update(param_fields)
|
attrs.update(param_fields)
|
||||||
|
|
||||||
return super().__new__(mcs, name, bases, attrs)
|
return super().__new__(mcs, name, bases, attrs)
|
||||||
@ -39,12 +41,6 @@ class ConfigRevisionForm(forms.BaseModelForm, metaclass=FormMetaclass):
|
|||||||
'comment': forms.Textarea(),
|
'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):
|
def save(self, commit=True):
|
||||||
instance = super().save(commit=False)
|
instance = super().save(commit=False)
|
||||||
|
|
||||||
|
@ -2,23 +2,6 @@ from django import forms
|
|||||||
from django.contrib.postgres.forms import SimpleArrayField
|
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:
|
class ConfigParam:
|
||||||
|
|
||||||
def __init__(self, name, label, default, description=None, field=None, field_kwargs=None):
|
def __init__(self, name, label, default, description=None, field=None, field_kwargs=None):
|
||||||
@ -43,14 +26,14 @@ PARAMS = (
|
|||||||
label='Globally unique IP space',
|
label='Globally unique IP space',
|
||||||
default=False,
|
default=False,
|
||||||
description="Enforce unique IP addressing within the global table",
|
description="Enforce unique IP addressing within the global table",
|
||||||
field=OptionalBooleanField
|
field=forms.BooleanField
|
||||||
),
|
),
|
||||||
ConfigParam(
|
ConfigParam(
|
||||||
name='PREFER_IPV4',
|
name='PREFER_IPV4',
|
||||||
label='Prefer IPv4',
|
label='Prefer IPv4',
|
||||||
default=False,
|
default=False,
|
||||||
description="Prefer IPv4 addresses over IPv6",
|
description="Prefer IPv4 addresses over IPv6",
|
||||||
field=OptionalBooleanField
|
field=forms.BooleanField
|
||||||
),
|
),
|
||||||
|
|
||||||
# Racks
|
# Racks
|
||||||
@ -127,7 +110,7 @@ PARAMS = (
|
|||||||
label='Maintenance mode',
|
label='Maintenance mode',
|
||||||
default=False,
|
default=False,
|
||||||
description="Enable maintenance mode",
|
description="Enable maintenance mode",
|
||||||
field=OptionalBooleanField
|
field=forms.BooleanField
|
||||||
),
|
),
|
||||||
ConfigParam(
|
ConfigParam(
|
||||||
name='MAPS_URL',
|
name='MAPS_URL',
|
||||||
|
Reference in New Issue
Block a user