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.
This commit is contained in:
Dillon Henschen 2023-05-02 23:35:25 -04:00
parent 9eeca06115
commit 09b6b1c39a
3 changed files with 64 additions and 1 deletions

View File

@ -293,11 +293,22 @@ class DeviceTypeImportForm(NetBoxModelImportForm):
help_text=_('The default platform for devices of this type (optional)') 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: class Meta:
model = DeviceType model = DeviceType
fields = [ fields = [
'manufacturer', 'default_platform', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', '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',
] ]

View File

@ -184,6 +184,8 @@ class DeviceType(PrimaryModel, WeightMixin):
'subdevice_role': self.subdevice_role, 'subdevice_role': self.subdevice_role,
'airflow': self.airflow, 'airflow': self.airflow,
'comments': self.comments, 'comments': self.comments,
'weight': float(self.weight) if self.weight is not None else None,
'weight_unit': self.weight_unit,
} }
# Component templates # Component templates

View File

@ -852,6 +852,56 @@ inventory-items:
ii1 = InventoryItemTemplate.objects.first() ii1 = InventoryItemTemplate.objects.first()
self.assertEqual(ii1.name, 'Inventory Item 1') 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): def test_export_objects(self):
url = reverse('dcim:devicetype_list') url = reverse('dcim:devicetype_list')
self.add_permissions('dcim.view_devicetype') self.add_permissions('dcim.view_devicetype')