#3892: Convert CABLE_TERMINATION_TYPES to a Q object

This commit is contained in:
Jeremy Stretch 2020-01-15 15:51:51 -05:00
parent b98ac64ac2
commit f8dad1744c
6 changed files with 48 additions and 15 deletions

View File

@ -609,10 +609,10 @@ class InventoryItemSerializer(TaggitSerializer, ValidatedModelSerializer):
class CableSerializer(ValidatedModelSerializer): class CableSerializer(ValidatedModelSerializer):
termination_a_type = ContentTypeField( termination_a_type = ContentTypeField(
queryset=ContentType.objects.filter(model__in=CABLE_TERMINATION_TYPES) queryset=ContentType.objects.filter(CABLE_TERMINATION_MODELS)
) )
termination_b_type = ContentTypeField( termination_b_type = ContentTypeField(
queryset=ContentType.objects.filter(model__in=CABLE_TERMINATION_TYPES) queryset=ContentType.objects.filter(CABLE_TERMINATION_MODELS)
) )
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)

View File

@ -1,3 +1,5 @@
from django.db.models import Q
from .choices import InterfaceTypeChoices from .choices import InterfaceTypeChoices
@ -43,10 +45,21 @@ CONNECTION_STATUS_CHOICES = [
] ]
# Cable endpoint types # Cable endpoint types
CABLE_TERMINATION_TYPES = [ CABLE_TERMINATION_MODELS = Q(
'consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontport', 'rearport', Q(app_label='circuits', model__in=(
'circuittermination', 'powerfeed', 'circuittermination',
] )) |
Q(app_label='dcim', model__in=(
'consoleport',
'consoleserverport',
'frontport',
'interface',
'powerfeed',
'poweroutlet',
'powerport',
'rearport',
))
)
COMPATIBLE_TERMINATION_TYPES = { COMPATIBLE_TERMINATION_TYPES = {
'consoleport': ['consoleserverport', 'frontport', 'rearport'], 'consoleport': ['consoleserverport', 'frontport', 'rearport'],

View File

@ -3374,9 +3374,7 @@ class CableCSVForm(forms.ModelForm):
) )
side_a_type = forms.ModelChoiceField( side_a_type = forms.ModelChoiceField(
queryset=ContentType.objects.all(), queryset=ContentType.objects.all(),
limit_choices_to={ limit_choices_to=CABLE_TERMINATION_MODELS,
'model__in': CABLE_TERMINATION_TYPES,
},
to_field_name='model', to_field_name='model',
help_text='Side A type' help_text='Side A type'
) )
@ -3395,9 +3393,7 @@ class CableCSVForm(forms.ModelForm):
) )
side_b_type = forms.ModelChoiceField( side_b_type = forms.ModelChoiceField(
queryset=ContentType.objects.all(), queryset=ContentType.objects.all(),
limit_choices_to={ limit_choices_to=CABLE_TERMINATION_MODELS,
'model__in': CABLE_TERMINATION_TYPES,
},
to_field_name='model', to_field_name='model',
help_text='Side B type' help_text='Side B type'
) )

View File

@ -0,0 +1,24 @@
# Generated by Django 2.2.8 on 2020-01-15 20:51
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dcim', '0089_deterministic_ordering'),
]
operations = [
migrations.AlterField(
model_name='cable',
name='termination_a_type',
field=models.ForeignKey(limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'circuits'), ('model__in', ('circuittermination',))), models.Q(('app_label', 'dcim'), ('model__in', ('consoleport', 'consoleserverport', 'frontport', 'interface', 'powerfeed', 'poweroutlet', 'powerport', 'rearport'))), _connector='OR')), on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
),
migrations.AlterField(
model_name='cable',
name='termination_b_type',
field=models.ForeignKey(limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'circuits'), ('model__in', ('circuittermination',))), models.Q(('app_label', 'dcim'), ('model__in', ('consoleport', 'consoleserverport', 'frontport', 'interface', 'powerfeed', 'poweroutlet', 'powerport', 'rearport'))), _connector='OR')), on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
),
]

View File

@ -1939,7 +1939,7 @@ class Cable(ChangeLoggedModel):
""" """
termination_a_type = models.ForeignKey( termination_a_type = models.ForeignKey(
to=ContentType, to=ContentType,
limit_choices_to={'model__in': CABLE_TERMINATION_TYPES}, limit_choices_to=CABLE_TERMINATION_MODELS,
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='+' related_name='+'
) )
@ -1950,7 +1950,7 @@ class Cable(ChangeLoggedModel):
) )
termination_b_type = models.ForeignKey( termination_b_type = models.ForeignKey(
to=ContentType, to=ContentType,
limit_choices_to={'model__in': CABLE_TERMINATION_TYPES}, limit_choices_to=CABLE_TERMINATION_MODELS,
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='+' related_name='+'
) )

View File

@ -30,7 +30,7 @@ class ChoicesTest(APITestCase):
# Cable # Cable
self.assertEqual(choices_to_dict(response.data.get('cable:length_unit')), CableLengthUnitChoices.as_dict()) self.assertEqual(choices_to_dict(response.data.get('cable:length_unit')), CableLengthUnitChoices.as_dict())
self.assertEqual(choices_to_dict(response.data.get('cable:status')), CableStatusChoices.as_dict()) self.assertEqual(choices_to_dict(response.data.get('cable:status')), CableStatusChoices.as_dict())
content_types = ContentType.objects.filter(model__in=CABLE_TERMINATION_TYPES) content_types = ContentType.objects.filter(CABLE_TERMINATION_MODELS)
cable_termination_choices = { cable_termination_choices = {
"{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types "{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types
} }