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

Consolidate middleware under NetBox core

This commit is contained in:
Jeremy Stretch
2020-11-10 16:59:37 -05:00
parent c496b05630
commit db0c61dea4
4 changed files with 39 additions and 40 deletions

View File

@@ -1,32 +0,0 @@
import uuid
from .context_managers import change_logging
class ObjectChangeMiddleware(object):
"""
This middleware performs three functions in response to an object being created, updated, or deleted:
1. Create an ObjectChange to reflect the modification to the object in the changelog.
2. Enqueue any relevant webhooks.
3. Increment the metric counter for the event type.
The post_save and post_delete signals are employed to catch object modifications, however changes are recorded a bit
differently for each. Objects being saved are cached into thread-local storage for action *after* the response has
completed. This ensures that serialization of the object is performed only after any related objects (e.g. tags)
have been created. Conversely, deletions are acted upon immediately, so that the serialized representation of the
object is recorded before it (and any related objects) are actually deleted from the database.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# Assign a random unique ID to the request. This will be used to associate multiple object changes made during
# the same request.
request.id = uuid.uuid4()
# Process the request with change logging enabled
with change_logging(request):
response = self.get_response(request)
return response

View File

@@ -35,7 +35,7 @@ class ChangeLoggedModel(models.Model):
def to_objectchange(self, action):
"""
Return a new ObjectChange representing a change made to this object. This will typically be called automatically
by extras.middleware.ChangeLoggingMiddleware.
by ChangeLoggingMiddleware.
"""
return ObjectChange(
changed_object=self,