mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Move to_grams() & to_meters() from utilities.utils to utilities.conversion
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import datetime
|
||||
import decimal
|
||||
import json
|
||||
from decimal import Decimal
|
||||
from itertools import count, groupby
|
||||
from urllib.parse import urlencode
|
||||
|
||||
@ -14,11 +13,9 @@ from django.http import QueryDict
|
||||
from django.utils import timezone
|
||||
from django.utils.datastructures import MultiValueDict
|
||||
from django.utils.timezone import localtime
|
||||
from django.utils.translation import gettext as _
|
||||
from jinja2.sandbox import SandboxedEnvironment
|
||||
from mptt.models import MPTTModel
|
||||
|
||||
from dcim.choices import CableLengthUnitChoices, WeightUnitChoices
|
||||
from extras.utils import is_taggable
|
||||
from netbox.config import get_config
|
||||
from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
|
||||
@ -253,68 +250,6 @@ def drange(start, end, step=decimal.Decimal(1)):
|
||||
start += step
|
||||
|
||||
|
||||
def to_meters(length, unit):
|
||||
"""
|
||||
Convert the given length to meters.
|
||||
"""
|
||||
try:
|
||||
if length < 0:
|
||||
raise ValueError(_("Length must be a positive number"))
|
||||
except TypeError:
|
||||
raise TypeError(_("Invalid value '{length}' for length (must be a number)").format(length=length))
|
||||
|
||||
valid_units = CableLengthUnitChoices.values()
|
||||
if unit not in valid_units:
|
||||
raise ValueError(
|
||||
_("Unknown unit {unit}. Must be one of the following: {valid_units}").format(
|
||||
unit=unit, valid_units=', '.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 * Decimal(1609.344)
|
||||
if unit == CableLengthUnitChoices.UNIT_FOOT:
|
||||
return length * Decimal(0.3048)
|
||||
if unit == CableLengthUnitChoices.UNIT_INCH:
|
||||
return length * Decimal(0.0254)
|
||||
raise ValueError(_("Unknown unit {unit}. Must be 'km', 'm', 'cm', 'mi', 'ft', or 'in'.").format(unit=unit))
|
||||
|
||||
|
||||
def to_grams(weight, unit):
|
||||
"""
|
||||
Convert the given weight to kilograms.
|
||||
"""
|
||||
try:
|
||||
if weight < 0:
|
||||
raise ValueError(_("Weight must be a positive number"))
|
||||
except TypeError:
|
||||
raise TypeError(_("Invalid value '{weight}' for weight (must be a number)").format(weight=weight))
|
||||
|
||||
valid_units = WeightUnitChoices.values()
|
||||
if unit not in valid_units:
|
||||
raise ValueError(
|
||||
_("Unknown unit {unit}. Must be one of the following: {valid_units}").format(
|
||||
unit=unit, valid_units=', '.join(valid_units)
|
||||
)
|
||||
)
|
||||
|
||||
if unit == WeightUnitChoices.UNIT_KILOGRAM:
|
||||
return weight * 1000
|
||||
if unit == WeightUnitChoices.UNIT_GRAM:
|
||||
return weight
|
||||
if unit == WeightUnitChoices.UNIT_POUND:
|
||||
return weight * Decimal(453.592)
|
||||
if unit == WeightUnitChoices.UNIT_OUNCE:
|
||||
return weight * Decimal(28.3495)
|
||||
raise ValueError(_("Unknown unit {unit}. Must be 'kg', 'g', 'lb', 'oz'.").format(unit=unit))
|
||||
|
||||
|
||||
def render_jinja2(template_code, context):
|
||||
"""
|
||||
Render a Jinja2 template with the provided context. Return the rendered content.
|
||||
|
Reference in New Issue
Block a user