From 2f3c39295c83a74e4d8546da8571eba66379147f Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 17 Apr 2019 14:19:57 -0400 Subject: [PATCH] Clean up, update Webhook models --- netbox/extras/constants.py | 43 ++++++++++++++----- netbox/extras/migrations/0022_custom_links.py | 7 ++- netbox/extras/models.py | 6 ++- netbox/extras/webhooks.py | 2 +- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/netbox/extras/constants.py b/netbox/extras/constants.py index cf1822352..b72ae8c08 100644 --- a/netbox/extras/constants.py +++ b/netbox/extras/constants.py @@ -195,13 +195,36 @@ WEBHOOK_CT_CHOICES = ( ) # 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', +] diff --git a/netbox/extras/migrations/0022_custom_links.py b/netbox/extras/migrations/0022_custom_links.py index 45f43059d..f94ca670f 100644 --- a/netbox/extras/migrations/0022_custom_links.py +++ b/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( model_name='customfield', name='obj_type', @@ -40,4 +40,9 @@ class Migration(migrations.Migration): 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'), ), + 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'), + ), ] diff --git a/netbox/extras/models.py b/netbox/extras/models.py index 49a4f7022..7acc82fdc 100644 --- a/netbox/extras/models.py +++ b/netbox/extras/models.py @@ -26,6 +26,10 @@ from .querysets import ConfigContextQuerySet # Webhooks # +def get_webhook_models(): + return model_names_to_filter_dict(WEBHOOK_MODELS) + + 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 @@ -37,7 +41,7 @@ class Webhook(models.Model): to=ContentType, related_name='webhooks', 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." ) name = models.CharField( diff --git a/netbox/extras/webhooks.py b/netbox/extras/webhooks.py index 1ad050866..46090fb2e 100644 --- a/netbox/extras/webhooks.py +++ b/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 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 # Retrieve any applicable Webhooks