Convert console port types to slugs (#3569)

This commit is contained in:
Jeremy Stretch 2019-11-06 16:56:46 -05:00
parent 48dcd2d2b9
commit 8cb7eb0382
8 changed files with 70 additions and 115 deletions

View File

@ -4,7 +4,7 @@ from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
from dcim.choices import PowerOutletTypes, PowerPortTypes
from dcim.choices import *
from dcim.constants import *
from dcim.models import (
Cable, ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceBay,
@ -201,7 +201,10 @@ class DeviceTypeSerializer(TaggitSerializer, CustomFieldModelSerializer):
class ConsolePortTemplateSerializer(ValidatedModelSerializer):
device_type = NestedDeviceTypeSerializer()
type = ChoiceField(choices=CONSOLE_TYPE_CHOICES, required=False)
type = ChoiceField(
choices=ConsolePortTypes.CHOICES,
required=False
)
class Meta:
model = ConsolePortTemplate
@ -210,7 +213,10 @@ class ConsolePortTemplateSerializer(ValidatedModelSerializer):
class ConsoleServerPortTemplateSerializer(ValidatedModelSerializer):
device_type = NestedDeviceTypeSerializer()
type = ChoiceField(choices=CONSOLE_TYPE_CHOICES, required=False)
type = ChoiceField(
choices=ConsolePortTypes.CHOICES,
required=False
)
class Meta:
model = ConsoleServerPortTemplate
@ -381,7 +387,10 @@ class DeviceWithConfigContextSerializer(DeviceSerializer):
class ConsoleServerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
device = NestedDeviceSerializer()
type = ChoiceField(choices=CONSOLE_TYPE_CHOICES, required=False)
type = ChoiceField(
choices=ConsolePortTypes.CHOICES,
required=False
)
cable = NestedCableSerializer(read_only=True)
tags = TagListSerializerField(required=False)
@ -395,7 +404,10 @@ class ConsoleServerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer)
class ConsolePortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
device = NestedDeviceSerializer()
type = ChoiceField(choices=CONSOLE_TYPE_CHOICES, required=False)
type = ChoiceField(
choices=ConsolePortTypes.CHOICES,
required=False
)
cable = NestedCableSerializer(read_only=True)
tags = TagListSerializerField(required=False)

View File

@ -21,7 +21,7 @@ class ConsolePortTypes:
TYPE_USB_MICRO_B = 'usb-micro-b'
TYPE_OTHER = 'other'
TYPE_CHOICES = (
CHOICES = (
('Serial', (
(TYPE_DE9, 'DE-9'),
(TYPE_DB25, 'DB-25'),
@ -41,26 +41,6 @@ class ConsolePortTypes:
)),
)
@classmethod
def slug_to_integer(cls, slug):
"""
Provide backward-compatible mapping of the type slug to integer.
"""
return {
# Slug: integer
cls.TYPE_DE9: CONSOLE_TYPE_DE9,
cls.TYPE_DB25: CONSOLE_TYPE_DB25,
cls.TYPE_RJ45: CONSOLE_TYPE_RJ45,
cls.TYPE_USB_A: CONSOLE_TYPE_USB_A,
cls.TYPE_USB_B: CONSOLE_TYPE_USB_B,
cls.TYPE_USB_C: CONSOLE_TYPE_USB_C,
cls.TYPE_USB_MINI_A: CONSOLE_TYPE_USB_MINI_A,
cls.TYPE_USB_MINI_B: CONSOLE_TYPE_USB_MINI_B,
cls.TYPE_USB_MICRO_A: CONSOLE_TYPE_USB_MICRO_A,
cls.TYPE_USB_MICRO_B: CONSOLE_TYPE_USB_MICRO_B,
cls.TYPE_OTHER: CONSOLE_TYPE_OTHER,
}.get(slug)
#
# Power port types

View File

@ -57,41 +57,6 @@ SUBDEVICE_ROLE_CHOICES = (
(SUBDEVICE_ROLE_CHILD, 'Child'),
)
#
# Numeric console port types
#
CONSOLE_TYPE_DE9 = 1000
CONSOLE_TYPE_DB25 = 1100
CONSOLE_TYPE_RJ45 = 2000
CONSOLE_TYPE_USB_A = 3000
CONSOLE_TYPE_USB_B = 3010
CONSOLE_TYPE_USB_C = 3020
CONSOLE_TYPE_USB_MINI_A = 3100
CONSOLE_TYPE_USB_MINI_B = 3110
CONSOLE_TYPE_USB_MICRO_A = 3200
CONSOLE_TYPE_USB_MICRO_B = 3210
CONSOLE_TYPE_OTHER = 32767
CONSOLE_TYPE_CHOICES = [
['Serial', [
[CONSOLE_TYPE_DE9, 'DE-9'],
[CONSOLE_TYPE_DB25, 'DB-25'],
[CONSOLE_TYPE_RJ45, 'RJ-45'],
]],
['USB', [
[CONSOLE_TYPE_USB_A, 'USB Type A'],
[CONSOLE_TYPE_USB_B, 'USB Type B'],
[CONSOLE_TYPE_USB_C, 'USB Type C'],
[CONSOLE_TYPE_USB_MINI_A, 'USB Mini A'],
[CONSOLE_TYPE_USB_MINI_B, 'USB Mini B'],
[CONSOLE_TYPE_USB_MICRO_A, 'USB Micro A'],
[CONSOLE_TYPE_USB_MICRO_B, 'USB Micro B'],
]],
['Other', [
[CONSOLE_TYPE_OTHER, 'Other'],
]],
]
#
# Numeric interface types
#

View File

@ -11,6 +11,7 @@ from utilities.filters import (
TreeNodeMultipleChoiceFilter,
)
from virtualization.models import Cluster
from .choices import *
from .constants import *
from .models import (
Cable, ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceBay,
@ -642,7 +643,7 @@ class DeviceComponentFilterSet(django_filters.FilterSet):
class ConsolePortFilter(DeviceComponentFilterSet):
type = django_filters.MultipleChoiceFilter(
choices=CONSOLE_TYPE_CHOICES,
choices=ConsolePortTypes.CHOICES,
null_value=None
)
cabled = django_filters.BooleanFilter(
@ -653,12 +654,12 @@ class ConsolePortFilter(DeviceComponentFilterSet):
class Meta:
model = ConsolePort
fields = ['id', 'name', 'type', 'description', 'connection_status']
fields = ['id', 'name', 'description', 'connection_status']
class ConsoleServerPortFilter(DeviceComponentFilterSet):
type = django_filters.MultipleChoiceFilter(
choices=CONSOLE_TYPE_CHOICES,
choices=ConsolePortTypes.CHOICES,
null_value=None
)
cabled = django_filters.BooleanFilter(
@ -669,10 +670,14 @@ class ConsoleServerPortFilter(DeviceComponentFilterSet):
class Meta:
model = ConsoleServerPort
fields = ['id', 'name', 'type', 'description', 'connection_status']
fields = ['id', 'name', 'description', 'connection_status']
class PowerPortFilter(DeviceComponentFilterSet):
type = django_filters.MultipleChoiceFilter(
choices=PowerPortTypes.CHOICES,
null_value=None
)
cabled = django_filters.BooleanFilter(
field_name='cable',
lookup_expr='isnull',
@ -681,10 +686,14 @@ class PowerPortFilter(DeviceComponentFilterSet):
class Meta:
model = PowerPort
fields = ['id', 'name', 'type', 'maximum_draw', 'allocated_draw', 'description', 'connection_status']
fields = ['id', 'name', 'maximum_draw', 'allocated_draw', 'description', 'connection_status']
class PowerOutletFilter(DeviceComponentFilterSet):
type = django_filters.MultipleChoiceFilter(
choices=PowerOutletTypes.CHOICES,
null_value=None
)
cabled = django_filters.BooleanFilter(
field_name='cable',
lookup_expr='isnull',
@ -693,7 +702,7 @@ class PowerOutletFilter(DeviceComponentFilterSet):
class Meta:
model = PowerOutlet
fields = ['id', 'name', 'type', 'feed_leg', 'description', 'connection_status']
fields = ['id', 'name', 'feed_leg', 'description', 'connection_status']
class InterfaceFilter(django_filters.FilterSet):

View File

@ -954,7 +954,7 @@ class ConsolePortTemplateCreateForm(ComponentForm):
label='Name'
)
type = forms.ChoiceField(
choices=CONSOLE_TYPE_CHOICES,
choices=ConsolePortTypes.CHOICES,
widget=StaticSelect2()
)
@ -976,7 +976,7 @@ class ConsoleServerPortTemplateCreateForm(ComponentForm):
label='Name'
)
type = forms.ChoiceField(
choices=CONSOLE_TYPE_CHOICES,
choices=add_blank_choice(ConsolePortTypes.CHOICES),
widget=StaticSelect2()
)
@ -1265,9 +1265,6 @@ class ComponentTemplateImportForm(BootstrapMixin, forms.ModelForm):
class ConsolePortTemplateImportForm(ComponentTemplateImportForm):
type = forms.ChoiceField(
choices=ConsolePortTypes.TYPE_CHOICES
)
class Meta:
model = ConsolePortTemplate
@ -1275,16 +1272,8 @@ class ConsolePortTemplateImportForm(ComponentTemplateImportForm):
'device_type', 'name', 'type',
]
def clean_type(self):
# Convert slug value to field integer value
slug = self.cleaned_data['type']
return ConsolePortTypes.slug_to_integer(slug)
class ConsoleServerPortTemplateImportForm(ComponentTemplateImportForm):
type = forms.ChoiceField(
choices=ConsolePortTypes.TYPE_CHOICES
)
class Meta:
model = ConsoleServerPortTemplate
@ -1292,11 +1281,6 @@ class ConsoleServerPortTemplateImportForm(ComponentTemplateImportForm):
'device_type', 'name', 'type',
]
def clean_type(self):
# Convert slug value to field integer value
slug = self.cleaned_data['type']
return ConsolePortTypes.slug_to_integer(slug)
class PowerPortTemplateImportForm(ComponentTemplateImportForm):
@ -2099,8 +2083,9 @@ class ConsolePortCreateForm(ComponentForm):
label='Name'
)
type = forms.ChoiceField(
choices=CONSOLE_TYPE_CHOICES,
widget=StaticSelect2(),
choices=add_blank_choice(ConsolePortTypes.CHOICES),
required=False,
widget=StaticSelect2()
)
description = forms.CharField(
max_length=100,
@ -2135,8 +2120,9 @@ class ConsoleServerPortCreateForm(ComponentForm):
label='Name'
)
type = forms.ChoiceField(
choices=CONSOLE_TYPE_CHOICES,
widget=StaticSelect2(),
choices=add_blank_choice(ConsolePortTypes.CHOICES),
required=False,
widget=StaticSelect2()
)
description = forms.CharField(
max_length=100,
@ -2153,7 +2139,7 @@ class ConsoleServerPortBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditF
widget=forms.MultipleHiddenInput()
)
type = forms.ChoiceField(
choices=add_blank_choice(CONSOLE_TYPE_CHOICES),
choices=add_blank_choice(ConsolePortTypes.CHOICES),
required=False,
widget=StaticSelect2()
)
@ -2207,7 +2193,8 @@ class PowerPortCreateForm(ComponentForm):
)
type = forms.ChoiceField(
choices=add_blank_choice(PowerPortTypes.CHOICES),
required=False
required=False,
widget=StaticSelect2()
)
maximum_draw = forms.IntegerField(
min_value=1,
@ -2266,7 +2253,8 @@ class PowerOutletCreateForm(ComponentForm):
)
type = forms.ChoiceField(
choices=add_blank_choice(PowerOutletTypes.CHOICES),
required=False
required=False,
widget=StaticSelect2()
)
power_port = forms.ModelChoiceField(
queryset=PowerPort.objects.all(),

View File

@ -13,21 +13,21 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='consoleport',
name='type',
field=models.PositiveSmallIntegerField(blank=True, null=True),
field=models.CharField(blank=True, max_length=50),
),
migrations.AddField(
model_name='consoleporttemplate',
name='type',
field=models.PositiveSmallIntegerField(blank=True, null=True),
field=models.CharField(blank=True, max_length=50),
),
migrations.AddField(
model_name='consoleserverport',
name='type',
field=models.PositiveSmallIntegerField(blank=True, null=True),
field=models.CharField(blank=True, max_length=50),
),
migrations.AddField(
model_name='consoleserverporttemplate',
name='type',
field=models.PositiveSmallIntegerField(blank=True, null=True),
field=models.CharField(blank=True, max_length=50),
),
]

