Cable.length_unit to slug (#3569)

This commit is contained in:
Jeremy Stretch 2019-11-25 20:40:29 -05:00
parent 79a40e22c9
commit 4846557d61
6 changed files with 61 additions and 14 deletions

View File

@ -586,7 +586,7 @@ class CableSerializer(ValidatedModelSerializer):
termination_a = serializers.SerializerMethodField(read_only=True) termination_a = serializers.SerializerMethodField(read_only=True)
termination_b = serializers.SerializerMethodField(read_only=True) termination_b = serializers.SerializerMethodField(read_only=True)
status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, required=False) status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, required=False)
length_unit = ChoiceField(choices=CABLE_LENGTH_UNIT_CHOICES, required=False, allow_null=True) length_unit = ChoiceField(choices=CableLengthUnitChoices, required=False, allow_null=True)
class Meta: class Meta:
model = Cable model = Cable

View File

@ -834,3 +834,25 @@ class CableTypeChoices(ChoiceSet):
TYPE_AOC: 3800, TYPE_AOC: 3800,
TYPE_POWER: 5000, TYPE_POWER: 5000,
} }
class CableLengthUnitChoices(ChoiceSet):
UNIT_METER = 'm'
UNIT_CENTIMETER = 'cm'
UNIT_FOOT = 'ft'
UNIT_INCH = 'in'
CHOICES = (
(UNIT_METER, 'Meters'),
(UNIT_CENTIMETER, 'Centimeters'),
(UNIT_FOOT, 'Feet'),
(UNIT_INCH, 'Inches'),
)
LEGACY_MAP = {
UNIT_METER: 1200,
UNIT_CENTIMETER: 1100,
UNIT_FOOT: 2100,
UNIT_INCH: 2000,
}

View File

@ -72,12 +72,6 @@ LENGTH_UNIT_CENTIMETER = 1100
LENGTH_UNIT_MILLIMETER = 1000 LENGTH_UNIT_MILLIMETER = 1000
LENGTH_UNIT_FOOT = 2100 LENGTH_UNIT_FOOT = 2100
LENGTH_UNIT_INCH = 2000 LENGTH_UNIT_INCH = 2000
CABLE_LENGTH_UNIT_CHOICES = (
(LENGTH_UNIT_METER, 'Meters'),
(LENGTH_UNIT_CENTIMETER, 'Centimeters'),
(LENGTH_UNIT_FOOT, 'Feet'),
(LENGTH_UNIT_INCH, 'Inches'),
)
RACK_DIMENSION_UNIT_CHOICES = ( RACK_DIMENSION_UNIT_CHOICES = (
(LENGTH_UNIT_MILLIMETER, 'Millimeters'), (LENGTH_UNIT_MILLIMETER, 'Millimeters'),
(LENGTH_UNIT_INCH, 'Inches'), (LENGTH_UNIT_INCH, 'Inches'),

View File

@ -3149,7 +3149,7 @@ class CableCSVForm(forms.ModelForm):
help_text='Cable type' help_text='Cable type'
) )
length_unit = CSVChoiceField( length_unit = CSVChoiceField(
choices=CABLE_LENGTH_UNIT_CHOICES, choices=CableLengthUnitChoices,
required=False, required=False,
help_text='Length unit' help_text='Length unit'
) )
@ -3254,7 +3254,7 @@ class CableBulkEditForm(BootstrapMixin, BulkEditForm):
required=False required=False
) )
length_unit = forms.ChoiceField( length_unit = forms.ChoiceField(
choices=add_blank_choice(CABLE_LENGTH_UNIT_CHOICES), choices=add_blank_choice(CableLengthUnitChoices),
required=False, required=False,
initial='', initial='',
widget=StaticSelect2() widget=StaticSelect2()

View File

@ -23,6 +23,13 @@ CABLE_TYPE_CHOICES = (
(5000, 'power'), (5000, 'power'),
) )
CABLE_LENGTH_UNIT_CHOICES = (
(1200, 'm'),
(1100, 'cm'),
(2100, 'ft'),
(2000, 'in'),
)
def cable_type_to_slug(apps, schema_editor): def cable_type_to_slug(apps, schema_editor):
Cable = apps.get_model('dcim', 'Cable') Cable = apps.get_model('dcim', 'Cable')
@ -30,6 +37,12 @@ def cable_type_to_slug(apps, schema_editor):
Cable.objects.filter(type=id).update(type=slug) Cable.objects.filter(type=id).update(type=slug)
def cable_length_unit_to_slug(apps, schema_editor):
Cable = apps.get_model('dcim', 'Cable')
for id, slug in CABLE_LENGTH_UNIT_CHOICES:
Cable.objects.filter(length_unit=id).update(length_unit=slug)
class Migration(migrations.Migration): class Migration(migrations.Migration):
atomic = False atomic = False
@ -38,6 +51,8 @@ class Migration(migrations.Migration):
] ]
operations = [ operations = [
# Cable.type
migrations.AlterField( migrations.AlterField(
model_name='cable', model_name='cable',
name='type', name='type',
@ -51,4 +66,20 @@ class Migration(migrations.Migration):
name='type', name='type',
field=models.CharField(blank=True, max_length=50), field=models.CharField(blank=True, max_length=50),
), ),
# Cable.length_unit
migrations.AlterField(
model_name='cable',
name='length_unit',
field=models.CharField(blank=True, default='', max_length=50),
),
migrations.RunPython(
code=cable_length_unit_to_slug
),
migrations.AlterField(
model_name='cable',
name='length_unit',
field=models.CharField(blank=True, max_length=50),
),
] ]

View File

@ -2829,10 +2829,10 @@ class Cable(ChangeLoggedModel):
blank=True, blank=True,
null=True null=True
) )
length_unit = models.PositiveSmallIntegerField( length_unit = models.CharField(
choices=CABLE_LENGTH_UNIT_CHOICES, max_length=50,
choices=CableLengthUnitChoices,
blank=True, blank=True,
null=True
) )
# Stores the normalized length (in meters) for database ordering # Stores the normalized length (in meters) for database ordering
_abs_length = models.DecimalField( _abs_length = models.DecimalField(
@ -2960,10 +2960,10 @@ class Cable(ChangeLoggedModel):
)) ))
# Validate length and length_unit # Validate length and length_unit
if self.length is not None and self.length_unit is None: if self.length is not None and not self.length_unit:
raise ValidationError("Must specify a unit when setting a cable length") raise ValidationError("Must specify a unit when setting a cable length")
elif self.length is None: elif self.length is None:
self.length_unit = None self.length_unit = ''
def save(self, *args, **kwargs): def save(self, *args, **kwargs):