mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-31 04:46:26 -06:00
7699 bulk_edit
This commit is contained in:
parent
33b4beba10
commit
286f56bfc6
@ -1,18 +1,20 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from dcim.choices import InterfaceModeChoices
|
from dcim.choices import InterfaceModeChoices
|
||||||
from dcim.constants import INTERFACE_MTU_MAX, INTERFACE_MTU_MIN
|
from dcim.constants import INTERFACE_MTU_MAX, INTERFACE_MTU_MIN
|
||||||
from dcim.models import Device, DeviceRole, Platform, Region, Site, SiteGroup
|
from dcim.models import Device, DeviceRole, Platform, Site
|
||||||
from extras.models import ConfigTemplate
|
from extras.models import ConfigTemplate
|
||||||
from ipam.models import VLAN, VLANGroup, VRF
|
from ipam.models import VLAN, VLANGroup, VRF
|
||||||
from netbox.forms import NetBoxModelBulkEditForm
|
from netbox.forms import NetBoxModelBulkEditForm
|
||||||
from tenancy.models import Tenant
|
from tenancy.models import Tenant
|
||||||
from utilities.forms import BulkRenameForm, add_blank_choice
|
from utilities.forms import BulkRenameForm, add_blank_choice, get_field_value
|
||||||
from utilities.forms.fields import CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField
|
from utilities.forms.fields import CommentField, ContentTypeChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField
|
||||||
from utilities.forms.rendering import FieldSet
|
from utilities.forms.rendering import FieldSet
|
||||||
from utilities.forms.widgets import BulkEditNullBooleanSelect
|
from utilities.forms.widgets import BulkEditNullBooleanSelect, HTMXSelect
|
||||||
from virtualization.choices import *
|
from virtualization.choices import *
|
||||||
|
from virtualization.constants import CLUSTER_SCOPE_TYPES
|
||||||
from virtualization.models import *
|
from virtualization.models import *
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -77,24 +79,18 @@ class ClusterBulkEditForm(NetBoxModelBulkEditForm):
|
|||||||
queryset=Tenant.objects.all(),
|
queryset=Tenant.objects.all(),
|
||||||
required=False
|
required=False
|
||||||
)
|
)
|
||||||
region = DynamicModelChoiceField(
|
scope_type = ContentTypeChoiceField(
|
||||||
label=_('Region'),
|
queryset=ContentType.objects.filter(model__in=CLUSTER_SCOPE_TYPES),
|
||||||
queryset=Region.objects.all(),
|
widget=HTMXSelect(method='post', attrs={'hx-select': '#form_fields'}),
|
||||||
required=False,
|
required=False,
|
||||||
|
label=_('Scope type')
|
||||||
)
|
)
|
||||||
site_group = DynamicModelChoiceField(
|
scope = DynamicModelChoiceField(
|
||||||
label=_('Site group'),
|
label=_('Scope'),
|
||||||
queryset=SiteGroup.objects.all(),
|
queryset=Site.objects.none(), # Initial queryset
|
||||||
required=False,
|
required=False,
|
||||||
)
|
disabled=True,
|
||||||
site = DynamicModelChoiceField(
|
selector=True
|
||||||
label=_('Site'),
|
|
||||||
queryset=Site.objects.all(),
|
|
||||||
required=False,
|
|
||||||
query_params={
|
|
||||||
'region_id': '$region',
|
|
||||||
'group_id': '$site_group',
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
description = forms.CharField(
|
description = forms.CharField(
|
||||||
label=_('Description'),
|
label=_('Description'),
|
||||||
@ -106,13 +102,28 @@ class ClusterBulkEditForm(NetBoxModelBulkEditForm):
|
|||||||
model = Cluster
|
model = Cluster
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
FieldSet('type', 'group', 'status', 'tenant', 'description'),
|
FieldSet('type', 'group', 'status', 'tenant', 'description'),
|
||||||
FieldSet('region', 'site_group', 'site', name=_('Site')),
|
FieldSet('scope_type', 'scope', name=_('Scope')),
|
||||||
)
|
)
|
||||||
nullable_fields = (
|
nullable_fields = (
|
||||||
'group', 'site', 'tenant', 'description', 'comments',
|
'group', 'scope', 'tenant', 'description', 'comments',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
if scope_type_id := get_field_value(self, 'scope_type'):
|
||||||
|
try:
|
||||||
|
scope_type = ContentType.objects.get(pk=scope_type_id)
|
||||||
|
model = scope_type.model_class()
|
||||||
|
self.fields['scope'].queryset = model.objects.all()
|
||||||
|
self.fields['scope'].widget.attrs['selector'] = model._meta.label_lower
|
||||||
|
self.fields['scope'].disabled = False
|
||||||
|
self.fields['scope'].label = _(bettertitle(model._meta.verbose_name))
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class VirtualMachineBulkEditForm(NetBoxModelBulkEditForm):
|
class VirtualMachineBulkEditForm(NetBoxModelBulkEditForm):
|
||||||
status = forms.ChoiceField(
|
status = forms.ChoiceField(
|
||||||
label=_('Status'),
|
label=_('Status'),
|
||||||
|
Loading…
Reference in New Issue
Block a user