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.
This commit is contained in:
Dillon Henschen 2023-05-04 01:21:03 -04:00
parent 09b6b1c39a
commit c776482228
3 changed files with 54 additions and 1 deletions

View File

@ -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):

View File

@ -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

View File

@ -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')