9654 add weight fields to devices

This commit is contained in:
Arthur 2022-09-22 19:54:23 -07:00
parent 81f0992ace
commit f55cd6388a
4 changed files with 26 additions and 8 deletions

View File

@ -1328,7 +1328,7 @@ class DeviceWeightUnitChoices(ChoiceSet):
(UNIT_KILOGRAM, 'Kilograms'),
(UNIT_GRAM, 'Grams'),
(UNIT_POUND, 'Pounds'),
(UNIT_OUNCE, 'Ounce'),
(UNIT_OUNCE, 'Ounces'),
)

View File

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

View File

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

View File

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