Skip to content

Commit c55c14e

Browse files
Closes #11670: Add ability to optionally import DeviceType and ModuleType weight (#12512)
* 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. * 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. * Merge tests; misc cleanup --------- Co-authored-by: jeremystretch <[email protected]>
1 parent e1b7a3a commit c55c14e

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

netbox/dcim/forms/bulk_import.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,21 @@ class DeviceTypeImportForm(NetBoxModelImportForm):
292292
required=False,
293293
help_text=_('The default platform for devices of this type (optional)')
294294
)
295+
weight = forms.DecimalField(
296+
required=False,
297+
help_text=_('Device weight'),
298+
)
299+
weight_unit = CSVChoiceField(
300+
choices=WeightUnitChoices,
301+
required=False,
302+
help_text=_('Unit for device weight')
303+
)
295304

296305
class Meta:
297306
model = DeviceType
298307
fields = [
299308
'manufacturer', 'default_platform', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth',
300-
'subdevice_role', 'airflow', 'description', 'comments',
309+
'subdevice_role', 'airflow', 'description', 'weight', 'weight_unit', 'comments',
301310
]
302311

303312

@@ -306,10 +315,19 @@ class ModuleTypeImportForm(NetBoxModelImportForm):
306315
queryset=Manufacturer.objects.all(),
307316
to_field_name='name'
308317
)
318+
weight = forms.DecimalField(
319+
required=False,
320+
help_text=_('Module weight'),
321+
)
322+
weight_unit = CSVChoiceField(
323+
choices=WeightUnitChoices,
324+
required=False,
325+
help_text=_('Unit for module weight')
326+
)
309327

310328
class Meta:
311329
model = ModuleType
312-
fields = ['manufacturer', 'model', 'part_number', 'description', 'comments']
330+
fields = ['manufacturer', 'model', 'part_number', 'description', 'weight', 'weight_unit', 'comments']
313331

314332

315333
class DeviceRoleImportForm(NetBoxModelImportForm):

netbox/dcim/models/devices.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ def to_yaml(self):
184184
'subdevice_role': self.subdevice_role,
185185
'airflow': self.airflow,
186186
'comments': self.comments,
187+
'weight': float(self.weight) if self.weight is not None else None,
188+
'weight_unit': self.weight_unit,
187189
}
188190

189191
# Component templates
@@ -361,6 +363,8 @@ def to_yaml(self):
361363
'model': self.model,
362364
'part_number': self.part_number,
363365
'comments': self.comments,
366+
'weight': float(self.weight) if self.weight is not None else None,
367+
'weight_unit': self.weight_unit,
364368
}
365369

366370
# Component templates

netbox/dcim/tests/test_views.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,15 @@ def test_import_objects(self):
681681
"""
682682
IMPORT_DATA = """
683683
manufacturer: Generic
684-
default_platform: Platform
685684
model: TEST-1000
686685
slug: test-1000
686+
default_platform: Platform
687687
u_height: 2
688+
is_full_depth: false
689+
airflow: front-to-rear
688690
subdevice_role: parent
691+
weight: 10
692+
weight_unit: kg
689693
comments: Test comment
690694
console-ports:
691695
- name: Console Port 1
@@ -794,8 +798,16 @@ def test_import_objects(self):
794798
self.assertHttpStatus(response, 200)
795799

796800
device_type = DeviceType.objects.get(model='TEST-1000')
797-
self.assertEqual(device_type.comments, 'Test comment')
801+
self.assertEqual(device_type.manufacturer.pk, manufacturer.pk)
798802
self.assertEqual(device_type.default_platform.pk, platform.pk)
803+
self.assertEqual(device_type.slug, 'test-1000')
804+
self.assertEqual(device_type.u_height, 2)
805+
self.assertFalse(device_type.is_full_depth)
806+
self.assertEqual(device_type.airflow, DeviceAirflowChoices.AIRFLOW_FRONT_TO_REAR)
807+
self.assertEqual(device_type.subdevice_role, SubdeviceRoleChoices.ROLE_PARENT)
808+
self.assertEqual(device_type.weight, 10)
809+
self.assertEqual(device_type.weight_unit, WeightUnitChoices.UNIT_KILOGRAM)
810+
self.assertEqual(device_type.comments, 'Test comment')
799811

800812
# Verify all of the components were created
801813
self.assertEqual(device_type.consoleporttemplates.count(), 3)
@@ -1019,6 +1031,8 @@ def test_import_objects(self):
10191031
IMPORT_DATA = """
10201032
manufacturer: Generic
10211033
model: TEST-1000
1034+
weight: 10
1035+
weight_unit: lb
10221036
comments: Test comment
10231037
console-ports:
10241038
- name: Console Port 1
@@ -1082,7 +1096,8 @@ def test_import_objects(self):
10821096
"""
10831097

10841098
# Create the manufacturer
1085-
Manufacturer(name='Generic', slug='generic').save()
1099+
manufacturer = Manufacturer(name='Generic', slug='generic')
1100+
manufacturer.save()
10861101

10871102
# Add all required permissions to the test user
10881103
self.add_permissions(
@@ -1105,6 +1120,9 @@ def test_import_objects(self):
11051120
self.assertHttpStatus(response, 200)
11061121

11071122
module_type = ModuleType.objects.get(model='TEST-1000')
1123+
self.assertEqual(module_type.manufacturer.pk, manufacturer.pk)
1124+
self.assertEqual(module_type.weight, 10)
1125+
self.assertEqual(module_type.weight_unit, WeightUnitChoices.UNIT_POUND)
11081126
self.assertEqual(module_type.comments, 'Test comment')
11091127

11101128
# Verify all the components were created

0 commit comments

Comments
 (0)