From f8dad1744c37dc1717a94c6d652d4d98d2c5d0f2 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 15 Jan 2020 15:51:51 -0500 Subject: [PATCH] #3892: Convert CABLE_TERMINATION_TYPES to a Q object --- netbox/dcim/api/serializers.py | 4 ++-- netbox/dcim/constants.py | 21 ++++++++++++---- netbox/dcim/forms.py | 8 ++----- .../0090_cable_termination_models.py | 24 +++++++++++++++++++ netbox/dcim/models/__init__.py | 4 ++-- netbox/dcim/tests/test_api.py | 2 +- 6 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 netbox/dcim/migrations/0090_cable_termination_models.py diff --git a/netbox/dcim/api/serializers.py b/netbox/dcim/api/serializers.py index 664eb88f1..f0382a3f5 100644 --- a/netbox/dcim/api/serializers.py +++ b/netbox/dcim/api/serializers.py @@ -609,10 +609,10 @@ class InventoryItemSerializer(TaggitSerializer, ValidatedModelSerializer): class CableSerializer(ValidatedModelSerializer): termination_a_type = ContentTypeField( - queryset=ContentType.objects.filter(model__in=CABLE_TERMINATION_TYPES) + queryset=ContentType.objects.filter(CABLE_TERMINATION_MODELS) ) 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_b = serializers.SerializerMethodField(read_only=True) diff --git a/netbox/dcim/constants.py b/netbox/dcim/constants.py index 2a4e2e88e..3a6f8e5e9 100644 --- a/netbox/dcim/constants.py +++ b/netbox/dcim/constants.py @@ -1,3 +1,5 @@ +from django.db.models import Q + from .choices import InterfaceTypeChoices @@ -43,10 +45,21 @@ CONNECTION_STATUS_CHOICES = [ ] # Cable endpoint types -CABLE_TERMINATION_TYPES = [ - 'consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontport', 'rearport', - 'circuittermination', 'powerfeed', -] +CABLE_TERMINATION_MODELS = Q( + Q(app_label='circuits', model__in=( + 'circuittermination', + )) | + Q(app_label='dcim', model__in=( + 'consoleport', + 'consoleserverport', + 'frontport', + 'interface', + 'powerfeed', + 'poweroutlet', + 'powerport', + 'rearport', + )) +) COMPATIBLE_TERMINATION_TYPES = { 'consoleport': ['consoleserverport', 'frontport', 'rearport'], diff --git a/netbox/dcim/forms.py b/netbox/dcim/forms.py index 2f2abaedf..45ed5e136 100644 --- a/netbox/dcim/forms.py +++ b/netbox/dcim/forms.py @@ -3374,9 +3374,7 @@ class CableCSVForm(forms.ModelForm): ) side_a_type = forms.ModelChoiceField( queryset=ContentType.objects.all(), - limit_choices_to={ - 'model__in': CABLE_TERMINATION_TYPES, - }, + limit_choices_to=CABLE_TERMINATION_MODELS, to_field_name='model', help_text='Side A type' ) @@ -3395,9 +3393,7 @@ class CableCSVForm(forms.ModelForm): ) side_b_type = forms.ModelChoiceField( queryset=ContentType.objects.all(), - limit_choices_to={ - 'model__in': CABLE_TERMINATION_TYPES, - }, + limit_choices_to=CABLE_TERMINATION_MODELS, to_field_name='model', help_text='Side B type' ) diff --git a/netbox/dcim/migrations/0090_cable_termination_models.py b/netbox/dcim/migrations/0090_cable_termination_models.py new file mode 100644 index 000000000..b5f240f3e --- /dev/null +++ b/netbox/dcim/migrations/0090_cable_termination_models.py @@ -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'), + ), + ] diff --git a/netbox/dcim/models/__init__.py b/netbox/dcim/models/__init__.py index 8d066e921..46fd3a1b9 100644 --- a/netbox/dcim/models/__init__.py +++ b/netbox/dcim/models/__init__.py @@ -1939,7 +1939,7 @@ class Cable(ChangeLoggedModel): """ termination_a_type = models.ForeignKey( to=ContentType, - limit_choices_to={'model__in': CABLE_TERMINATION_TYPES}, + limit_choices_to=CABLE_TERMINATION_MODELS, on_delete=models.PROTECT, related_name='+' ) @@ -1950,7 +1950,7 @@ class Cable(ChangeLoggedModel): ) termination_b_type = models.ForeignKey( to=ContentType, - limit_choices_to={'model__in': CABLE_TERMINATION_TYPES}, + limit_choices_to=CABLE_TERMINATION_MODELS, on_delete=models.PROTECT, related_name='+' ) diff --git a/netbox/dcim/tests/test_api.py b/netbox/dcim/tests/test_api.py index dd0a6511f..13d3a48f2 100644 --- a/netbox/dcim/tests/test_api.py +++ b/netbox/dcim/tests/test_api.py @@ -30,7 +30,7 @@ class ChoicesTest(APITestCase): # Cable 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()) - content_types = ContentType.objects.filter(model__in=CABLE_TERMINATION_TYPES) + content_types = ContentType.objects.filter(CABLE_TERMINATION_MODELS) cable_termination_choices = { "{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types }