Skip to content

Commit

Permalink
Closes #5806: Add kilometer and mile as choices for cable length unit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed May 10, 2021
1 parent 22e484e commit a6d937b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/release-notes/version-2.12.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions netbox/dcim/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
Expand Down
6 changes: 5 additions & 1 deletion netbox/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a6d937b

Please sign in to comment.