Add super().clean(); renamed content_type; simplified component creation

This commit is contained in:
Renato Almeida de Oliveira Zaroubin 2025-03-14 20:56:21 +00:00
parent f62b2c7552
commit ef9886b618

View File

@ -1162,12 +1162,13 @@ class InventoryItemImportForm(NetBoxModelImportForm):
self.fields['parent'].queryset = InventoryItem.objects.none() self.fields['parent'].queryset = InventoryItem.objects.none()
def clean(self): def clean(self):
super().clean()
cleaned_data = self.cleaned_data cleaned_data = self.cleaned_data
content_type = cleaned_data.get('component_type') component_type = cleaned_data.get('component_type')
component_name = cleaned_data.get('component_name') component_name = cleaned_data.get('component_name')
device = self.cleaned_data.get("device") device = self.cleaned_data.get("device")
if content_type: if component_type:
if device is None: if device is None:
cleaned_data.pop('component_type') cleaned_data.pop('component_type')
if component_name is None: if component_name is None:
@ -1177,9 +1178,8 @@ class InventoryItemImportForm(NetBoxModelImportForm):
) )
if all([device, component_name]): if all([device, component_name]):
try: try:
model = content_type.model_class() model = component_type.model_class()
component = model.objects.get(device=device, name=component_name) self.instance.component = model.objects.get(device=device, name=component_name)
self.instance.component = component
except ObjectDoesNotExist: except ObjectDoesNotExist:
cleaned_data.pop('component_type') cleaned_data.pop('component_type')
cleaned_data.pop('component_name') cleaned_data.pop('component_name')
@ -1188,7 +1188,11 @@ class InventoryItemImportForm(NetBoxModelImportForm):
device=device, component_name=component_name device=device, component_name=component_name
) )
) )
else:
if component_name:
raise forms.ValidationError(
_("Component type must be specified when component name is specified")
)
return cleaned_data return cleaned_data