2
0
Эх сурвалжийг харах

Cable.length_unit to slug (#3569)

Jeremy Stretch 6 жил өмнө
parent
commit
4846557d61

+ 1 - 1
netbox/dcim/api/serializers.py

@@ -586,7 +586,7 @@ class CableSerializer(ValidatedModelSerializer):
     termination_a = serializers.SerializerMethodField(read_only=True)
     termination_b = serializers.SerializerMethodField(read_only=True)
     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:
         model = Cable

+ 22 - 0
netbox/dcim/choices.py

@@ -834,3 +834,25 @@ class CableTypeChoices(ChoiceSet):
         TYPE_AOC: 3800,
         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,
+    }

+ 0 - 6
netbox/dcim/constants.py

@@ -72,12 +72,6 @@ LENGTH_UNIT_CENTIMETER = 1100
 LENGTH_UNIT_MILLIMETER = 1000
 LENGTH_UNIT_FOOT = 2100
 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 = (
     (LENGTH_UNIT_MILLIMETER, 'Millimeters'),
     (LENGTH_UNIT_INCH, 'Inches'),

+ 2 - 2
netbox/dcim/forms.py

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

+ 31 - 0
netbox/dcim/migrations/0083_3569_cable_fields.py

@@ -23,6 +23,13 @@ CABLE_TYPE_CHOICES = (
     (5000, 'power'),
 )
 
+CABLE_LENGTH_UNIT_CHOICES = (
+    (1200, 'm'),
+    (1100, 'cm'),
+    (2100, 'ft'),
+    (2000, 'in'),
+)
+
 
 def cable_type_to_slug(apps, schema_editor):
     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)
 
 
+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):
     atomic = False
 
@@ -38,6 +51,8 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
+
+        # Cable.type
         migrations.AlterField(
             model_name='cable',
             name='type',
@@ -51,4 +66,20 @@ class Migration(migrations.Migration):
             name='type',
             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),
+        ),
+
     ]

+ 5 - 5
netbox/dcim/models.py

@@ -2829,10 +2829,10 @@ class Cable(ChangeLoggedModel):
         blank=True,
         null=True
     )
-    length_unit = models.PositiveSmallIntegerField(
-        choices=CABLE_LENGTH_UNIT_CHOICES,
+    length_unit = models.CharField(
+        max_length=50,
+        choices=CableLengthUnitChoices,
         blank=True,
-        null=True
     )
     # Stores the normalized length (in meters) for database ordering
     _abs_length = models.DecimalField(
@@ -2960,10 +2960,10 @@ class Cable(ChangeLoggedModel):
             ))
 
         # 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")
         elif self.length is None:
-            self.length_unit = None
+            self.length_unit = ''
 
     def save(self, *args, **kwargs):