Forráskód Böngészése

Closes #5806: Add kilometer and mile as choices for cable length unit

jeremystretch 4 éve
szülő
commit
a6d937b366

+ 4 - 0
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

+ 7 - 0
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'),
     )

+ 5 - 1
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):