mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Add brief_mode to DynamicModelChoiceMixin
This commit is contained in:
@ -8,9 +8,9 @@ from extras.models import Tag
|
||||
from tenancy.forms import TenancyFilterForm, TenancyForm
|
||||
from tenancy.models import Tenant
|
||||
from utilities.forms import (
|
||||
APISelectMultiple, add_blank_choice, BootstrapMixin, CommentField, CSVChoiceField, CSVModelChoiceField,
|
||||
CSVModelForm, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField, SmallTextarea, SlugField,
|
||||
StaticSelect2, StaticSelect2Multiple, TagFilterField,
|
||||
add_blank_choice, BootstrapMixin, CommentField, CSVChoiceField, CSVModelChoiceField, CSVModelForm, DatePicker,
|
||||
DynamicModelChoiceField, DynamicModelMultipleChoiceField, SmallTextarea, SlugField, StaticSelect2,
|
||||
StaticSelect2Multiple, TagFilterField,
|
||||
)
|
||||
from .choices import CircuitStatusChoices
|
||||
from .models import Circuit, CircuitTermination, CircuitType, Provider
|
||||
|
@ -21,7 +21,7 @@ from ipam.models import IPAddress, VLAN
|
||||
from tenancy.forms import TenancyFilterForm, TenancyForm
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
from utilities.forms import (
|
||||
APISelect, APISelectMultiple, add_blank_choice, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect,
|
||||
APISelect, add_blank_choice, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect,
|
||||
ColorSelect, CommentField, CSVChoiceField, CSVModelChoiceField, CSVModelForm, DynamicModelChoiceField,
|
||||
DynamicModelMultipleChoiceField, ExpandableNameField, form_from_model, JSONField, NumericArrayField, SelectWithPK,
|
||||
SmallTextarea, SlugField, StaticSelect2, StaticSelect2Multiple, TagFilterField, BOOLEAN_WITH_BLANK_CHOICES,
|
||||
@ -2619,20 +2619,20 @@ class InterfaceForm(InterfaceCommonForm, BootstrapMixin, forms.ModelForm):
|
||||
required=False,
|
||||
label='Untagged VLAN',
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
}
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
label='Tagged VLANs',
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
}
|
||||
)
|
||||
tags = DynamicModelMultipleChoiceField(
|
||||
queryset=Tag.objects.all(),
|
||||
@ -2716,19 +2716,19 @@ class InterfaceCreateForm(ComponentCreateForm, InterfaceCommonForm):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
}
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
}
|
||||
)
|
||||
field_order = (
|
||||
'device', 'name_pattern', 'label_pattern', 'type', 'enabled', 'lag', 'mtu', 'mac_address', 'description',
|
||||
@ -2781,19 +2781,19 @@ class InterfaceBulkEditForm(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
}
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
}
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
@ -8,8 +8,8 @@ from extras.forms import (
|
||||
)
|
||||
from extras.models import Tag
|
||||
from utilities.forms import (
|
||||
APISelectMultiple, BootstrapMixin, CSVModelChoiceField, CSVModelForm, DynamicModelChoiceField,
|
||||
DynamicModelMultipleChoiceField, SlugField, StaticSelect2Multiple, TagFilterField,
|
||||
BootstrapMixin, CSVModelChoiceField, CSVModelForm, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
|
||||
SlugField, TagFilterField,
|
||||
)
|
||||
from .constants import *
|
||||
from .models import Secret, SecretRole, UserKey
|
||||
|
@ -251,16 +251,18 @@ class DynamicModelChoiceMixin:
|
||||
:param null_option: The string used to represent a null selection (if any)
|
||||
:param disabled_indicator: The name of the field which, if populated, will disable selection of the
|
||||
choice (optional)
|
||||
:param brief_mode: Use the "brief" format (?brief=true) when making API requests (default)
|
||||
"""
|
||||
filter = django_filters.ModelChoiceFilter
|
||||
widget = widgets.APISelect
|
||||
|
||||
def __init__(self, *args, display_field='name', query_params=None, null_option=None, disabled_indicator=None,
|
||||
**kwargs):
|
||||
def __init__(self, display_field='name', query_params=None, null_option=None, disabled_indicator=None,
|
||||
brief_mode=True, *args, **kwargs):
|
||||
self.display_field = display_field
|
||||
self.query_params = query_params or {}
|
||||
self.null_option = null_option
|
||||
self.disabled_indicator = disabled_indicator
|
||||
self.brief_mode = brief_mode
|
||||
|
||||
# to_field_name is set by ModelChoiceField.__init__(), but we need to set it early for reference
|
||||
# by widget_attrs()
|
||||
@ -285,6 +287,10 @@ class DynamicModelChoiceMixin:
|
||||
if self.disabled_indicator is not None:
|
||||
attrs['disabled-indicator'] = self.disabled_indicator
|
||||
|
||||
# Toggle brief mode
|
||||
if not self.brief_mode:
|
||||
attrs['data-full'] = 'true'
|
||||
|
||||
# Attach any static query parameters
|
||||
for key, value in self.query_params.items():
|
||||
widget.add_query_param(key, value)
|
||||
|
@ -123,7 +123,6 @@ class APISelect(SelectWithDisabled):
|
||||
A select widget populated via an API call
|
||||
|
||||
:param api_url: API endpoint URL. Required if not set automatically by the parent field.
|
||||
:param full: Omit brief=true when fetching REST API results
|
||||
"""
|
||||
def __init__(self, api_url=None, full=False, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
@ -131,8 +130,6 @@ class APISelect(SelectWithDisabled):
|
||||
self.attrs['class'] = 'netbox-select2-api'
|
||||
if api_url:
|
||||
self.attrs['data-url'] = '/{}{}'.format(settings.BASE_PATH, api_url.lstrip('/')) # Inject BASE_PATH
|
||||
if full:
|
||||
self.attrs['data-full'] = full
|
||||
|
||||
def add_query_param(self, name, value):
|
||||
"""
|
||||
|
@ -13,10 +13,10 @@ from ipam.models import IPAddress, VLAN
|
||||
from tenancy.forms import TenancyFilterForm, TenancyForm
|
||||
from tenancy.models import Tenant
|
||||
from utilities.forms import (
|
||||
add_blank_choice, APISelect, APISelectMultiple, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect,
|
||||
BulkRenameForm, CommentField, ConfirmationForm, CSVChoiceField, CSVModelChoiceField, CSVModelForm,
|
||||
DynamicModelChoiceField, DynamicModelMultipleChoiceField, ExpandableNameField, form_from_model, JSONField,
|
||||
SlugField, SmallTextarea, StaticSelect2, StaticSelect2Multiple, TagFilterField, BOOLEAN_WITH_BLANK_CHOICES,
|
||||
add_blank_choice, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect, BulkRenameForm, CommentField,
|
||||
ConfirmationForm, CSVChoiceField, CSVModelChoiceField, CSVModelForm, DynamicModelChoiceField,
|
||||
DynamicModelMultipleChoiceField, ExpandableNameField, form_from_model, JSONField, SlugField, SmallTextarea,
|
||||
StaticSelect2, StaticSelect2Multiple, TagFilterField, BOOLEAN_WITH_BLANK_CHOICES,
|
||||
)
|
||||
from .choices import *
|
||||
from .models import Cluster, ClusterGroup, ClusterType, VirtualMachine, VMInterface
|
||||
@ -526,19 +526,19 @@ class VMInterfaceForm(BootstrapMixin, forms.ModelForm):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
}
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
}
|
||||
)
|
||||
tags = DynamicModelMultipleChoiceField(
|
||||
queryset=Tag.objects.all(),
|
||||
@ -626,19 +626,19 @@ class VMInterfaceCreateForm(BootstrapMixin, forms.Form):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
}
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
}
|
||||
)
|
||||
tags = DynamicModelMultipleChoiceField(
|
||||
queryset=Tag.objects.all(),
|
||||
@ -716,19 +716,19 @@ class VMInterfaceBulkEditForm(BootstrapMixin, BulkEditForm):
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelect(full=True)
|
||||
}
|
||||
)
|
||||
tagged_vlans = DynamicModelMultipleChoiceField(
|
||||
queryset=VLAN.objects.all(),
|
||||
required=False,
|
||||
display_field='display_name',
|
||||
brief_mode=False,
|
||||
query_params={
|
||||
'site_id': 'null',
|
||||
},
|
||||
widget=APISelectMultiple(full=True)
|
||||
}
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
Reference in New Issue
Block a user