From f62b2c7552c5af8d696b898111fab6127d6205ef Mon Sep 17 00:00:00 2001 From: Renato Almeida de Oliveira Zaroubin Date: Tue, 11 Mar 2025 22:46:19 +0000 Subject: [PATCH 1/4] Refactor InventoryItemImportForm clean method --- netbox/dcim/forms/bulk_import.py | 44 +++++++++++++++++++------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index 92f7220da91..d6135098777 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -1161,27 +1161,35 @@ def __init__(self, *args, **kwargs): else: self.fields['parent'].queryset = InventoryItem.objects.none() - def clean_component_name(self): - content_type = self.cleaned_data.get('component_type') - component_name = self.cleaned_data.get('component_name') + def clean(self): + cleaned_data = self.cleaned_data + content_type = cleaned_data.get('component_type') + component_name = cleaned_data.get('component_name') device = self.cleaned_data.get("device") - if not device and hasattr(self, 'instance') and hasattr(self.instance, 'device'): - device = self.instance.device - - if not all([device, content_type, component_name]): - return None - - model = content_type.model_class() - try: - component = model.objects.get(device=device, name=component_name) - self.instance.component = component - except ObjectDoesNotExist: - raise forms.ValidationError( - _("Component not found: {device} - {component_name}").format( - device=device, component_name=component_name + if content_type: + if device is None: + cleaned_data.pop('component_type') + if component_name is None: + cleaned_data.pop('component_type') + raise forms.ValidationError( + _("Component name must be specified when component type is specified") ) - ) + if all([device, component_name]): + try: + model = content_type.model_class() + component = model.objects.get(device=device, name=component_name) + self.instance.component = component + except ObjectDoesNotExist: + cleaned_data.pop('component_type') + cleaned_data.pop('component_name') + raise forms.ValidationError( + _("Component not found: {device} - {component_name}").format( + device=device, component_name=component_name + ) + ) + + return cleaned_data # From ef9886b618255b9d4c7d90f379dda5fa0625b380 Mon Sep 17 00:00:00 2001 From: Renato Almeida de Oliveira Zaroubin Date: Fri, 14 Mar 2025 20:56:21 +0000 Subject: [PATCH 2/4] Add super().clean(); renamed content_type; simplified component creation --- netbox/dcim/forms/bulk_import.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index d6135098777..2f2c66c032f 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -1162,12 +1162,13 @@ def __init__(self, *args, **kwargs): self.fields['parent'].queryset = InventoryItem.objects.none() def clean(self): + super().clean() 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') device = self.cleaned_data.get("device") - if content_type: + if component_type: if device is None: cleaned_data.pop('component_type') if component_name is None: @@ -1177,9 +1178,8 @@ def clean(self): ) if all([device, component_name]): try: - model = content_type.model_class() - component = model.objects.get(device=device, name=component_name) - self.instance.component = component + model = component_type.model_class() + self.instance.component = model.objects.get(device=device, name=component_name) except ObjectDoesNotExist: cleaned_data.pop('component_type') cleaned_data.pop('component_name') @@ -1188,7 +1188,11 @@ def clean(self): 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 From d327147ccf2acd580fd58f873af1fde32d4131b5 Mon Sep 17 00:00:00 2001 From: Renato Almeida de Oliveira Zaroubin Date: Wed, 19 Mar 2025 00:21:43 +0000 Subject: [PATCH 3/4] Fix missing component_name issue --- netbox/dcim/forms/bulk_import.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index 2f2c66c032f..d7b7d7d475d 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -1164,15 +1164,15 @@ def __init__(self, *args, **kwargs): def clean(self): super().clean() cleaned_data = self.cleaned_data - component_type = cleaned_data.get('component_type') - component_name = cleaned_data.get('component_name') + component_type = cleaned_data.get('component_type', None) + component_name = cleaned_data.get('component_name', None) device = self.cleaned_data.get("device") if component_type: if device is None: - cleaned_data.pop('component_type') + cleaned_data.pop('component_type', None) if component_name is None: - cleaned_data.pop('component_type') + cleaned_data.pop('component_type', None) raise forms.ValidationError( _("Component name must be specified when component type is specified") ) @@ -1181,13 +1181,19 @@ def clean(self): model = component_type.model_class() self.instance.component = model.objects.get(device=device, name=component_name) except ObjectDoesNotExist: - cleaned_data.pop('component_type') - cleaned_data.pop('component_name') + cleaned_data.pop('component_type', None) + cleaned_data.pop('component_name', None) raise forms.ValidationError( _("Component not found: {device} - {component_name}").format( device=device, component_name=component_name ) ) + else: + cleaned_data.pop('component_type', None) + if not component_name: + raise forms.ValidationError( + _("Component name must be specified when component type is specified") + ) else: if component_name: raise forms.ValidationError( From c8f82c23afabf36d4dbcbf135e9be17476fd6910 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 19 Mar 2025 09:34:08 -0400 Subject: [PATCH 4/4] Update netbox/dcim/forms/bulk_import.py --- netbox/dcim/forms/bulk_import.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index d7b7d7d475d..cb36e94bfe8 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -1164,8 +1164,8 @@ def __init__(self, *args, **kwargs): def clean(self): super().clean() cleaned_data = self.cleaned_data - component_type = cleaned_data.get('component_type', None) - component_name = cleaned_data.get('component_name', None) + component_type = cleaned_data.get('component_type') + component_name = cleaned_data.get('component_name') device = self.cleaned_data.get("device") if component_type: