Browse Source

#3892: Convert GRAPH_MODELS to a Q object

Jeremy Stretch 6 years ago
parent
commit
f81e7d30e2

+ 1 - 1
netbox/extras/api/serializers.py

@@ -30,7 +30,7 @@ from .nested_serializers import *
 
 class GraphSerializer(ValidatedModelSerializer):
     type = ContentTypeField(
-        queryset=ContentType.objects.filter(**model_names_to_filter_dict(GRAPH_MODELS)),
+        queryset=ContentType.objects.filter(GRAPH_MODELS),
     )
 
     class Meta:

+ 9 - 5
netbox/extras/constants.py

@@ -70,11 +70,15 @@ CUSTOMLINK_MODELS = Q(
 )
 
 # Models which can have Graphs associated with them
-GRAPH_MODELS = (
-    'circuits.provider',
-    'dcim.device',
-    'dcim.interface',
-    'dcim.site',
+GRAPH_MODELS = Q(
+    Q(app_label='circuits', model__in=[
+        'provider',
+    ]) |
+    Q(app_label='dcim', model__in=[
+        'device',
+        'interface',
+        'site',
+    ])
 )
 
 # Models which support export templates

+ 6 - 1
netbox/extras/migrations/0036_contenttype_filters_to_q_objects.py

@@ -1,4 +1,4 @@
-# Generated by Django 2.2.8 on 2020-01-15 21:04
+# Generated by Django 2.2.8 on 2020-01-15 21:07
 
 from django.db import migrations, models
 import django.db.models.deletion
@@ -21,4 +21,9 @@ class Migration(migrations.Migration):
             name='content_type',
             field=models.ForeignKey(limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'circuits'), ('model__in', ['circuit', 'provider'])), models.Q(('app_label', 'dcim'), ('model__in', ['cable', 'device', 'devicetype', 'powerpanel', 'powerfeed', 'rack', 'site'])), models.Q(('app_label', 'ipam'), ('model__in', ['aggregate', 'ipaddress', 'prefix', 'service', 'vlan', 'vrf'])), models.Q(('app_label', 'secrets'), ('model__in', ['secret'])), models.Q(('app_label', 'tenancy'), ('model__in', ['tenant'])), models.Q(('app_label', 'virtualization'), ('model__in', ['cluster', 'virtualmachine'])), _connector='OR')), on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
         ),
+        migrations.AlterField(
+            model_name='graph',
+            name='type',
+            field=models.ForeignKey(limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'circuits'), ('model__in', ['provider'])), models.Q(('app_label', 'dcim'), ('model__in', ['device', 'interface', 'site'])), _connector='OR')), on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
+        ),
     ]

+ 1 - 1
netbox/extras/models.py

@@ -423,7 +423,7 @@ class Graph(models.Model):
     type = models.ForeignKey(
         to=ContentType,
         on_delete=models.CASCADE,
-        limit_choices_to=model_names_to_filter_dict(GRAPH_MODELS)
+        limit_choices_to=GRAPH_MODELS
     )
     weight = models.PositiveSmallIntegerField(
         default=1000

+ 1 - 1
netbox/extras/tests/test_api.py

@@ -29,7 +29,7 @@ class ChoicesTest(APITestCase):
         self.assertEqual(choices_to_dict(response.data.get('export-template:template_language')), TemplateLanguageChoices.as_dict())
 
         # Graph
-        content_types = ContentType.objects.filter(**model_names_to_filter_dict(GRAPH_MODELS))
+        content_types = ContentType.objects.filter(GRAPH_MODELS)
         graph_type_choices = {
             "{}.{}".format(ct.app_label, ct.model): ct.name for ct in content_types
         }