1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Rack.outer_unit to slug (#3569)

This commit is contained in:
Jeremy Stretch
2019-11-25 20:54:24 -05:00
parent 4846557d61
commit 9872a46583
7 changed files with 62 additions and 24 deletions

View File

@ -119,7 +119,7 @@ class RackSerializer(TaggitSerializer, CustomFieldModelSerializer):
role = NestedRackRoleSerializer(required=False, allow_null=True) role = NestedRackRoleSerializer(required=False, allow_null=True)
type = ChoiceField(choices=RackTypeChoices, required=False, allow_null=True) type = ChoiceField(choices=RackTypeChoices, required=False, allow_null=True)
width = ChoiceField(choices=RackWidthChoices, required=False) width = ChoiceField(choices=RackWidthChoices, required=False)
outer_unit = ChoiceField(choices=RACK_DIMENSION_UNIT_CHOICES, required=False) outer_unit = ChoiceField(choices=RackDimensionUnitChoices, required=False)
tags = TagListSerializerField(required=False) tags = TagListSerializerField(required=False)
device_count = serializers.IntegerField(read_only=True) device_count = serializers.IntegerField(read_only=True)
powerfeed_count = serializers.IntegerField(read_only=True) powerfeed_count = serializers.IntegerField(read_only=True)

View File

@ -89,6 +89,22 @@ class RackStatusChoices(ChoiceSet):
} }
class RackDimensionUnitChoices(ChoiceSet):
UNIT_MILLIMETER = 'mm'
UNIT_INCH = 'in'
CHOICES = (
(UNIT_MILLIMETER, 'Millimeters'),
(UNIT_INCH, 'Inches'),
)
LEGACY_MAP = {
UNIT_MILLIMETER: 1000,
UNIT_INCH: 2000,
}
# #
# DeviceTypes # DeviceTypes
# #

View File

@ -67,16 +67,6 @@ COMPATIBLE_TERMINATION_TYPES = {
'circuittermination': ['interface', 'frontport', 'rearport'], 'circuittermination': ['interface', 'frontport', 'rearport'],
} }
LENGTH_UNIT_METER = 1200
LENGTH_UNIT_CENTIMETER = 1100
LENGTH_UNIT_MILLIMETER = 1000
LENGTH_UNIT_FOOT = 2100
LENGTH_UNIT_INCH = 2000
RACK_DIMENSION_UNIT_CHOICES = (
(LENGTH_UNIT_MILLIMETER, 'Millimeters'),
(LENGTH_UNIT_INCH, 'Inches'),
)
# Power feeds # Power feeds
POWERFEED_TYPE_PRIMARY = 1 POWERFEED_TYPE_PRIMARY = 1
POWERFEED_TYPE_REDUNDANT = 2 POWERFEED_TYPE_REDUNDANT = 2

View File

@ -496,7 +496,7 @@ class RackCSVForm(forms.ModelForm):
help_text='Rail-to-rail width (in inches)' help_text='Rail-to-rail width (in inches)'
) )
outer_unit = CSVChoiceField( outer_unit = CSVChoiceField(
choices=RACK_DIMENSION_UNIT_CHOICES, choices=RackDimensionUnitChoices,
required=False, required=False,
help_text='Unit for outer dimensions' help_text='Unit for outer dimensions'
) )
@ -617,7 +617,7 @@ class RackBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditFor
min_value=1 min_value=1
) )
outer_unit = forms.ChoiceField( outer_unit = forms.ChoiceField(
choices=add_blank_choice(RACK_DIMENSION_UNIT_CHOICES), choices=add_blank_choice(RackDimensionUnitChoices),
required=False, required=False,
widget=StaticSelect2() widget=StaticSelect2()
) )

View File

