mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
Cable.status to slug (#3569)
This commit is contained in:
parent
086813fc3e
commit
12657ebd7c
@ -585,7 +585,7 @@ class CableSerializer(ValidatedModelSerializer):
|
|||||||
)
|
)
|
||||||
termination_a = serializers.SerializerMethodField(read_only=True)
|
termination_a = serializers.SerializerMethodField(read_only=True)
|
||||||
termination_b = serializers.SerializerMethodField(read_only=True)
|
termination_b = serializers.SerializerMethodField(read_only=True)
|
||||||
status = ChoiceField(choices=CONNECTION_STATUS_CHOICES, required=False)
|
status = ChoiceField(choices=CableStatusChoices, required=False)
|
||||||
length_unit = ChoiceField(choices=CableLengthUnitChoices, required=False, allow_null=True)
|
length_unit = ChoiceField(choices=CableLengthUnitChoices, required=False, allow_null=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -923,6 +923,22 @@ class CableTypeChoices(ChoiceSet):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class CableStatusChoices(ChoiceSet):
|
||||||
|
|
||||||
|
STATUS_CONNECTED = 'connected'
|
||||||
|
STATUS_PLANNED = 'planned'
|
||||||
|
|
||||||
|
CHOICES = (
|
||||||
|
(STATUS_CONNECTED, 'Connected'),
|
||||||
|
(STATUS_PLANNED, 'Planned'),
|
||||||
|
)
|
||||||
|
|
||||||
|
LEGACY_MAP = {
|
||||||
|
STATUS_CONNECTED: True,
|
||||||
|
STATUS_PLANNED: False,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class CableLengthUnitChoices(ChoiceSet):
|
class CableLengthUnitChoices(ChoiceSet):
|
||||||
|
|
||||||
UNIT_METER = 'm'
|
UNIT_METER = 'm'
|
||||||
|
@ -983,7 +983,7 @@ class CableFilter(django_filters.FilterSet):
|
|||||||
choices=CableTypeChoices
|
choices=CableTypeChoices
|
||||||
)
|
)
|
||||||
status = django_filters.MultipleChoiceFilter(
|
status = django_filters.MultipleChoiceFilter(
|
||||||
choices=CONNECTION_STATUS_CHOICES
|
choices=CableStatusChoices
|
||||||
)
|
)
|
||||||
color = django_filters.MultipleChoiceFilter(
|
color = django_filters.MultipleChoiceFilter(
|
||||||
choices=COLOR_CHOICES
|
choices=COLOR_CHOICES
|
||||||
|
@ -3427,7 +3427,7 @@ class CableCSVForm(forms.ModelForm):
|
|||||||
|
|
||||||
# Cable attributes
|
# Cable attributes
|
||||||
status = CSVChoiceField(
|
status = CSVChoiceField(
|
||||||
choices=CONNECTION_STATUS_CHOICES,
|
choices=CableStatusChoices,
|
||||||
required=False,
|
required=False,
|
||||||
help_text='Connection status'
|
help_text='Connection status'
|
||||||
)
|
)
|
||||||
@ -3523,7 +3523,7 @@ class CableBulkEditForm(BootstrapMixin, BulkEditForm):
|
|||||||
widget=StaticSelect2()
|
widget=StaticSelect2()
|
||||||
)
|
)
|
||||||
status = forms.ChoiceField(
|
status = forms.ChoiceField(
|
||||||
choices=add_blank_choice(CONNECTION_STATUS_CHOICES),
|
choices=add_blank_choice(CableStatusChoices),
|
||||||
required=False,
|
required=False,
|
||||||
widget=StaticSelect2(),
|
widget=StaticSelect2(),
|
||||||
initial=''
|
initial=''
|
||||||
@ -3597,7 +3597,7 @@ class CableFilterForm(BootstrapMixin, forms.Form):
|
|||||||
)
|
)
|
||||||
status = forms.ChoiceField(
|
status = forms.ChoiceField(
|
||||||
required=False,
|
required=False,
|
||||||
choices=add_blank_choice(CONNECTION_STATUS_CHOICES),
|
choices=add_blank_choice(CableStatusChoices),
|
||||||
widget=StaticSelect2()
|
widget=StaticSelect2()
|
||||||
)
|
)
|
||||||
color = forms.CharField(
|
color = forms.CharField(
|
||||||
|
@ -23,6 +23,11 @@ CABLE_TYPE_CHOICES = (
|
|||||||
(5000, 'power'),
|
(5000, 'power'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
CABLE_STATUS_CHOICES = (
|
||||||
|
(True, 'connected'),
|
||||||
|
(False, 'planned'),
|
||||||
|
)
|
||||||
|
|
||||||
CABLE_LENGTH_UNIT_CHOICES = (
|
CABLE_LENGTH_UNIT_CHOICES = (
|
||||||
(1200, 'm'),
|
(1200, 'm'),
|
||||||
(1100, 'cm'),
|
(1100, 'cm'),
|
||||||
@ -37,6 +42,12 @@ def cable_type_to_slug(apps, schema_editor):
|
|||||||
Cable.objects.filter(type=id).update(type=slug)
|
Cable.objects.filter(type=id).update(type=slug)
|
||||||
|
|
||||||
|
|
||||||
|
def cable_status_to_slug(apps, schema_editor):
|
||||||
|
Cable = apps.get_model('dcim', 'Cable')
|
||||||
|
for bool, slug in CABLE_STATUS_CHOICES:
|
||||||
|
Cable.objects.filter(status=str(bool)).update(status=slug)
|
||||||
|
|
||||||
|
|
||||||
def cable_length_unit_to_slug(apps, schema_editor):
|
def cable_length_unit_to_slug(apps, schema_editor):
|
||||||
Cable = apps.get_model('dcim', 'Cable')
|
Cable = apps.get_model('dcim', 'Cable')
|
||||||
for id, slug in CABLE_LENGTH_UNIT_CHOICES:
|
for id, slug in CABLE_LENGTH_UNIT_CHOICES:
|
||||||
@ -67,6 +78,16 @@ class Migration(migrations.Migration):
|
|||||||
field=models.CharField(blank=True, max_length=50),
|
field=models.CharField(blank=True, max_length=50),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
# Cable.status
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='cable',
|
||||||
|
name='status',
|
||||||
|
field=models.CharField(max_length=50),
|
||||||
|
),
|
||||||
|
migrations.RunPython(
|
||||||
|
code=cable_status_to_slug
|
||||||
|
),
|
||||||
|
|
||||||
# Cable.length_unit
|
# Cable.length_unit
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='cable',
|
model_name='cable',
|
||||||
|
@ -2840,9 +2840,10 @@ class Cable(ChangeLoggedModel):
|
|||||||
choices=CableTypeChoices,
|
choices=CableTypeChoices,
|
||||||
blank=True
|
blank=True
|
||||||
)
|
)
|
||||||
status = models.BooleanField(
|
status = models.CharField(
|
||||||
choices=CONNECTION_STATUS_CHOICES,
|
max_length=50,
|
||||||
default=CONNECTION_STATUS_CONNECTED
|
choices=CableStatusChoices,
|
||||||
|
default=CableStatusChoices.STATUS_CONNECTED
|
||||||
)
|
)
|
||||||
label = models.CharField(
|
label = models.CharField(
|
||||||
max_length=100,
|
max_length=100,
|
||||||
@ -3039,12 +3040,12 @@ class Cable(ChangeLoggedModel):
|
|||||||
b_path = self.termination_a.trace()
|
b_path = self.termination_a.trace()
|
||||||
|
|
||||||
# Determine overall path status (connected or planned)
|
# Determine overall path status (connected or planned)
|
||||||
if self.status == CONNECTION_STATUS_PLANNED:
|
if self.status == CableStatusChoices.STATUS_PLANNED:
|
||||||
path_status = CONNECTION_STATUS_PLANNED
|
path_status = CONNECTION_STATUS_PLANNED
|
||||||
else:
|
else:
|
||||||
path_status = CONNECTION_STATUS_CONNECTED
|
path_status = CONNECTION_STATUS_CONNECTED
|
||||||
for segment in a_path[1:] + b_path[1:]:
|
for segment in a_path[1:] + b_path[1:]:
|
||||||
if segment[1] is None or segment[1].status == CONNECTION_STATUS_PLANNED:
|
if segment[1] is None or segment[1].status == CableStatusChoices.STATUS_PLANNED:
|
||||||
path_status = CONNECTION_STATUS_PLANNED
|
path_status = CONNECTION_STATUS_PLANNED
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -2895,7 +2895,7 @@ class CableTest(APITestCase):
|
|||||||
'termination_a_id': interface_a.pk,
|
'termination_a_id': interface_a.pk,
|
||||||
'termination_b_type': 'dcim.interface',
|
'termination_b_type': 'dcim.interface',
|
||||||
'termination_b_id': interface_b.pk,
|
'termination_b_id': interface_b.pk,
|
||||||
'status': CONNECTION_STATUS_PLANNED,
|
'status': CableStatusChoices.STATUS_PLANNED,
|
||||||
'label': 'Test Cable 4',
|
'label': 'Test Cable 4',
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2949,7 +2949,7 @@ class CableTest(APITestCase):
|
|||||||
|
|
||||||
data = {
|
data = {
|
||||||
'label': 'Test Cable X',
|
'label': 'Test Cable X',
|
||||||
'status': CONNECTION_STATUS_CONNECTED,
|
'status': CableStatusChoices.STATUS_CONNECTED,
|
||||||
}
|
}
|
||||||
|
|
||||||
url = reverse('dcim-api:cable-detail', kwargs={'pk': self.cable1.pk})
|
url = reverse('dcim-api:cable-detail', kwargs={'pk': self.cable1.pk})
|
||||||
|
@ -487,7 +487,11 @@ class CablePathTestCase(TestCase):
|
|||||||
self.assertIsNone(interface1.connection_status)
|
self.assertIsNone(interface1.connection_status)
|
||||||
|
|
||||||
# Third segment
|
# Third segment
|
||||||
cable3 = Cable(termination_a=self.front_port2, termination_b=self.interface2, status=CONNECTION_STATUS_PLANNED)
|
cable3 = Cable(
|
||||||
|
termination_a=self.front_port2,
|
||||||
|
termination_b=self.interface2,
|
||||||
|
status=CableStatusChoices.STATUS_PLANNED
|
||||||
|
)
|
||||||
cable3.save()
|
cable3.save()
|
||||||
interface1 = Interface.objects.get(pk=self.interface1.pk)
|
interface1 = Interface.objects.get(pk=self.interface1.pk)
|
||||||
self.assertEqual(interface1.connected_endpoint, self.interface2)
|
self.assertEqual(interface1.connected_endpoint, self.interface2)
|
||||||
|
Loading…
Reference in New Issue
Block a user