mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Add initial_params to all forms
This commit is contained in:
parent
1f0b1dd10e
commit
7598b891c1
@ -4280,7 +4280,10 @@ class PowerPanelFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|||||||
class PowerFeedForm(BootstrapMixin, CustomFieldModelForm):
|
class PowerFeedForm(BootstrapMixin, CustomFieldModelForm):
|
||||||
site = DynamicModelChoiceField(
|
site = DynamicModelChoiceField(
|
||||||
queryset=Site.objects.all(),
|
queryset=Site.objects.all(),
|
||||||
required=False
|
required=False,
|
||||||
|
initial_params={
|
||||||
|
'powerpanel_set': '$power_panel'
|
||||||
|
}
|
||||||
)
|
)
|
||||||
power_panel = DynamicModelChoiceField(
|
power_panel = DynamicModelChoiceField(
|
||||||
queryset=PowerPanel.objects.all(),
|
queryset=PowerPanel.objects.all(),
|
||||||
@ -4315,14 +4318,6 @@ class PowerFeedForm(BootstrapMixin, CustomFieldModelForm):
|
|||||||
'phase': StaticSelect2(),
|
'phase': StaticSelect2(),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
# Initialize site field
|
|
||||||
if self.instance and hasattr(self.instance, 'power_panel'):
|
|
||||||
self.initial['site'] = self.instance.power_panel.site
|
|
||||||
|
|
||||||
|
|
||||||
class PowerFeedCSVForm(CustomFieldModelCSVForm):
|
class PowerFeedCSVForm(CustomFieldModelCSVForm):
|
||||||
site = CSVModelChoiceField(
|
site = CSVModelChoiceField(
|
||||||
|
@ -100,23 +100,6 @@ class DeviceTestCase(TestCase):
|
|||||||
self.assertIn('face', form.errors)
|
self.assertIn('face', form.errors)
|
||||||
self.assertIn('position', form.errors)
|
self.assertIn('position', form.errors)
|
||||||
|
|
||||||
def test_initial_data_population(self):
|
|
||||||
device_type = DeviceType.objects.first()
|
|
||||||
cluster = Cluster.objects.first()
|
|
||||||
test = DeviceForm(initial={
|
|
||||||
'device_type': device_type.pk,
|
|
||||||
'device_role': DeviceRole.objects.first().pk,
|
|
||||||
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
|
||||||
'site': Site.objects.first().pk,
|
|
||||||
'cluster': cluster.pk,
|
|
||||||
})
|
|
||||||
|
|
||||||
# Check that the initial value for the manufacturer is set automatically when assigning the device type
|
|
||||||
self.assertEqual(test.initial['manufacturer'], device_type.manufacturer.pk)
|
|
||||||
|
|
||||||
# Check that the initial value for the cluster group is set automatically when assigning the cluster
|
|
||||||
self.assertEqual(test.initial['cluster_group'], cluster.group.pk)
|
|
||||||
|
|
||||||
|
|
||||||
class LabelTestCase(TestCase):
|
class LabelTestCase(TestCase):
|
||||||
|
|
||||||
|
@ -265,6 +265,9 @@ class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|||||||
null_option='None',
|
null_option='None',
|
||||||
query_params={
|
query_params={
|
||||||
'site_id': '$site'
|
'site_id': '$site'
|
||||||
|
},
|
||||||
|
initial_params={
|
||||||
|
'vlans': '$vlan'
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
vlan = DynamicModelChoiceField(
|
vlan = DynamicModelChoiceField(
|
||||||
@ -297,14 +300,6 @@ class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
||||||
# Initialize helper selectors
|
|
||||||
instance = kwargs.get('instance')
|
|
||||||
initial = kwargs.get('initial', {}).copy()
|
|
||||||
if instance and instance.vlan is not None:
|
|
||||||
initial['vlan_group'] = instance.vlan.group
|
|
||||||
kwargs['initial'] = initial
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.fields['vrf'].empty_label = 'Global'
|
self.fields['vrf'].empty_label = 'Global'
|
||||||
@ -501,7 +496,10 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel
|
|||||||
device = DynamicModelChoiceField(
|
device = DynamicModelChoiceField(
|
||||||
queryset=Device.objects.all(),
|
queryset=Device.objects.all(),
|
||||||
required=False,
|
required=False,
|
||||||
display_field='display_name'
|
display_field='display_name',
|
||||||
|
initial_params={
|
||||||
|
'interfaces': '$interface'
|
||||||
|
}
|
||||||
)
|
)
|
||||||
interface = DynamicModelChoiceField(
|
interface = DynamicModelChoiceField(
|
||||||
queryset=Interface.objects.all(),
|
queryset=Interface.objects.all(),
|
||||||
@ -512,7 +510,10 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel
|
|||||||
)
|
)
|
||||||
virtual_machine = DynamicModelChoiceField(
|
virtual_machine = DynamicModelChoiceField(
|
||||||
queryset=VirtualMachine.objects.all(),
|
queryset=VirtualMachine.objects.all(),
|
||||||
required=False
|
required=False,
|
||||||
|
initial_params={
|
||||||
|
'interfaces': '$vminterface'
|
||||||
|
}
|
||||||
)
|
)
|
||||||
vminterface = DynamicModelChoiceField(
|
vminterface = DynamicModelChoiceField(
|
||||||
queryset=VMInterface.objects.all(),
|
queryset=VMInterface.objects.all(),
|
||||||
@ -611,10 +612,8 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel
|
|||||||
initial = kwargs.get('initial', {}).copy()
|
initial = kwargs.get('initial', {}).copy()
|
||||||
if instance:
|
if instance:
|
||||||
if type(instance.assigned_object) is Interface:
|
if type(instance.assigned_object) is Interface:
|
||||||
initial['device'] = instance.assigned_object.device
|
|
||||||
initial['interface'] = instance.assigned_object
|
initial['interface'] = instance.assigned_object
|
||||||
elif type(instance.assigned_object) is VMInterface:
|
elif type(instance.assigned_object) is VMInterface:
|
||||||
initial['virtual_machine'] = instance.assigned_object.virtual_machine
|
|
||||||
initial['vminterface'] = instance.assigned_object
|
initial['vminterface'] = instance.assigned_object
|
||||||
if instance.nat_inside:
|
if instance.nat_inside:
|
||||||
nat_inside_parent = instance.nat_inside.assigned_object
|
nat_inside_parent = instance.nat_inside.assigned_object
|
||||||
|
@ -119,7 +119,10 @@ class TenancyForm(forms.Form):
|
|||||||
tenant_group = DynamicModelChoiceField(
|
tenant_group = DynamicModelChoiceField(
|
||||||
queryset=TenantGroup.objects.all(),
|
queryset=TenantGroup.objects.all(),
|
||||||
required=False,
|
required=False,
|
||||||
null_option='None'
|
null_option='None',
|
||||||
|
initial_params={
|
||||||
|
'tenants': '$tenant'
|
||||||
|
}
|
||||||
)
|
)
|
||||||
tenant = DynamicModelChoiceField(
|
tenant = DynamicModelChoiceField(
|
||||||
queryset=Tenant.objects.all(),
|
queryset=Tenant.objects.all(),
|
||||||
@ -129,17 +132,6 @@ class TenancyForm(forms.Form):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
|
|
||||||
# Initialize helper selector
|
|
||||||
instance = kwargs.get('instance')
|
|
||||||
if instance and instance.tenant is not None:
|
|
||||||
initial = kwargs.get('initial', {}).copy()
|
|
||||||
initial['tenant_group'] = instance.tenant.group
|
|
||||||
kwargs['initial'] = initial
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class TenancyFilterForm(forms.Form):
|
class TenancyFilterForm(forms.Form):
|
||||||
tenant_group = DynamicModelMultipleChoiceField(
|
tenant_group = DynamicModelMultipleChoiceField(
|
||||||
|
@ -309,7 +309,8 @@ class DynamicModelChoiceMixin:
|
|||||||
value = form.initial.get(child_field.lstrip('$'))
|
value = form.initial.get(child_field.lstrip('$'))
|
||||||
if value:
|
if value:
|
||||||
filter_kwargs[kwarg] = value
|
filter_kwargs[kwarg] = value
|
||||||
self.initial = self.queryset.filter(**filter_kwargs).first()
|
if filter_kwargs:
|
||||||
|
self.initial = self.queryset.filter(**filter_kwargs).first()
|
||||||
|
|
||||||
# Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options
|
# Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options
|
||||||
# will be populated on-demand via the APISelect widget.
|
# will be populated on-demand via the APISelect widget.
|
||||||
|
@ -266,7 +266,10 @@ class VirtualMachineForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|||||||
cluster_group = DynamicModelChoiceField(
|
cluster_group = DynamicModelChoiceField(
|
||||||
queryset=ClusterGroup.objects.all(),
|
queryset=ClusterGroup.objects.all(),
|
||||||
required=False,
|
required=False,
|
||||||
null_option='None'
|
null_option='None',
|
||||||
|
initial_params={
|
||||||
|
'clusters': '$cluster'
|
||||||
|
}
|
||||||
)
|
)
|
||||||
cluster = DynamicModelChoiceField(
|
cluster = DynamicModelChoiceField(
|
||||||
queryset=Cluster.objects.all(),
|
queryset=Cluster.objects.all(),
|
||||||
@ -311,14 +314,6 @@ class VirtualMachineForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
||||||
# Initialize helper selector
|
|
||||||
instance = kwargs.get('instance')
|
|
||||||
if instance.pk and instance.cluster is not None:
|
|
||||||
initial = kwargs.get('initial', {}).copy()
|
|
||||||
initial['cluster_group'] = instance.cluster.group
|
|
||||||
kwargs['initial'] = initial
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
if self.instance.pk:
|
if self.instance.pk:
|
||||||
|
Loading…
Reference in New Issue
Block a user