mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
9654 device weight (#10448)
* 9654 add weight fields to devices * 9654 changes from code review * 9654 change _abs_weight to grams * Resolve migrations conflict * 9654 code-review changes * 9654 total weight on devices * Misc cleanup Co-authored-by: Jeremy Stretch <jstretch@ns1.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import decimal
|
||||
from functools import cached_property
|
||||
|
||||
from django.apps import apps
|
||||
from django.contrib.auth.models import User
|
||||
@@ -18,7 +19,8 @@ from utilities.choices import ColorChoices
|
||||
from utilities.fields import ColorField, NaturalOrderingField
|
||||
from utilities.utils import array_to_string, drange
|
||||
from .device_components import PowerPort
|
||||
from .devices import Device
|
||||
from .devices import Device, Module
|
||||
from .mixins import WeightMixin
|
||||
from .power import PowerFeed
|
||||
|
||||
__all__ = (
|
||||
@@ -62,7 +64,7 @@ class RackRole(OrganizationalModel):
|
||||
return reverse('dcim:rackrole', args=[self.pk])
|
||||
|
||||
|
||||
class Rack(NetBoxModel):
|
||||
class Rack(NetBoxModel, WeightMixin):
|
||||
"""
|
||||
Devices are housed within Racks. Each rack has a defined height measured in rack units, and a front and rear face.
|
||||
Each Rack is assigned to a Site and (optionally) a Location.
|
||||
@@ -185,7 +187,7 @@ class Rack(NetBoxModel):
|
||||
|
||||
clone_fields = (
|
||||
'site', 'location', 'tenant', 'status', 'role', 'type', 'width', 'u_height', 'desc_units', 'outer_width',
|
||||
'outer_depth', 'outer_unit',
|
||||
'outer_depth', 'outer_unit', 'weight', 'weight_unit',
|
||||
)
|
||||
|
||||
class Meta:
|
||||
@@ -454,6 +456,22 @@ class Rack(NetBoxModel):
|
||||
|
||||
return int(allocated_draw / available_power_total * 100)
|
||||
|
||||
@cached_property
|
||||
def total_weight(self):
|
||||
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__rack=self)
|
||||
.exclude(module_type___abs_weight__isnull=True)
|
||||
.prefetch_related('module_type')
|
||||
)
|
||||
if self._abs_weight:
|
||||
total_weight += self._abs_weight
|
||||
return round(total_weight / 1000, 2)
|
||||
|
||||
|
||||
class RackReservation(NetBoxModel):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user