mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
#3892: Convert WEBHOOK_MODELS to a Q object
This commit is contained in:
@ -139,36 +139,48 @@ LOG_LEVEL_CODES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Models which support registered webhooks
|
# Models which support registered webhooks
|
||||||
WEBHOOK_MODELS = [
|
WEBHOOK_MODELS = Q(
|
||||||
'circuits.circuit',
|
Q(app_label='circuits', model__in=[
|
||||||
'circuits.provider',
|
'circuit',
|
||||||
'dcim.cable',
|
'provider',
|
||||||
'dcim.consoleport',
|
]) |
|
||||||
'dcim.consoleserverport',
|
Q(app_label='dcim', model__in=[
|
||||||
'dcim.device',
|
'cable',
|
||||||
'dcim.devicebay',
|
'consoleport',
|
||||||
'dcim.devicetype',
|
'consoleserverport',
|
||||||
'dcim.interface',
|
'device',
|
||||||
'dcim.inventoryitem',
|
'devicebay',
|
||||||
'dcim.frontport',
|
'devicetype',
|
||||||
'dcim.manufacturer',
|
'frontport',
|
||||||
'dcim.poweroutlet',
|
'interface',
|
||||||
'dcim.powerpanel',
|
'inventoryitem',
|
||||||
'dcim.powerport',
|
'manufacturer',
|
||||||
'dcim.powerfeed',
|
'poweroutlet',
|
||||||
'dcim.rack',
|
'powerpanel',
|
||||||
'dcim.rearport',
|
'powerport',
|
||||||
'dcim.region',
|
'powerfeed',
|
||||||
'dcim.site',
|
'rack',
|
||||||
'dcim.virtualchassis',
|
'rearport',
|
||||||
'ipam.aggregate',
|
'region',
|
||||||
'ipam.ipaddress',
|
'site',
|
||||||
'ipam.prefix',
|
'virtualchassis',
|
||||||
'ipam.service',
|
]) |
|
||||||
'ipam.vlan',
|
Q(app_label='ipam', model__in=[
|
||||||
'ipam.vrf',
|
'aggregate',
|
||||||
'secrets.secret',
|
'ipaddress',
|
||||||
'tenancy.tenant',
|
'prefix',
|
||||||
'virtualization.cluster',
|
'service',
|
||||||
'virtualization.virtualmachine',
|
'vlan',
|
||||||
]
|
'vrf',
|
||||||
|
]) |
|
||||||
|
Q(app_label='secrets', model__in=[
|
||||||
|
'secret',
|
||||||
|
]) |
|
||||||
|
Q(app_label='tenancy', model__in=[
|
||||||
|
'tenant',
|
||||||
|
]) |
|
||||||
|
Q(app_label='virtualization', model__in=[
|
||||||
|
'cluster',
|
||||||
|
'virtualmachine',
|
||||||
|
])
|
||||||
|
)
|
||||||
|
@ -43,6 +43,6 @@ class Migration(migrations.Migration):
|
|||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='webhook',
|
model_name='webhook',
|
||||||
name='obj_type',
|
name='obj_type',
|
||||||
field=models.ManyToManyField(limit_choices_to=extras.models.get_webhook_models, related_name='webhooks', to='contenttypes.ContentType'),
|
field=models.ManyToManyField(related_name='webhooks', to='contenttypes.ContentType'),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -125,7 +125,7 @@ class Migration(migrations.Migration):
|
|||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='webhook',
|
model_name='webhook',
|
||||||
name='obj_type',
|
name='obj_type',
|
||||||
field=models.ManyToManyField(limit_choices_to=extras.models.get_webhook_models, related_name='webhooks', to='contenttypes.ContentType'),
|
field=models.ManyToManyField(related_name='webhooks', to='contenttypes.ContentType'),
|
||||||
),
|
),
|
||||||
migrations.RunSQL(
|
migrations.RunSQL(
|
||||||
sql="SELECT setval('extras_tag_id_seq', (SELECT id FROM extras_tag ORDER BY id DESC LIMIT 1) + 1)",
|
sql="SELECT setval('extras_tag_id_seq', (SELECT id FROM extras_tag ORDER BY id DESC LIMIT 1) + 1)",
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Generated by Django 2.2.8 on 2020-01-15 21:11
|
# Generated by Django 2.2.8 on 2020-01-15 21:18
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
@ -31,4 +31,9 @@ class Migration(migrations.Migration):
|
|||||||
name='type',
|
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'),
|
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'),
|
||||||
),
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='webhook',
|
||||||
|
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', ['cable', 'consoleport', 'consoleserverport', 'device', 'devicebay', 'devicetype', 'frontport', 'interface', 'inventoryitem', 'manufacturer', 'poweroutlet', 'powerpanel', 'powerport', 'powerfeed', 'rack', 'rearport', 'region', 'site', 'virtualchassis'])), 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='webhooks', to='contenttypes.ContentType'),
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
@ -43,10 +43,6 @@ __all__ = (
|
|||||||
# 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
|
||||||
@ -58,7 +54,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=get_webhook_models,
|
limit_choices_to=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,6 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
from extras.models import Webhook
|
from extras.models import Webhook
|
||||||
@ -14,7 +13,10 @@ 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 instance._meta.label.lower() not in WEBHOOK_MODELS:
|
obj_type = ContentType.objects.get_for_model(instance.__class__)
|
||||||
|
|
||||||
|
webhook_models = ContentType.objects.filter(WEBHOOK_MODELS)
|
||||||
|
if obj_type not in webhook_models:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Retrieve any applicable Webhooks
|
# Retrieve any applicable Webhooks
|
||||||
@ -23,7 +25,6 @@ def enqueue_webhooks(instance, user, request_id, action):
|
|||||||
ObjectChangeActionChoices.ACTION_UPDATE: 'type_update',
|
ObjectChangeActionChoices.ACTION_UPDATE: 'type_update',
|
||||||
ObjectChangeActionChoices.ACTION_DELETE: 'type_delete',
|
ObjectChangeActionChoices.ACTION_DELETE: 'type_delete',
|
||||||
}[action]
|
}[action]
|
||||||
obj_type = ContentType.objects.get_for_model(instance.__class__)
|
|
||||||
webhooks = Webhook.objects.filter(obj_type=obj_type, enabled=True, **{action_flag: True})
|
webhooks = Webhook.objects.filter(obj_type=obj_type, enabled=True, **{action_flag: True})
|
||||||
|
|
||||||
if webhooks.exists():
|
if webhooks.exists():
|
||||||
|
Reference in New Issue
Block a user