@ -16,6 +16,11 @@ RACK_STATUS_CHOICES = (
(4, 'deprecated'), (4, 'deprecated'),
) )
RACK_DIMENSION_CHOICES = (
(1000, 'mm'),
(2000, 'in'),
)
def rack_type_to_slug(apps, schema_editor): def rack_type_to_slug(apps, schema_editor):
Rack = apps.get_model('dcim', 'Rack') Rack = apps.get_model('dcim', 'Rack')
@ -29,6 +34,12 @@ def rack_status_to_slug(apps, schema_editor):
Rack.objects.filter(status=str(id)).update(status=slug) Rack.objects.filter(status=str(id)).update(status=slug)
def rack_outer_unit_to_slug(apps, schema_editor):
Rack = apps.get_model('dcim', 'Rack')
for id, slug in RACK_DIMENSION_CHOICES:
Rack.objects.filter(status=str(id)).update(status=slug)
class Migration(migrations.Migration): class Migration(migrations.Migration):
atomic = False atomic = False
@ -62,4 +73,19 @@ class Migration(migrations.Migration):
migrations.RunPython( migrations.RunPython(
code=rack_status_to_slug code=rack_status_to_slug
), ),
# Rack.outer_unit
migrations.AlterField(
model_name='rack',
name='outer_unit',
field=models.CharField(blank=True, default='', max_length=50),
),
migrations.RunPython(
code=rack_outer_unit_to_slug
),
migrations.AlterField(
model_name='rack',
name='outer_unit',
field=models.CharField(blank=True, max_length=50),
),
] ]

View File

@ -535,10 +535,10 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
blank=True, blank=True,
null=True null=True
) )
outer_unit = models.PositiveSmallIntegerField( outer_unit = models.CharField(
choices=RACK_DIMENSION_UNIT_CHOICES, max_length=50,
choices=RackDimensionUnitChoices,
blank=True, blank=True,
null=True
) )
comments = models.TextField( comments = models.TextField(
blank=True blank=True
@ -584,10 +584,10 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
def clean(self): def clean(self):
# Validate outer dimensions and unit # Validate outer dimensions and unit
if (self.outer_width is not None or self.outer_depth is not None) and self.outer_unit is None: if (self.outer_width is not None or self.outer_depth is not None) and not self.outer_unit:
raise ValidationError("Must specify a unit when setting an outer width/depth") raise ValidationError("Must specify a unit when setting an outer width/depth")
elif self.outer_width is None and self.outer_depth is None: elif self.outer_width is None and self.outer_depth is None:
self.outer_unit = None self.outer_unit = ''
if self.pk: if self.pk:
# Validate that Rack is tall enough to house the installed Devices # Validate that Rack is tall enough to house the installed Devices

View File

@ -5,7 +5,7 @@ from collections import OrderedDict
from django.core.serializers import serialize from django.core.serializers import serialize
from django.db.models import Count, OuterRef, Subquery from django.db.models import Count, OuterRef, Subquery
from dcim.constants import LENGTH_UNIT_CENTIMETER, LENGTH_UNIT_FOOT, LENGTH_UNIT_INCH, LENGTH_UNIT_METER from dcim.choices import CableLengthUnitChoices
def csv_format(data): def csv_format(data):
@ -165,12 +165,18 @@ def to_meters(length, unit):
length = int(length) length = int(length)
if length < 0: if length < 0:
raise ValueError("Length must be a positive integer") raise ValueError("Length must be a positive integer")
if unit == LENGTH_UNIT_METER:
valid_units = [u[0] for u in CableLengthUnitChoices]
if unit not in valid_units:
raise ValueError(
"Unknown unit {}. Must be one of the following: {}".format(unit, ', '.join(valid_units))
)
if unit == CableLengthUnitChoices.UNIT_METER:
return length return length
if unit == LENGTH_UNIT_CENTIMETER: if unit == CableLengthUnitChoices.UNIT_CENTIMETER:
return length / 100 return length / 100
if unit == LENGTH_UNIT_FOOT: if unit == CableLengthUnitChoices.UNIT_FOOT:
return length * 0.3048 return length * 0.3048
if unit == LENGTH_UNIT_INCH: if unit == CableLengthUnitChoices.UNIT_INCH:
return length * 0.3048 * 12 return length * 0.3048 * 12
raise ValueError("Unknown unit {}. Must be 'm', 'cm', 'ft', or 'in'.".format(unit))