9654 change _abs_weight to grams

This commit is contained in:
Arthur 2022-09-27 11:41:54 -07:00
parent b049678457
commit f964c3f35e
4 changed files with 14 additions and 16 deletions

View File

@ -13,7 +13,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='devicetype',
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(
model_name='devicetype',
@ -28,7 +28,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='moduletype',
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(
model_name='moduletype',
@ -43,7 +43,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='rack',
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(
model_name='rack',

View File

@ -1,7 +1,7 @@
from django.core.exceptions import ValidationError
from django.db import models
from dcim.choices import *
from utilities.utils import to_kilograms
from utilities.utils import to_grams
class WeightMixin(models.Model):
@ -16,10 +16,8 @@ class WeightMixin(models.Model):
choices=WeightUnitChoices,
blank=True,
)
# Stores the normalized weight (in kilograms) for database ordering
_abs_weight = models.DecimalField(
max_digits=10,
decimal_places=2,
# Stores the normalized weight (in grams) for database ordering
_abs_weight = models.PositiveBigIntegerField(
blank=True,
null=True
)
@ -29,9 +27,9 @@ class WeightMixin(models.Model):
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:
self._abs_weight = to_kilograms(self.weight, self.weight_unit)
self._abs_weight = to_grams(self.weight, self.weight_unit)
else:
self._abs_weight = None

View File

@ -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'))
if self._abs_weight:
total_weight += self._abs_weight
return round(total_weight, 2)
return round(total_weight / 1000, 2)
class RackReservation(NetBoxModel):

View File

@ -270,7 +270,7 @@ def to_meters(length, unit):
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.
"""
@ -285,13 +285,13 @@ def to_kilograms(weight, unit):
raise ValueError(f"Unknown unit {unit}. Must be one of the following: {', '.join(valid_units)}")
if unit == WeightUnitChoices.UNIT_KILOGRAM:
return weight
if unit == WeightUnitChoices.UNIT_GRAM:
return weight * 1000
if unit == WeightUnitChoices.UNIT_GRAM:
return weight
if unit == WeightUnitChoices.UNIT_POUND:
return weight * Decimal(0.453592)
return weight * Decimal(453.592)
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'.")