1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fix YAML import for ModuleTypes

This commit is contained in:
jeremystretch
2021-12-17 13:28:17 -05:00
parent 7c60e3c0ff
commit 5bd223a468
3 changed files with 37 additions and 24 deletions

View File

@@ -50,23 +50,21 @@ class ModuleTypeImportForm(BootstrapMixin, forms.ModelForm):
class ComponentTemplateImportForm(BootstrapMixin, forms.ModelForm):
def __init__(self, device_type, data=None, *args, **kwargs):
# Must pass the parent DeviceType on form initialization
data.update({
'device_type': device_type.pk,
})
super().__init__(data, *args, **kwargs)
def clean_device_type(self):
data = self.cleaned_data['device_type']
# Limit fields referencing other components to the parent DeviceType
for field_name, field in self.fields.items():
if isinstance(field, forms.ModelChoiceField) and field_name != 'device_type':
field.queryset = field.queryset.filter(device_type=data)
if data := self.cleaned_data['device_type']:
for field_name, field in self.fields.items():
if isinstance(field, forms.ModelChoiceField) and field_name not in ['device_type', 'module_type']:
field.queryset = field.queryset.filter(device_type=data)
return data
def clean_module_type(self):
# Limit fields referencing other components to the parent ModuleType
if data := self.cleaned_data['module_type']:
for field_name, field in self.fields.items():
if isinstance(field, forms.ModelChoiceField) and field_name not in ['device_type', 'module_type']:
field.queryset = field.queryset.filter(module_type=data)
return data
@@ -76,7 +74,7 @@ class ConsolePortTemplateImportForm(ComponentTemplateImportForm):
class Meta:
model = ConsolePortTemplate
fields = [
'device_type', 'name', 'label', 'type', 'description',
'device_type', 'module_type', 'name', 'label', 'type', 'description',
]
@@ -85,7 +83,7 @@ class ConsoleServerPortTemplateImportForm(ComponentTemplateImportForm):
class Meta:
model = ConsoleServerPortTemplate
fields = [
'device_type', 'name', 'label', 'type', 'description',
'device_type', 'module_type', 'name', 'label', 'type', 'description',
]
@@ -94,7 +92,7 @@ class PowerPortTemplateImportForm(ComponentTemplateImportForm):
class Meta:
model = PowerPortTemplate
fields = [
'device_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
]
@@ -108,7 +106,7 @@ class PowerOutletTemplateImportForm(ComponentTemplateImportForm):
class Meta:
model = PowerOutletTemplate
fields = [
'device_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description',
'device_type', 'module_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description',
]
@@ -120,7 +118,7 @@ class InterfaceTemplateImportForm(ComponentTemplateImportForm):
class Meta:
model = InterfaceTemplate
fields = [
'device_type', 'name', 'label', 'type', 'mgmt_only', 'description',
'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'description',
]
@@ -136,7 +134,7 @@ class FrontPortTemplateImportForm(ComponentTemplateImportForm):
class Meta:
model = FrontPortTemplate
fields = [
'device_type', 'name', 'type', 'rear_port', 'rear_port_position', 'label', 'description',
'device_type', 'module_type', 'name', 'type', 'rear_port', 'rear_port_position', 'label', 'description',
]
@@ -148,7 +146,7 @@ class RearPortTemplateImportForm(ComponentTemplateImportForm):
class Meta:
model = RearPortTemplate
fields = [
'device_type', 'name', 'type', 'positions', 'label', 'description',
'device_type', 'module_type', 'name', 'type', 'positions', 'label', 'description',
]