Просмотр исходного кода

Consolidate middleware under NetBox core

Jeremy Stretch 5 лет назад
Родитель
Сommit
db0c61dea4

+ 0 - 32
netbox/extras/middleware.py

@@ -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

+ 1 - 1
netbox/extras/models/change_logging.py

@@ -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,

+ 33 - 2
netbox/utilities/middleware.py → netbox/netbox/middleware.py

@@ -1,3 +1,4 @@
+import uuid
 from urllib import parse
 
 from django.conf import settings
@@ -6,8 +7,9 @@ from django.db import ProgrammingError
 from django.http import Http404, HttpResponseRedirect
 from django.urls import reverse
 
-from .api import is_api_request
-from .views import server_error, rest_api_server_error
+from extras.context_managers import change_logging
+from utilities.api import is_api_request
+from utilities.views import server_error, rest_api_server_error
 
 
 class LoginRequiredMiddleware(object):
@@ -51,6 +53,35 @@ class RemoteUserMiddleware(RemoteUserMiddleware_):
         return super().process_request(request)
 
 
+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
+
+
 class APIVersionMiddleware(object):
     """
     If the request is for an API endpoint, include the API version as a response header.

+ 5 - 5
netbox/netbox/settings.py

@@ -309,11 +309,11 @@ MIDDLEWARE = [
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'django.middleware.security.SecurityMiddleware',
-    'utilities.middleware.ExceptionHandlingMiddleware',
-    'utilities.middleware.RemoteUserMiddleware',
-    'utilities.middleware.LoginRequiredMiddleware',
-    'utilities.middleware.APIVersionMiddleware',
-    'extras.middleware.ObjectChangeMiddleware',
+    'netbox.middleware.ExceptionHandlingMiddleware',
+    'netbox.middleware.RemoteUserMiddleware',
+    'netbox.middleware.LoginRequiredMiddleware',
+    'netbox.middleware.APIVersionMiddleware',
+    'netbox.middleware.ObjectChangeMiddleware',
     'django_prometheus.middleware.PrometheusAfterMiddleware',
 ]