View File

@ -20,7 +20,7 @@ from utilities.fields import ColorField
from utilities.managers import NaturalOrderingManager
from utilities.models import ChangeLoggedModel
from utilities.utils import serialize_object, to_meters
from .choices import PowerOutletTypes, PowerPortTypes
from .choices import *
from .constants import *
from .exceptions import LoopDetected
from .fields import ASNField, MACAddressField
@ -1015,10 +1015,10 @@ class ConsolePortTemplate(ComponentTemplateModel):
name = models.CharField(
max_length=50
)
type = models.PositiveSmallIntegerField(
choices=CONSOLE_TYPE_CHOICES,
blank=True,
null=True
type = models.CharField(
max_length=50,
choices=ConsolePortTypes.CHOICES,
blank=True
)
objects = NaturalOrderingManager()
@ -1050,10 +1050,10 @@ class ConsoleServerPortTemplate(ComponentTemplateModel):
name = models.CharField(
max_length=50
)
type = models.PositiveSmallIntegerField(
choices=CONSOLE_TYPE_CHOICES,
blank=True,
null=True
type = models.CharField(
max_length=50,
choices=ConsolePortTypes.CHOICES,
blank=True
)
objects = NaturalOrderingManager()
@ -1869,10 +1869,10 @@ class ConsolePort(CableTermination, ComponentModel):
name = models.CharField(
max_length=50
)
type = models.PositiveSmallIntegerField(
choices=CONSOLE_TYPE_CHOICES,
blank=True,
null=True
type = models.CharField(
max_length=50,
choices=ConsolePortTypes.CHOICES,
blank=True
)
connected_endpoint = models.OneToOneField(
to='dcim.ConsoleServerPort',
@ -1926,10 +1926,10 @@ class ConsoleServerPort(CableTermination, ComponentModel):
name = models.CharField(
max_length=50
)
type = models.PositiveSmallIntegerField(
choices=CONSOLE_TYPE_CHOICES,
blank=True,
null=True
type = models.CharField(
max_length=50,
choices=ConsolePortTypes.CHOICES,
blank=True
)
connection_status = models.NullBooleanField(
choices=CONNECTION_STATUS_CHOICES,

View File

@ -3,6 +3,7 @@ import urllib.parse
from django.test import Client, TestCase
from django.urls import reverse
from dcim.choices import *
from dcim.constants import *
from dcim.models import (
Cable, ConsolePortTemplate, ConsoleServerPortTemplate, Device, DeviceBayTemplate, DeviceRole, DeviceType,
@ -319,12 +320,12 @@ device-bays:
self.assertEqual(dt.consoleport_templates.count(), 3)
cp1 = ConsolePortTemplate.objects.first()
self.assertEqual(cp1.name, 'Console Port 1')
self.assertEqual(cp1.type, CONSOLE_TYPE_DE9)
self.assertEqual(cp1.type, ConsolePortTypes.TYPE_DE9)
self.assertEqual(dt.consoleserverport_templates.count(), 3)
csp1 = ConsoleServerPortTemplate.objects.first()
self.assertEqual(csp1.name, 'Console Server Port 1')
self.assertEqual(csp1.type, CONSOLE_TYPE_RJ45)
self.assertEqual(csp1.type, ConsolePortTypes.TYPE_RJ45)
self.assertEqual(dt.powerport_templates.count(), 3)
pp1 = PowerPortTemplate.objects.first()