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