diff --git a/netbox/dcim/models/mixins.py b/netbox/dcim/models/mixins.py index 4f38bebea..d4a05699c 100644 --- a/netbox/dcim/models/mixins.py +++ b/netbox/dcim/models/mixins.py @@ -37,7 +37,7 @@ class WeightMixin(models.Model): # Store the given weight (if any) in grams for use in database ordering if self.weight and self.weight_unit: - self._abs_weight = int(to_grams(self.weight, self.weight_unit)) + self._abs_weight = to_grams(self.weight, self.weight_unit) else: self._abs_weight = None diff --git a/netbox/utilities/conversion.py b/netbox/utilities/conversion.py index be95df14f..07e57d96e 100644 --- a/netbox/utilities/conversion.py +++ b/netbox/utilities/conversion.py @@ -10,9 +10,9 @@ __all__ = ( ) -def to_grams(weight, unit) -> float: +def to_grams(weight, unit) -> int: """ - Convert the given weight to kilograms. + Convert the given weight to integer grams. """ try: if weight < 0: @@ -21,13 +21,13 @@ def to_grams(weight, unit) -> float: raise TypeError(_("Invalid value '{weight}' for weight (must be a number)").format(weight=weight)) if unit == WeightUnitChoices.UNIT_KILOGRAM: - return weight * 1000 + return int(weight * 1000) if unit == WeightUnitChoices.UNIT_GRAM: - return weight + return int(weight) if unit == WeightUnitChoices.UNIT_POUND: - return weight * Decimal(453.592) + return int(weight * Decimal(453.592)) if unit == WeightUnitChoices.UNIT_OUNCE: - return weight * Decimal(28.3495) + return int(weight * Decimal(28.3495)) raise ValueError( _("Unknown unit {unit}. Must be one of the following: {valid_units}").format( unit=unit,