From 4846557d61befa271d914e1bc7e3a02264f8b03b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 25 Nov 2019 20:40:29 -0500 Subject: [PATCH] Cable.length_unit to slug (#3569) --- netbox/dcim/api/serializers.py | 2 +- netbox/dcim/choices.py | 22 +++++++++++++ netbox/dcim/constants.py | 6 ---- netbox/dcim/forms.py | 4 +-- .../dcim/migrations/0083_3569_cable_fields.py | 31 +++++++++++++++++++ netbox/dcim/models.py | 10 +++--- 6 files changed, 61 insertions(+), 14 deletions(-) diff --git a/netbox/dcim/api/serializers.py b/netbox/dcim/api/serializers.py index 04752641a..283382515 100644 --- a/netbox/dcim/api/serializers.py +++ b/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 diff --git a/netbox/dcim/choices.py b/netbox/dcim/choices.py index b86ef9d6a..d9e02ab4f 100644 --- a/netbox/dcim/choices.py +++ b/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, + } diff --git a/netbox/dcim/constants.py b/netbox/dcim/constants.py index 7aff330e5..d2b715415 100644 --- a/netbox/dcim/constants.py +++ b/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'), diff --git a/netbox/dcim/forms.py b/netbox/dcim/forms.py index 8cfeb2736..c606c4797 100644 --- a/netbox/dcim/forms.py +++ b/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() diff --git a/netbox/dcim/migrations/0083_3569_cable_fields.py b/netbox/dcim/migrations/0083_3569_cable_fields.py index 432935ecf..d6f013b37 100644 --- a/netbox/dcim/migrations/0083_3569_cable_fields.py +++ b/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), + ), + ] diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 3e1de04d7..52b55373d 100644 --- a/netbox/dcim/models.py +++ b/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):