Explorar el Código

Clean up, update Webhook models

Jeremy Stretch hace 6 años
padre
commit
2f3c39295c

+ 33 - 10
netbox/extras/constants.py

@@ -195,13 +195,36 @@ WEBHOOK_CT_CHOICES = (
 )
 )
 
 
 # Models which support registered webhooks
 # Models which support registered webhooks
-WEBHOOK_MODELS = (
-    'provider', 'circuit',                                           # Circuits
-    'site', 'rack', 'devicetype', 'device', 'virtualchassis',        # DCIM
-    'consoleport', 'consoleserverport', 'powerport', 'poweroutlet',
-    'interface', 'devicebay', 'inventoryitem',
-    'aggregate', 'prefix', 'ipaddress', 'vlan', 'vrf', 'service',    # IPAM
-    'secret',                                                        # Secrets
-    'tenant',                                                        # Tenancy
-    'cluster', 'virtualmachine',                                     # Virtualization
-)
+WEBHOOK_MODELS = [
+    'circuits.circuit',
+    'circuits.provider',
+    'dcim.cable',
+    'dcim.consoleport',
+    'dcim.consoleserverport',
+    'dcim.device',
+    'dcim.devicebay',
+    'dcim.devicetype',
+    'dcim.interface',
+    'dcim.inventoryitem',
+    'dcim.frontport',
+    'dcim.manufacturer',
+    'dcim.poweroutlet',
+    'dcim.powerpanel',
+    'dcim.powerport',
+    'dcim.powerfeed',
+    'dcim.rack',
+    'dcim.rearport',
+    'dcim.region',
+    'dcim.site',
+    'dcim.virtualchassis',
+    'ipam.aggregate',
+    'ipam.ipaddress',
+    'ipam.prefix',
+    'ipam.service',
+    'ipam.vlan',
+    'ipam.vrf',
+    'secrets.secret',
+    'tenancy.tenant',
+    'virtualization.cluster',
+    'virtualization.virtualmachine',
+]

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

@@ -29,7 +29,7 @@ class Migration(migrations.Migration):
             },
             },
         ),
         ),
 
 
-        # Update limit_choices_to for CustomFields and ExportTemplates
+        # Update limit_choices_to for CustomFields, ExportTemplates, and Webhooks
         migrations.AlterField(
         migrations.AlterField(
             model_name='customfield',
             model_name='customfield',
             name='obj_type',
             name='obj_type',
@@ -40,4 +40,9 @@ class Migration(migrations.Migration):
             name='content_type',
             name='content_type',
             field=models.ForeignKey(limit_choices_to=extras.models.get_export_template_models, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
             field=models.ForeignKey(limit_choices_to=extras.models.get_export_template_models, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
         ),
         ),
+        migrations.AlterField(
+            model_name='webhook',
+            name='obj_type',
+            field=models.ManyToManyField(limit_choices_to=extras.models.get_webhook_models, related_name='webhooks', to='contenttypes.ContentType'),
+        ),
     ]
     ]

+ 5 - 1
netbox/extras/models.py

@@ -26,6 +26,10 @@ from .querysets import ConfigContextQuerySet
 # Webhooks
 # Webhooks
 #
 #
 
 
+def get_webhook_models():
+    return model_names_to_filter_dict(WEBHOOK_MODELS)
+
+
 class Webhook(models.Model):
 class Webhook(models.Model):
     """
     """
     A Webhook defines a request that will be sent to a remote application when an object is created, updated, and/or
     A Webhook defines a request that will be sent to a remote application when an object is created, updated, and/or
@@ -37,7 +41,7 @@ class Webhook(models.Model):
         to=ContentType,
         to=ContentType,
         related_name='webhooks',
         related_name='webhooks',
         verbose_name='Object types',
         verbose_name='Object types',
-        limit_choices_to={'model__in': WEBHOOK_MODELS},
+        limit_choices_to=get_webhook_models,
         help_text="The object(s) to which this Webhook applies."
         help_text="The object(s) to which this Webhook applies."
     )
     )
     name = models.CharField(
     name = models.CharField(

+ 1 - 1
netbox/extras/webhooks.py

@@ -14,7 +14,7 @@ def enqueue_webhooks(instance, user, request_id, action):
     Find Webhook(s) assigned to this instance + action and enqueue them
     Find Webhook(s) assigned to this instance + action and enqueue them
     to be processed
     to be processed
     """
     """
-    if not settings.WEBHOOKS_ENABLED or instance._meta.model_name not in WEBHOOK_MODELS:
+    if not settings.WEBHOOKS_ENABLED or instance._meta.label.lower() not in WEBHOOK_MODELS:
         return
         return
 
 
     # Retrieve any applicable Webhooks
     # Retrieve any applicable Webhooks