1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #3451: Add pre-/post-change snapshots to webhooks

This commit is contained in:
Jeremy Stretch
2021-03-09 13:03:44 -05:00
parent c083b862a7
commit c6641ec1de
5 changed files with 70 additions and 25 deletions

View File

@@ -6,6 +6,7 @@ from django.utils import timezone
from django_rq import get_queue
from utilities.api import get_serializer_for_model
from utilities.utils import serialize_object
from .choices import *
from .models import Webhook
from .registry import registry
@@ -44,6 +45,7 @@ def enqueue_webhooks(instance, user, request_id, action):
webhooks = Webhook.objects.filter(content_types=content_type, enabled=True, **{action_flag: True})
if webhooks.exists():
# Get the Model's API serializer class and serialize the object
serializer_class = get_serializer_for_model(instance.__class__)
serializer_context = {
@@ -51,16 +53,23 @@ def enqueue_webhooks(instance, user, request_id, action):
}
serializer = serializer_class(instance, context=serializer_context)
# Gather pre- and post-change snapshots
snapshots = {
'prechange': getattr(instance, '_prechange_snapshot', None),
'postchange': serialize_object(instance) if action != ObjectChangeActionChoices.ACTION_DELETE else None,
}
# Enqueue the webhooks
webhook_queue = get_queue('default')
for webhook in webhooks:
webhook_queue.enqueue(
"extras.webhooks_worker.process_webhook",
webhook,
serializer.data,
instance._meta.model_name,
action,
str(timezone.now()),
user.username,
request_id
webhook=webhook,
model_name=instance._meta.model_name,
event=action,
data=serializer.data,
snapshots=snapshots,
timestamp=str(timezone.now()),
username=user.username,
request_id=request_id
)