mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Refactor of webhook signaling system to use the same middleware mechanics of Changelogging
This commit is contained in:
committed by
Jeremy Stretch
parent
9876a2efcd
commit
722d0d5554
@@ -9,7 +9,11 @@ from django.conf import settings
|
||||
from django.db.models.signals import post_delete, post_save
|
||||
from django.utils import timezone
|
||||
|
||||
from .constants import OBJECTCHANGE_ACTION_CREATE, OBJECTCHANGE_ACTION_DELETE, OBJECTCHANGE_ACTION_UPDATE
|
||||
from extras.webhooks import enqueue_webhooks
|
||||
from .constants import (
|
||||
OBJECTCHANGE_ACTION_CREATE, OBJECTCHANGE_ACTION_DELETE, OBJECTCHANGE_ACTION_UPDATE,
|
||||
WEBHOOK_MODELS
|
||||
)
|
||||
from .models import ObjectChange
|
||||
|
||||
|
||||
@@ -18,12 +22,10 @@ _thread_locals = threading.local()
|
||||
|
||||
def mark_object_changed(instance, **kwargs):
|
||||
"""
|
||||
Mark an object as having been created, saved, or updated. At the end of the request, this change will be recorded.
|
||||
We have to wait until the *end* of the request to the serialize the object, because related fields like tags and
|
||||
custom fields have not yet been updated when the post_save signal is emitted.
|
||||
Mark an object as having been created, saved, or updated. At the end of the request, this change will be recorded
|
||||
and/or associated webhooks fired. We have to wait until the *end* of the request to the serialize the object,
|
||||
because related fields like tags and custom fields have not yet been updated when the post_save signal is emitted.
|
||||
"""
|
||||
if not hasattr(instance, 'log_change'):
|
||||
return
|
||||
|
||||
# Determine what action is being performed. The post_save signal sends a `created` boolean, whereas post_delete
|
||||
# does not.
|
||||
@@ -35,7 +37,12 @@ def mark_object_changed(instance, **kwargs):
|
||||
_thread_locals.changed_objects.append((instance, action))
|
||||
|
||||
|
||||
class ChangeLoggingMiddleware(object):
|
||||
class ObjectChangeMiddleware(object):
|
||||
"""
|
||||
This middleware intercepts all requests to connects object signals to the Django runtime. The signals collect all
|
||||
changed objects into a local thread by way of the `mark_object_changed()` receiver. At the end of the request,
|
||||
the middleware iterates over the objects to process change events like Change Logging and Webhooks.
|
||||
"""
|
||||
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
@@ -56,11 +63,16 @@ class ChangeLoggingMiddleware(object):
|
||||
# Process the request
|
||||
response = self.get_response(request)
|
||||
|
||||
# Record object changes
|
||||
# Perform change logging and fire Webhook signals
|
||||
for obj, action in _thread_locals.changed_objects:
|
||||
if obj.pk:
|
||||
# Log object changes
|
||||
if obj.pk and hasattr(obj, 'log_change'):
|
||||
obj.log_change(request.user, request.id, action)
|
||||
|
||||
# Enqueue Webhooks if they are enabled
|
||||
if settings.WEBHOOKS_ENABLED and obj.__class__.__name__.lower() in WEBHOOK_MODELS:
|
||||
enqueue_webhooks(obj, action)
|
||||
|
||||
# Housekeeping: 1% chance of clearing out expired ObjectChanges
|
||||
if _thread_locals.changed_objects and settings.CHANGELOG_RETENTION and random.randint(1, 100) == 1:
|
||||
cutoff = timezone.now() - timedelta(days=settings.CHANGELOG_RETENTION)
|
||||
|
Reference in New Issue
Block a user