From 09b6b1c39a80e65250cf332b07709f36cbad5e0d Mon Sep 17 00:00:00 2001 From: Dillon Henschen Date: Tue, 2 May 2023 23:35:25 -0400 Subject: [PATCH] 11670: Add optional weight to DeviceType import This is 1 of 2 commits to address issue #11670 To maintain consistency, the import design of the DeviceType weight follows the same pattern used for importing weight and weight units in DCIM Racks. --- netbox/dcim/forms/bulk_import.py | 13 ++++++++- netbox/dcim/models/devices.py | 2 ++ netbox/dcim/tests/test_views.py | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index 8b7bd47ea..1d0307ebb 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -293,11 +293,22 @@ class DeviceTypeImportForm(NetBoxModelImportForm): help_text=_('The default platform for devices of this type (optional)') ) + weight = forms.DecimalField( + required=False, + help_text=_("Device weight"), + ) + + weight_unit = CSVChoiceField( + choices=WeightUnitChoices, + required=False, + help_text=_('Unit for device weight') + ) + class Meta: model = DeviceType fields = [ 'manufacturer', 'default_platform', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', - 'subdevice_role', 'airflow', 'description', 'comments', + 'subdevice_role', 'airflow', 'description', 'comments', 'weight', 'weight_unit', ] diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 02c68c10a..727d7f3c3 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -184,6 +184,8 @@ class DeviceType(PrimaryModel, WeightMixin): 'subdevice_role': self.subdevice_role, 'airflow': self.airflow, 'comments': self.comments, + 'weight': float(self.weight) if self.weight is not None else None, + 'weight_unit': self.weight_unit, } # Component templates diff --git a/netbox/dcim/tests/test_views.py b/netbox/dcim/tests/test_views.py index bae5a8e0b..9b762ae3b 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -852,6 +852,56 @@ inventory-items: ii1 = InventoryItemTemplate.objects.first() self.assertEqual(ii1.name, 'Inventory Item 1') + @override_settings(EXEMPT_VIEW_PERMISSIONS=['*']) + def test_import_devicetype_with_weight(self): + """ + Custom import test for JSON-based imports specifically including device weight + """ + IMPORT_DATA = """ +{ + "manufacturer": "Manufacturer 1", + "model": "ABCDEFG", + "slug": "abcdefg", + "u_height": 1, + "is_full_depth": false, + "airflow": "front-to-rear", + "description": "DeviceType with weight", + "weight": 10, + "weight_unit": "kg" +} +""" + # Add all required permissions to the test user + self.add_permissions( + 'dcim.view_devicetype', + 'dcim.add_devicetype', + 'dcim.add_consoleporttemplate', + 'dcim.add_consoleserverporttemplate', + 'dcim.add_powerporttemplate', + 'dcim.add_poweroutlettemplate', + 'dcim.add_interfacetemplate', + 'dcim.add_frontporttemplate', + 'dcim.add_rearporttemplate', + 'dcim.add_modulebaytemplate', + 'dcim.add_devicebaytemplate', + 'dcim.add_inventoryitemtemplate', + ) + + form_data = { + 'data': IMPORT_DATA, + 'format': 'json' + } + response = self.client.post(reverse('dcim:devicetype_import'), data=form_data, follow=True) + self.assertHttpStatus(response, 200) + + device_type = DeviceType.objects.get(model='ABCDEFG') + self.assertEqual(device_type.slug, "abcdefg") + self.assertEqual(device_type.u_height, 1) + self.assertFalse(device_type.is_full_depth) + self.assertEqual(device_type.airflow, DeviceAirflowChoices.AIRFLOW_FRONT_TO_REAR) + self.assertEqual(device_type.description, 'DeviceType with weight') + self.assertEqual(device_type.weight, 10) + self.assertEqual(device_type.weight_unit, WeightUnitChoices.UNIT_KILOGRAM) + def test_export_objects(self): url = reverse('dcim:devicetype_list') self.add_permissions('dcim.view_devicetype')