From f55cd6388ada7ec07004c4ded0fba3b6e478ac01 Mon Sep 17 00:00:00 2001 From: Arthur Date: Thu, 22 Sep 2022 19:54:23 -0700 Subject: [PATCH] 9654 add weight fields to devices --- netbox/dcim/choices.py | 2 +- netbox/dcim/forms/bulk_edit.py | 13 ++++++++++++- netbox/dcim/forms/models.py | 14 ++++++++++---- netbox/dcim/models/racks.py | 5 +++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/netbox/dcim/choices.py b/netbox/dcim/choices.py index 38720a614..d0aab4068 100644 --- a/netbox/dcim/choices.py +++ b/netbox/dcim/choices.py @@ -1328,7 +1328,7 @@ class DeviceWeightUnitChoices(ChoiceSet): (UNIT_KILOGRAM, 'Kilograms'), (UNIT_GRAM, 'Grams'), (UNIT_POUND, 'Pounds'), - (UNIT_OUNCE, 'Ounce'), + (UNIT_OUNCE, 'Ounces'), ) diff --git a/netbox/dcim/forms/bulk_edit.py b/netbox/dcim/forms/bulk_edit.py index 396f7e59b..534e76652 100644 --- a/netbox/dcim/forms/bulk_edit.py +++ b/netbox/dcim/forms/bulk_edit.py @@ -355,12 +355,23 @@ class DeviceTypeBulkEditForm(NetBoxModelBulkEditForm): required=False, widget=StaticSelect() ) + weight = forms.DecimalField( + min_value=0, + required=False + ) + weight_unit = forms.ChoiceField( + choices=add_blank_choice(DeviceWeightUnitChoices), + required=False, + initial='', + widget=StaticSelect() + ) model = DeviceType fieldsets = ( (None, ('manufacturer', 'part_number', 'u_height', 'is_full_depth', 'airflow')), + ('Attributes', ('weight', 'weight_unit')), ) - nullable_fields = ('part_number', 'airflow') + nullable_fields = ('part_number', 'airflow', 'weight') class ModuleTypeBulkEditForm(NetBoxModelBulkEditForm): diff --git a/netbox/dcim/forms/models.py b/netbox/dcim/forms/models.py index 5728e7f2d..edcbc29c3 100644 --- a/netbox/dcim/forms/models.py +++ b/netbox/dcim/forms/models.py @@ -363,6 +363,7 @@ class DeviceTypeForm(NetBoxModelForm): ('Chassis', ( 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', )), + ('Attributes', ('weight', 'weight_unit')), ('Images', ('front_image', 'rear_image')), ) @@ -370,7 +371,7 @@ class DeviceTypeForm(NetBoxModelForm): model = DeviceType fields = [ 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'subdevice_role', 'airflow', - 'front_image', 'rear_image', 'comments', 'tags', + 'weight', 'weight_unit', 'front_image', 'rear_image', 'comments', 'tags', ] widgets = { 'subdevice_role': StaticSelect(), @@ -379,7 +380,8 @@ class DeviceTypeForm(NetBoxModelForm): }), 'rear_image': ClearableFileInput(attrs={ 'accept': DEVICETYPE_IMAGE_FORMATS - }) + }), + 'weight_unit': StaticSelect(), } @@ -391,16 +393,20 @@ class ModuleTypeForm(NetBoxModelForm): fieldsets = ( ('Module Type', ( - 'manufacturer', 'model', 'part_number', 'tags', + 'manufacturer', 'model', 'part_number', 'tags', 'weight', 'weight_unit' )), ) class Meta: model = ModuleType fields = [ - 'manufacturer', 'model', 'part_number', 'comments', 'tags', + 'manufacturer', 'model', 'part_number', 'comments', 'tags', 'weight', 'weight_unit' ] + widgets = { + 'weight_unit': StaticSelect(), + } + class DeviceRoleForm(NetBoxModelForm): slug = SlugField() diff --git a/netbox/dcim/models/racks.py b/netbox/dcim/models/racks.py index 74f63a730..0a34681e0 100644 --- a/netbox/dcim/models/racks.py +++ b/netbox/dcim/models/racks.py @@ -19,7 +19,7 @@ from utilities.choices import ColorChoices from utilities.fields import ColorField, NaturalOrderingField from utilities.utils import array_to_string, drange from .device_components import PowerOutlet, PowerPort -from .devices import Device +from .devices import Device, Module from .mixins import DeviceWeightMixin from .power import PowerFeed @@ -451,7 +451,8 @@ class Rack(NetBoxModel, DeviceWeightMixin): return int(allocated_draw / available_power_total * 100) def get_total_weight(self): - total_weight = sum(device._abs_weight for device in self.devices.exclude(_abs_weight__isnull=True)) + total_weight = sum(device.device_type._abs_weight for device in self.devices.exclude(device_type___abs_weight__isnull=True).prefetch_related('device_type')) + total_weight += sum(module.module_type._abs_weight for module in Module.objects.filter(device=self).exclude(module_type___abs_weight__isnull=True).prefetch_related('module_type')) total_weight += self._abs_weight return total_weight