mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 08:25:17 -06:00
9654 change _abs_weight to grams
This commit is contained in:
parent
b049678457
commit
f964c3f35e
@ -13,7 +13,7 @@ class Migration(migrations.Migration):
|
|||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='devicetype',
|
model_name='devicetype',
|
||||||
name='_abs_weight',
|
name='_abs_weight',
|
||||||
field=models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
|
field=models.PositiveBigIntegerField(blank=True, null=True),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='devicetype',
|
model_name='devicetype',
|
||||||
@ -28,7 +28,7 @@ class Migration(migrations.Migration):
|
|||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='moduletype',
|
model_name='moduletype',
|
||||||
name='_abs_weight',
|
name='_abs_weight',
|
||||||
field=models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
|
field=models.PositiveBigIntegerField(blank=True, null=True),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='moduletype',
|
model_name='moduletype',
|
||||||
@ -43,7 +43,7 @@ class Migration(migrations.Migration):
|
|||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='rack',
|
model_name='rack',
|
||||||
name='_abs_weight',
|
name='_abs_weight',
|
||||||
field=models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
|
field=models.PositiveBigIntegerField(blank=True, null=True),
|
||||||
),
|
),
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='rack',
|
model_name='rack',
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from dcim.choices import *
|
from dcim.choices import *
|
||||||
from utilities.utils import to_kilograms
|
from utilities.utils import to_grams
|
||||||
|
|
||||||
|
|
||||||
class WeightMixin(models.Model):
|
class WeightMixin(models.Model):
|
||||||
@ -16,10 +16,8 @@ class WeightMixin(models.Model):
|
|||||||
choices=WeightUnitChoices,
|
choices=WeightUnitChoices,
|
||||||
blank=True,
|
blank=True,
|
||||||
)
|
)
|
||||||
# Stores the normalized weight (in kilograms) for database ordering
|
# Stores the normalized weight (in grams) for database ordering
|
||||||
_abs_weight = models.DecimalField(
|
_abs_weight = models.PositiveBigIntegerField(
|
||||||
max_digits=10,
|
|
||||||
decimal_places=2,
|
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True
|
null=True
|
||||||
)
|
)
|
||||||
@ -29,9 +27,9 @@ class WeightMixin(models.Model):
|
|||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
||||||
# Store the given weight (if any) in kilograms for use in database ordering
|
# Store the given weight (if any) in grams for use in database ordering
|
||||||
if self.weight and self.weight_unit:
|
if self.weight and self.weight_unit:
|
||||||
self._abs_weight = to_kilograms(self.weight, self.weight_unit)
|
self._abs_weight = to_grams(self.weight, self.weight_unit)
|
||||||
else:
|
else:
|
||||||
self._abs_weight = None
|
self._abs_weight = None
|
||||||
|
|
||||||
|
@ -455,7 +455,7 @@ class Rack(NetBoxModel, WeightMixin):
|
|||||||
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'))
|
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:
|
if self._abs_weight:
|
||||||
total_weight += self._abs_weight
|
total_weight += self._abs_weight
|
||||||
return round(total_weight, 2)
|
return round(total_weight / 1000, 2)
|
||||||
|
|
||||||
|
|
||||||
class RackReservation(NetBoxModel):
|
class RackReservation(NetBoxModel):
|
||||||
|
@ -270,7 +270,7 @@ def to_meters(length, unit):
|
|||||||
raise ValueError(f"Unknown unit {unit}. Must be 'km', 'm', 'cm', 'mi', 'ft', or 'in'.")
|
raise ValueError(f"Unknown unit {unit}. Must be 'km', 'm', 'cm', 'mi', 'ft', or 'in'.")
|
||||||
|
|
||||||
|
|
||||||
def to_kilograms(weight, unit):
|
def to_grams(weight, unit):
|
||||||
"""
|
"""
|
||||||
Convert the given weight to kilograms.
|
Convert the given weight to kilograms.
|
||||||
"""
|
"""
|
||||||
@ -285,13 +285,13 @@ def to_kilograms(weight, unit):
|
|||||||
raise ValueError(f"Unknown unit {unit}. Must be one of the following: {', '.join(valid_units)}")
|
raise ValueError(f"Unknown unit {unit}. Must be one of the following: {', '.join(valid_units)}")
|
||||||
|
|
||||||
if unit == WeightUnitChoices.UNIT_KILOGRAM:
|
if unit == WeightUnitChoices.UNIT_KILOGRAM:
|
||||||
return weight
|
|
||||||
if unit == WeightUnitChoices.UNIT_GRAM:
|
|
||||||
return weight * 1000
|
return weight * 1000
|
||||||
|
if unit == WeightUnitChoices.UNIT_GRAM:
|
||||||
|
return weight
|
||||||
if unit == WeightUnitChoices.UNIT_POUND:
|
if unit == WeightUnitChoices.UNIT_POUND:
|
||||||
return weight * Decimal(0.453592)
|
return weight * Decimal(453.592)
|
||||||
if unit == WeightUnitChoices.UNIT_OUNCE:
|
if unit == WeightUnitChoices.UNIT_OUNCE:
|
||||||
return weight * Decimal(0.0283495)
|
return weight * Decimal(28.3495)
|
||||||
raise ValueError(f"Unknown unit {unit}. Must be 'kg', 'g', 'lb', 'oz'.")
|
raise ValueError(f"Unknown unit {unit}. Must be 'kg', 'g', 'lb', 'oz'.")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user