Jelajahi Sumber

Fixes #2616: Convert Rack outer_unit and Cable length_unit to integer-based choice fields

Jeremy Stretch 7 tahun lalu
induk
melakukan
d1cd366dc9

+ 1 - 0
CHANGELOG.md

@@ -44,6 +44,7 @@ NetBox now supports modeling physical cables for console, power, and interface c
 
 * [#2622](https://github.com/digitalocean/netbox/issues/2622) - Enable filtering cables by multiple types/colors
 * [#2624](https://github.com/digitalocean/netbox/issues/2624) - Delete associated content type and permissions when removing InterfaceConnection model
+* [#2616](https://github.com/digitalocean/netbox/issues/2616) - Convert Rack `outer_unit` and Cable `length_unit` to integer-based choice fields
 
 ## API Changes
 

+ 5 - 5
netbox/dcim/constants.py

@@ -360,11 +360,11 @@ COMPATIBLE_TERMINATION_TYPES = {
     'circuittermination': ['interface', 'frontport', 'rearport'],
 }
 
-LENGTH_UNIT_METER = 'm'
-LENGTH_UNIT_CENTIMETER = 'cm'
-LENGTH_UNIT_MILLIMETER = 'mm'
-LENGTH_UNIT_FOOT = 'ft'
-LENGTH_UNIT_INCH = 'in'
+LENGTH_UNIT_METER = 1200
+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'),

+ 1 - 1
netbox/dcim/migrations/0066_cables.py

@@ -166,7 +166,7 @@ class Migration(migrations.Migration):
                 ('label', models.CharField(blank=True, max_length=100)),
                 ('color', utilities.fields.ColorField(blank=True, max_length=6)),
                 ('length', models.PositiveSmallIntegerField(blank=True, null=True)),
-                ('length_unit', models.CharField(blank=True, max_length=2)),
+                ('length_unit', models.PositiveSmallIntegerField(blank=True, null=True)),
                 ('_abs_length', models.DecimalField(blank=True, decimal_places=4, max_digits=10, null=True)),
                 ('termination_a_type', models.ForeignKey(limit_choices_to={'model__in': ['consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontport', 'rearport']}, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType')),
                 ('termination_b_type', models.ForeignKey(limit_choices_to={'model__in': ['consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontport', 'rearport']}, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType')),

+ 1 - 1
netbox/dcim/migrations/0068_rack_new_fields.py

@@ -28,7 +28,7 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='rack',
             name='outer_unit',
-            field=models.CharField(blank=True, max_length=2),
+            field=models.PositiveSmallIntegerField(blank=True, null=True),
         ),
         migrations.AddField(
             model_name='rack',

+ 11 - 11
netbox/dcim/models.py

@@ -511,10 +511,10 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
         blank=True,
         null=True
     )
-    outer_unit = models.CharField(
+    outer_unit = models.PositiveSmallIntegerField(
         choices=RACK_DIMENSION_UNIT_CHOICES,
-        max_length=2,
-        blank=True
+        blank=True,
+        null=True
     )
     comments = models.TextField(
         blank=True
@@ -552,10 +552,10 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
     def clean(self):
 
         # Validate outer dimensions and unit
-        if (self.outer_width or self.outer_depth) and not self.outer_unit:
+        if (self.outer_width is not None or self.outer_depth is not None) and self.outer_unit is None:
             raise ValidationError("Must specify a unit when setting an outer width/depth")
         else:
-            self.outer_unit = ''
+            self.outer_unit = None
 
         if self.pk:
             # Validate that Rack is tall enough to house the installed Devices
@@ -2495,10 +2495,10 @@ class Cable(ChangeLoggedModel):
         blank=True,
         null=True
     )
-    length_unit = models.CharField(
+    length_unit = models.PositiveSmallIntegerField(
         choices=CABLE_LENGTH_UNIT_CHOICES,
-        max_length=2,
-        blank=True
+        blank=True,
+        null=True
     )
     # Stores the normalized length (in meters) for database ordering
     _abs_length = models.DecimalField(
@@ -2584,10 +2584,10 @@ class Cable(ChangeLoggedModel):
             raise ValidationError("Cannot connect to a virtual interface")
 
         # Validate length and length_unit
-        if self.length and not self.length_unit:
+        if self.length is not None and self.length_unit is None:
             raise ValidationError("Must specify a unit when setting a cable length")
-        if self.length_unit and self.length is None:
-            self.length_unit = ''
+        else:
+            self.length_unit = None
 
     def save(self, *args, **kwargs):