diff --git a/docs/release-notes/version-2.12.md b/docs/release-notes/version-2.12.md index 598237e76..c69923583 100644 --- a/docs/release-notes/version-2.12.md +++ b/docs/release-notes/version-2.12.md @@ -2,6 +2,10 @@ ## v2.12-beta1 (FUTURE) +### Enhancements + +* [#5806](https://github.com/netbox-community/netbox/issues/5806) - Add kilometer and mile as choices for cable length unit + ### Other Changes * [#5532](https://github.com/netbox-community/netbox/issues/5532) - Drop support for Python 3.6 diff --git a/netbox/dcim/choices.py b/netbox/dcim/choices.py index c5646cf2b..be35a24c5 100644 --- a/netbox/dcim/choices.py +++ b/netbox/dcim/choices.py @@ -1064,14 +1064,21 @@ class CableStatusChoices(ChoiceSet): class CableLengthUnitChoices(ChoiceSet): + # Metric + UNIT_KILOMETER = 'km' UNIT_METER = 'm' UNIT_CENTIMETER = 'cm' + + # Imperial + UNIT_MILE = 'mi' UNIT_FOOT = 'ft' UNIT_INCH = 'in' CHOICES = ( + (UNIT_KILOMETER, 'Kilometers'), (UNIT_METER, 'Meters'), (UNIT_CENTIMETER, 'Centimeters'), + (UNIT_MILE, 'Miles'), (UNIT_FOOT, 'Feet'), (UNIT_INCH, 'Inches'), ) diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py index ce6753877..a14a468b6 100644 --- a/netbox/utilities/utils.py +++ b/netbox/utilities/utils.py @@ -198,15 +198,19 @@ def to_meters(length, unit): "Unknown unit {}. Must be one of the following: {}".format(unit, ', '.join(valid_units)) ) + if unit == CableLengthUnitChoices.UNIT_KILOMETER: + return length * 1000 if unit == CableLengthUnitChoices.UNIT_METER: return length if unit == CableLengthUnitChoices.UNIT_CENTIMETER: return length / 100 + if unit == CableLengthUnitChoices.UNIT_MILE: + return length * 1609.344 if unit == CableLengthUnitChoices.UNIT_FOOT: return length * 0.3048 if unit == CableLengthUnitChoices.UNIT_INCH: return length * 0.3048 * 12 - raise ValueError("Unknown unit {}. Must be 'm', 'cm', 'ft', or 'in'.".format(unit)) + raise ValueError(f"Unknown unit {unit}. Must be 'km', 'm', 'cm', 'mi', 'ft', or 'in'.") def render_jinja2(template_code, context):