mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 19:22:53 -06:00
Merge branch 'develop-2.4' of https://github.com/digitalocean/netbox into develop-2.4
This commit is contained in:
commit
acfbe9c1b1
@ -9,8 +9,6 @@ class ExtrasConfig(AppConfig):
|
|||||||
name = "extras"
|
name = "extras"
|
||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
import extras.signals
|
|
||||||
|
|
||||||
# Check that we can connect to the configured Redis database if webhooks are enabled.
|
# Check that we can connect to the configured Redis database if webhooks are enabled.
|
||||||
if settings.WEBHOOKS_ENABLED:
|
if settings.WEBHOOKS_ENABLED:
|
||||||
try:
|
try:
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db.models.signals import post_delete, post_save
|
|
||||||
from django.dispatch import receiver
|
|
||||||
from django.core.cache import caches
|
|
||||||
|
|
||||||
from .models import Webhook
|
|
||||||
|
|
||||||
|
|
||||||
@receiver((post_save, post_delete), sender=Webhook)
|
|
||||||
def update_webhook_cache(**kwargs):
|
|
||||||
"""
|
|
||||||
When a Webhook has been modified, update the webhook cache.
|
|
||||||
"""
|
|
||||||
cache = caches['default']
|
|
||||||
cache.set('webhook_cache', Webhook.objects.all())
|
|
@ -4,30 +4,12 @@ from importlib import import_module
|
|||||||
from django.db.models.signals import post_save, post_delete
|
from django.db.models.signals import post_save, post_delete
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import caches
|
from django.core.cache import caches
|
||||||
|
from django.db.models import Q
|
||||||
from django.dispatch import Signal
|
from django.dispatch import Signal
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
from utilities.utils import dynamic_import
|
from utilities.utils import dynamic_import
|
||||||
from .models import Webhook
|
from extras.models import Webhook
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# Webhooks signals regiters and receivers
|
|
||||||
#
|
|
||||||
|
|
||||||
def get_or_set_webhook_cache():
|
|
||||||
"""
|
|
||||||
Retrieve the webhook cache. If it is None set it to the current
|
|
||||||
Webhook queryset
|
|
||||||
"""
|
|
||||||
cache = caches['default']
|
|
||||||
webhook_cache = cache.get('webhook_cache', None)
|
|
||||||
|
|
||||||
if webhook_cache is None:
|
|
||||||
webhook_cache = Webhook.objects.all()
|
|
||||||
cache.set('webhook_cache', webhook_cache)
|
|
||||||
|
|
||||||
return webhook_cache
|
|
||||||
|
|
||||||
|
|
||||||
def enqueue_webhooks(webhooks, model_class, data, event, signal_received_timestamp):
|
def enqueue_webhooks(webhooks, model_class, data, event, signal_received_timestamp):
|
||||||
@ -66,18 +48,17 @@ def post_save_receiver(sender, instance, created, **kwargs):
|
|||||||
"""
|
"""
|
||||||
if settings.WEBHOOKS_ENABLED:
|
if settings.WEBHOOKS_ENABLED:
|
||||||
signal_received_timestamp = time.time()
|
signal_received_timestamp = time.time()
|
||||||
webhook_cache = get_or_set_webhook_cache()
|
|
||||||
# look for any webhooks that match this event
|
# look for any webhooks that match this event
|
||||||
updated = not created
|
updated = not created
|
||||||
obj_type = ContentType.objects.get_for_model(sender)
|
obj_type = ContentType.objects.get_for_model(sender)
|
||||||
webhooks = [
|
webhooks = Webhook.objects.filter(
|
||||||
x
|
Q(enabled=True) &
|
||||||
for x in webhook_cache
|
(
|
||||||
if (
|
Q(type_create=created) |
|
||||||
x.enabled and x.type_create == created or x.type_update == updated and
|
Q(type_update=updated)
|
||||||
obj_type in x.obj_type.all()
|
) &
|
||||||
|
Q(obj_type=obj_type)
|
||||||
)
|
)
|
||||||
]
|
|
||||||
event = 'created' if created else 'updated'
|
event = 'created' if created else 'updated'
|
||||||
if webhooks:
|
if webhooks:
|
||||||
enqueue_webhooks(webhooks, sender, instance, event, signal_received_timestamp)
|
enqueue_webhooks(webhooks, sender, instance, event, signal_received_timestamp)
|
||||||
@ -90,10 +71,9 @@ def post_delete_receiver(sender, instance, **kwargs):
|
|||||||
"""
|
"""
|
||||||
if settings.WEBHOOKS_ENABLED:
|
if settings.WEBHOOKS_ENABLED:
|
||||||
signal_received_timestamp = time.time()
|
signal_received_timestamp = time.time()
|
||||||
webhook_cache = get_or_set_webhook_cache()
|
|
||||||
obj_type = ContentType.objects.get_for_model(sender)
|
obj_type = ContentType.objects.get_for_model(sender)
|
||||||
# look for any webhooks that match this event
|
# look for any webhooks that match this event
|
||||||
webhooks = [x for x in webhook_cache if x.enabled and x.type_delete and obj_type in x.obj_type.all()]
|
webhooks = Webhook.objects.filter(enabled=True, type_delete=True, obj_type=obj_type)
|
||||||
if webhooks:
|
if webhooks:
|
||||||
enqueue_webhooks(webhooks, sender, instance, 'deleted', signal_received_timestamp)
|
enqueue_webhooks(webhooks, sender, instance, 'deleted', signal_received_timestamp)
|
||||||
|
|
||||||
@ -106,15 +86,14 @@ def bulk_operation_receiver(sender, **kwargs):
|
|||||||
if settings.WEBHOOKS_ENABLED:
|
if settings.WEBHOOKS_ENABLED:
|
||||||
signal_received_timestamp = time.time()
|
signal_received_timestamp = time.time()
|
||||||
event = kwargs['event']
|
event = kwargs['event']
|
||||||
webhook_cache = get_or_set_webhook_cache()
|
|
||||||
obj_type = ContentType.objects.get_for_model(sender)
|
obj_type = ContentType.objects.get_for_model(sender)
|
||||||
# look for any webhooks that match this event
|
# look for any webhooks that match this event
|
||||||
if event == 'created':
|
if event == 'created':
|
||||||
webhooks = [x for x in webhook_cache if x.enabled and x.type_create and obj_type in x.obj_type.all()]
|
webhooks = Webhook.objects.filter(enabled=True, type_create=True, obj_type=obj_type)
|
||||||
elif event == 'updated':
|
elif event == 'updated':
|
||||||
webhooks = [x for x in webhook_cache if x.enabled and x.type_update and obj_type in x.obj_type.all()]
|
webhooks = Webhook.objects.filter(enabled=True, type_update=True, obj_type=obj_type)
|
||||||
elif event == 'deleted':
|
elif event == 'deleted':
|
||||||
webhooks = [x for x in webhook_cache if x.enabled and x.type_delete and obj_type in x.obj_type.all()]
|
webhooks = Webhook.objects.filter(enabled=True, type_delete=True, obj_type=obj_type)
|
||||||
else:
|
else:
|
||||||
webhooks = None
|
webhooks = None
|
||||||
|
|
||||||
|
@ -304,14 +304,6 @@ INTERNAL_IPS = (
|
|||||||
'::1',
|
'::1',
|
||||||
)
|
)
|
||||||
|
|
||||||
# Django CACHE - local memory cache
|
|
||||||
CACHES = {
|
|
||||||
'default': {
|
|
||||||
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
|
||||||
'LOCATION': 'webhooks',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
HOSTNAME = socket.gethostname()
|
HOSTNAME = socket.gethostname()
|
||||||
|
Loading…
Reference in New Issue
Block a user