Răsfoiți Sursa

Rack.status to slug (#3569)

Jeremy Stretch 6 ani în urmă
părinte
comite
8d27b62114

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

@@ -117,7 +117,7 @@ class RackSerializer(TaggitSerializer, CustomFieldModelSerializer):
     tenant = NestedTenantSerializer(required=False, allow_null=True)
     status = ChoiceField(choices=RACK_STATUS_CHOICES, required=False)
     role = NestedRackRoleSerializer(required=False, allow_null=True)
-    type = ChoiceField(choices=RACK_TYPE_CHOICES, required=False, allow_null=True)
+    type = ChoiceField(choices=RackTypeChoices, required=False, allow_null=True)
     width = ChoiceField(choices=RACK_WIDTH_CHOICES, required=False)
     outer_unit = ChoiceField(choices=RACK_DIMENSION_UNIT_CHOICES, required=False)
     tags = TagListSerializerField(required=False)

+ 29 - 1
netbox/dcim/choices.py

@@ -1,5 +1,33 @@
 from utilities.choices import ChoiceSet
-from .constants import *
+
+
+#
+# Racks
+#
+
+class RackTypeChoices(ChoiceSet):
+
+    TYPE_2POST = '2-post-frame'
+    TYPE_4POST = '4-post-frame'
+    TYPE_CABINET = '4-post-cabinet'
+    TYPE_WALLFRAME = 'wall-frame'
+    TYPE_WALLCABINET = 'wall-cabinet'
+
+    CHOICES = (
+        (TYPE_2POST, '2-post frame'),
+        (TYPE_4POST, '4-post frame'),
+        (TYPE_CABINET, '4-post cabinet'),
+        (TYPE_WALLFRAME, 'Wall-mounted frame'),
+        (TYPE_WALLCABINET, 'Wall-mounted cabinet'),
+    )
+
+    LEGACY_MAP = {
+        TYPE_2POST: 100,
+        TYPE_4POST: 200,
+        TYPE_CABINET: 300,
+        TYPE_WALLFRAME: 1000,
+        TYPE_WALLCABINET: 1100,
+    }
 
 
 #

+ 0 - 14
netbox/dcim/constants.py

@@ -1,17 +1,3 @@
-# Rack types
-RACK_TYPE_2POST = 100
-RACK_TYPE_4POST = 200
-RACK_TYPE_CABINET = 300
-RACK_TYPE_WALLFRAME = 1000
-RACK_TYPE_WALLCABINET = 1100
-RACK_TYPE_CHOICES = (
-    (RACK_TYPE_2POST, '2-post frame'),
-    (RACK_TYPE_4POST, '4-post frame'),
-    (RACK_TYPE_CABINET, '4-post cabinet'),
-    (RACK_TYPE_WALLFRAME, 'Wall-mounted frame'),
-    (RACK_TYPE_WALLCABINET, 'Wall-mounted cabinet'),
-)
-
 # Rack widths
 RACK_WIDTH_19IN = 19
 RACK_WIDTH_23IN = 23

+ 2 - 2
netbox/dcim/forms.py

@@ -487,7 +487,7 @@ class RackCSVForm(forms.ModelForm):
         }
     )
     type = CSVChoiceField(
-        choices=RACK_TYPE_CHOICES,
+        choices=RackTypeChoices,
         required=False,
         help_text='Rack type'
     )
@@ -593,7 +593,7 @@ class RackBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditFor
         required=False
     )
     type = forms.ChoiceField(
-        choices=add_blank_choice(RACK_TYPE_CHOICES),
+        choices=add_blank_choice(RackTypeChoices),
         required=False,
         widget=StaticSelect2()
     )

+ 33 - 0
netbox/dcim/migrations/0078_rack_choicefields_to_slugs.py

@@ -0,0 +1,33 @@
+from django.db import migrations, models
+
+RACK_TYPE_CHOICES = (
+    (100, '2-post-frame'),
+    (200, '4-post-frame'),
+    (300, '4-post-cabinet'),
+    (1000, 'wall-frame'),
+    (1100, 'wall-cabinet'),
+)
+
+
+def rack_type_to_slug(apps, schema_editor):
+    Rack = apps.get_model('dcim', 'Rack')
+    for id, slug in RACK_TYPE_CHOICES:
+        Rack.objects.filter(status=str(id)).update(status=slug)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('dcim', '0077_power_types'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='rack',
+            name='type',
+            field=models.CharField(blank=True, max_length=50),
+        ),
+        migrations.RunPython(
+            code=rack_type_to_slug
+        )
+    ]

+ 3 - 3
netbox/dcim/models.py

@@ -497,10 +497,10 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
         verbose_name='Asset tag',
         help_text='A unique tag used to identify this rack'
     )
-    type = models.PositiveSmallIntegerField(
-        choices=RACK_TYPE_CHOICES,
+    type = models.CharField(
+        choices=RackTypeChoices,
+        max_length=50,
         blank=True,
-        null=True,
         verbose_name='Type'
     )
     width = models.PositiveSmallIntegerField(