Added length and length_unit fields to Cable

This commit is contained in:
Jeremy Stretch
2018-10-26 10:28:25 -04:00
parent 2d90fc608e
commit 6bea8cc546
9 changed files with 97 additions and 8 deletions

View File

@@ -5,6 +5,8 @@ import six
from django.core.serializers import serialize
from django.http import HttpResponse
from dcim.constants import LENGTH_UNIT_CENTIMETER, LENGTH_UNIT_FOOT, LENGTH_UNIT_INCH, LENGTH_UNIT_METER
def csv_format(data):
"""
@@ -107,3 +109,21 @@ def serialize_object(obj, extra=None):
data.update(extra)
return data
def to_meters(length, unit):
"""
Convert the given length to meters.
"""
length = int(length)
if length < 0:
raise ValueError("Length must be a positive integer")
if unit == LENGTH_UNIT_METER:
return length
if unit == LENGTH_UNIT_CENTIMETER:
return length / 100
if unit == LENGTH_UNIT_FOOT:
return length * 0.3048
if unit == LENGTH_UNIT_INCH:
return length * 0.3048 * 12
raise ValueError("Unknown unit {}. Must be 'm', 'cm', 'ft', or 'in'.".format(unit))