Fixes: #18038 - Ensure DeviceType._abs_weight is stored as an integer (#18039)

* Coerce _abs_weight to int to prevent disagreement with PositiveBigIntegerField deserialization

* Perform coercion in to_grams
This commit is contained in:
bctiemann 2024-11-20 14:33:50 -05:00 committed by GitHub
parent 13bd2ed767
commit e36f23ed03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,9 +10,9 @@ __all__ = (
) )
def to_grams(weight, unit): def to_grams(weight, unit) -> int:
""" """
Convert the given weight to kilograms. Convert the given weight to integer grams.
""" """
try: try:
if weight < 0: if weight < 0:
@ -21,13 +21,13 @@ def to_grams(weight, unit):
raise TypeError(_("Invalid value '{weight}' for weight (must be a number)").format(weight=weight)) raise TypeError(_("Invalid value '{weight}' for weight (must be a number)").format(weight=weight))
if unit == WeightUnitChoices.UNIT_KILOGRAM: if unit == WeightUnitChoices.UNIT_KILOGRAM:
return weight * 1000 return int(weight * 1000)
if unit == WeightUnitChoices.UNIT_GRAM: if unit == WeightUnitChoices.UNIT_GRAM:
return weight return int(weight)
if unit == WeightUnitChoices.UNIT_POUND: if unit == WeightUnitChoices.UNIT_POUND:
return weight * Decimal(453.592) return int(weight * Decimal(453.592))
if unit == WeightUnitChoices.UNIT_OUNCE: if unit == WeightUnitChoices.UNIT_OUNCE:
return weight * Decimal(28.3495) return int(weight * Decimal(28.3495))
raise ValueError( raise ValueError(
_("Unknown unit {unit}. Must be one of the following: {valid_units}").format( _("Unknown unit {unit}. Must be one of the following: {valid_units}").format(
unit=unit, unit=unit,