Просмотр исходного кода

#3892: Convert CUSTOMLINK_MODELS to a Q object

Jeremy Stretch 6 лет назад
Родитель
Сommit
09bee75cb3

+ 33 - 21
netbox/extras/constants.py

@@ -35,27 +35,39 @@ CUSTOMFIELD_MODELS = Q(
 )
 )
 
 
 # Custom links
 # Custom links
-CUSTOMLINK_MODELS = [
-    'circuits.circuit',
-    'circuits.provider',
-    'dcim.cable',
-    'dcim.device',
-    'dcim.devicetype',
-    'dcim.powerpanel',
-    'dcim.powerfeed',
-    'dcim.rack',
-    'dcim.site',
-    'ipam.aggregate',
-    'ipam.ipaddress',
-    'ipam.prefix',
-    'ipam.service',
-    'ipam.vlan',
-    'ipam.vrf',
-    'secrets.secret',
-    'tenancy.tenant',
-    'virtualization.cluster',
-    'virtualization.virtualmachine',
-]
+CUSTOMLINK_MODELS = Q(
+    Q(app_label='circuits', model__in=[
+        'circuit',
+        'provider',
+    ]) |
+    Q(app_label='dcim', model__in=[
+        'cable',
+        'device',
+        'devicetype',
+        'powerpanel',
+        'powerfeed',
+        'rack',
+        'site',
+    ]) |
+    Q(app_label='ipam', model__in=[
+        'aggregate',
+        'ipaddress',
+        'prefix',
+        'service',
+        'vlan',
+        'vrf',
+    ]) |
+    Q(app_label='secrets', model__in=[
+        'secret',
+    ]) |
+    Q(app_label='tenancy', model__in=[
+        'tenant',
+    ]) |
+    Q(app_label='virtualization', model__in=[
+        'cluster',
+        'virtualmachine',
+    ])
+)
 
 
 # Models which can have Graphs associated with them
 # Models which can have Graphs associated with them
 GRAPH_MODELS = (
 GRAPH_MODELS = (

+ 1 - 1
netbox/extras/migrations/0022_custom_links.py

@@ -22,7 +22,7 @@ class Migration(migrations.Migration):
                 ('group_name', models.CharField(blank=True, max_length=50)),
                 ('group_name', models.CharField(blank=True, max_length=50)),
                 ('button_class', models.CharField(default='default', max_length=30)),
                 ('button_class', models.CharField(default='default', max_length=30)),
                 ('new_window', models.BooleanField()),
                 ('new_window', models.BooleanField()),
-                ('content_type', models.ForeignKey(limit_choices_to=extras.models.get_custom_link_models, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
+                ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
             ],
             ],
             options={
             options={
                 'ordering': ['group_name', 'weight', 'name'],
                 'ordering': ['group_name', 'weight', 'name'],

+ 1 - 1
netbox/extras/migrations/0022_custom_links_squashed_0034_configcontext_tags.py

@@ -106,7 +106,7 @@ class Migration(migrations.Migration):
                 ('group_name', models.CharField(blank=True, max_length=50)),
                 ('group_name', models.CharField(blank=True, max_length=50)),
                 ('button_class', models.CharField(default='default', max_length=30)),
                 ('button_class', models.CharField(default='default', max_length=30)),
                 ('new_window', models.BooleanField()),
                 ('new_window', models.BooleanField()),
-                ('content_type', models.ForeignKey(limit_choices_to=extras.models.get_custom_link_models, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
+                ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
             ],
             ],
             options={
             options={
                 'ordering': ['group_name', 'weight', 'name'],
                 'ordering': ['group_name', 'weight', 'name'],

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

@@ -1,6 +1,7 @@
-# Generated by Django 2.2.8 on 2020-01-15 21:00
+# Generated by Django 2.2.8 on 2020-01-15 21:04
 
 
 from django.db import migrations, models
 from django.db import migrations, models
+import django.db.models.deletion
 
 
 
 
 class Migration(migrations.Migration):
 class Migration(migrations.Migration):
@@ -15,4 +16,9 @@ class Migration(migrations.Migration):
             name='obj_type',
             name='obj_type',
             field=models.ManyToManyField(limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'circuits'), ('model__in', ['circuit', 'provider'])), models.Q(('app_label', 'dcim'), ('model__in', ['device', 'devicetype', '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')), related_name='custom_fields', to='contenttypes.ContentType'),
             field=models.ManyToManyField(limit_choices_to=models.Q(models.Q(models.Q(('app_label', 'circuits'), ('model__in', ['circuit', 'provider'])), models.Q(('app_label', 'dcim'), ('model__in', ['device', 'devicetype', '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')), related_name='custom_fields', to='contenttypes.ContentType'),
         ),
         ),
+        migrations.AlterField(
+            model_name='customlink',
+            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'),
+        ),
     ]
     ]

+ 1 - 5
netbox/extras/models.py

@@ -367,10 +367,6 @@ class CustomFieldChoice(models.Model):
 # Custom links
 # Custom links
 #
 #
 
 
-def get_custom_link_models():
-    return model_names_to_filter_dict(CUSTOMLINK_MODELS)
-
-
 class CustomLink(models.Model):
 class CustomLink(models.Model):
     """
     """
     A custom link to an external representation of a NetBox object. The link text and URL fields accept Jinja2 template
     A custom link to an external representation of a NetBox object. The link text and URL fields accept Jinja2 template
@@ -379,7 +375,7 @@ class CustomLink(models.Model):
     content_type = models.ForeignKey(
     content_type = models.ForeignKey(
         to=ContentType,
         to=ContentType,
         on_delete=models.CASCADE,
         on_delete=models.CASCADE,
-        limit_choices_to=get_custom_link_models
+        limit_choices_to=CUSTOMLINK_MODELS
     )
     )
     name = models.CharField(
     name = models.CharField(
         max_length=100,
         max_length=100,