From c776482228652f20d55563a7cd9286408b0a657b Mon Sep 17 00:00:00 2001 From: Dillon Henschen Date: Thu, 4 May 2023 01:21:03 -0400 Subject: [PATCH] Closes #11670: Add weight to ModuleType import This is commit 2 of 2 to address and close #11670. To maintain consistency, the import design of the ModuleType 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 | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index 1d0307ebb..5906e7395 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -318,9 +318,20 @@ class ModuleTypeImportForm(NetBoxModelImportForm): to_field_name='name' ) + weight = forms.DecimalField( + required=False, + help_text=_("Module weight"), + ) + + weight_unit = CSVChoiceField( + choices=WeightUnitChoices, + required=False, + help_text=_('Unit for module weight') + ) + class Meta: model = ModuleType - fields = ['manufacturer', 'model', 'part_number', 'description', 'comments'] + fields = ['manufacturer', 'model', 'part_number', 'description', 'comments', 'weight', 'weight_unit'] class DeviceRoleImportForm(NetBoxModelImportForm): diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 727d7f3c3..85a5d6870 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -363,6 +363,8 @@ class ModuleType(PrimaryModel, WeightMixin): 'model': self.model, 'part_number': self.part_number, '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 9b762ae3b..e286ae370 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -1196,6 +1196,46 @@ front-ports: self.assertEqual(fp1.rear_port, rp1) self.assertEqual(fp1.rear_port_position, 1) + @override_settings(EXEMPT_VIEW_PERMISSIONS=['*']) + def test_import_moduletype_with_weight(self): + """ + Custom import test for JSON-based imports specifically including module weight + """ + IMPORT_DATA = """ +{ + "manufacturer": "Manufacturer 2", + "model": "TEST-1001", + "weight": 10, + "weight_unit": "lb" +} +""" + + # Add all required permissions to the test user + self.add_permissions( + 'dcim.view_moduletype', + 'dcim.add_moduletype', + 'dcim.add_consoleporttemplate', + 'dcim.add_consoleserverporttemplate', + 'dcim.add_powerporttemplate', + 'dcim.add_poweroutlettemplate', + 'dcim.add_interfacetemplate', + 'dcim.add_frontporttemplate', + 'dcim.add_rearporttemplate', + ) + + form_data = { + 'data': IMPORT_DATA, + 'format': 'json' + } + response = self.client.post(reverse('dcim:moduletype_import'), data=form_data, follow=True) + self.assertHttpStatus(response, 200) + + module_type = ModuleType.objects.get(model='TEST-1001') + + # Verify the weight is correct + self.assertEqual(module_type.weight, 10) + self.assertEqual(module_type.weight_unit, WeightUnitChoices.UNIT_POUND) + def test_export_objects(self): url = reverse('dcim:moduletype_list') self.add_permissions('dcim.view_moduletype')