|
@@ -1,14 +1,15 @@
|
|
|
import random
|
|
import random
|
|
|
import threading
|
|
import threading
|
|
|
import uuid
|
|
import uuid
|
|
|
|
|
+from copy import deepcopy
|
|
|
from datetime import timedelta
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
from django.conf import settings
|
|
|
-from django.db.models.signals import post_delete, post_save
|
|
|
|
|
|
|
+from django.db.models.signals import pre_delete, post_save
|
|
|
from django.utils import timezone
|
|
from django.utils import timezone
|
|
|
-from django.utils.functional import curry
|
|
|
|
|
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
|
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
|
|
|
|
|
|
|
|
|
+from utilities.querysets import DummyQuerySet
|
|
|
from .constants import *
|
|
from .constants import *
|
|
|
from .models import ObjectChange
|
|
from .models import ObjectChange
|
|
|
from .signals import purge_changelog
|
|
from .signals import purge_changelog
|
|
@@ -19,33 +20,34 @@ _thread_locals = threading.local()
|
|
|
|
|
|
|
|
def handle_changed_object(sender, instance, **kwargs):
|
|
def handle_changed_object(sender, instance, **kwargs):
|
|
|
"""
|
|
"""
|
|
|
- Fires when an object is created or updated
|
|
|
|
|
|
|
+ Fires when an object is created or updated.
|
|
|
"""
|
|
"""
|
|
|
- # Queue the object and a new ObjectChange for processing once the request completes
|
|
|
|
|
- if hasattr(instance, 'to_objectchange'):
|
|
|
|
|
- action = OBJECTCHANGE_ACTION_CREATE if kwargs['created'] else OBJECTCHANGE_ACTION_UPDATE
|
|
|
|
|
- objectchange = instance.to_objectchange(action)
|
|
|
|
|
- _thread_locals.changed_objects.append(
|
|
|
|
|
- (instance, objectchange)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ # Queue the object for processing once the request completes
|
|
|
|
|
+ action = OBJECTCHANGE_ACTION_CREATE if kwargs['created'] else OBJECTCHANGE_ACTION_UPDATE
|
|
|
|
|
+ _thread_locals.changed_objects.append(
|
|
|
|
|
+ (instance, action)
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
|
|
|
-def _handle_deleted_object(request, sender, instance, **kwargs):
|
|
|
|
|
|
|
+def handle_deleted_object(sender, instance, **kwargs):
|
|
|
"""
|
|
"""
|
|
|
- Fires when an object is deleted
|
|
|
|
|
|
|
+ Fires when an object is deleted.
|
|
|
"""
|
|
"""
|
|
|
- # Record an Object Change
|
|
|
|
|
- if hasattr(instance, 'to_objectchange'):
|
|
|
|
|
- objectchange = instance.to_objectchange(OBJECTCHANGE_ACTION_DELETE)
|
|
|
|
|
- objectchange.user = request.user
|
|
|
|
|
- objectchange.request_id = request.id
|
|
|
|
|
- objectchange.save()
|
|
|
|
|
|
|
+ # Cache custom fields prior to copying the instance
|
|
|
|
|
+ if hasattr(instance, 'cache_custom_fields'):
|
|
|
|
|
+ instance.cache_custom_fields()
|
|
|
|
|
|
|
|
- # Enqueue webhooks
|
|
|
|
|
- enqueue_webhooks(instance, request.user, request.id, OBJECTCHANGE_ACTION_DELETE)
|
|
|
|
|
|
|
+ # Create a copy of the object being deleted
|
|
|
|
|
+ copy = deepcopy(instance)
|
|
|
|
|
|
|
|
- # Increment metric counters
|
|
|
|
|
- model_deletes.labels(instance._meta.model_name).inc()
|
|
|
|
|
|
|
+ # Preserve tags
|
|
|
|
|
+ if hasattr(instance, 'tags'):
|
|
|
|
|
+ copy.tags = DummyQuerySet(instance.tags.all())
|
|
|
|
|
+
|
|
|
|
|
+ # Queue the copy of the object for processing once the request completes
|
|
|
|
|
+ _thread_locals.changed_objects.append(
|
|
|
|
|
+ (copy, OBJECTCHANGE_ACTION_DELETE)
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
|
|
|
def purge_objectchange_cache(sender, **kwargs):
|
|
def purge_objectchange_cache(sender, **kwargs):
|
|
@@ -81,12 +83,9 @@ class ObjectChangeMiddleware(object):
|
|
|
# the same request.
|
|
# the same request.
|
|
|
request.id = uuid.uuid4()
|
|
request.id = uuid.uuid4()
|
|
|
|
|
|
|
|
- # Signals don't include the request context, so we're currying it into the post_delete function ahead of time.
|
|
|
|
|
- handle_deleted_object = curry(_handle_deleted_object, request)
|
|
|
|
|
-
|
|
|
|
|
# Connect our receivers to the post_save and post_delete signals.
|
|
# Connect our receivers to the post_save and post_delete signals.
|
|
|
- post_save.connect(handle_changed_object, dispatch_uid='cache_changed_object')
|
|
|
|
|
- post_delete.connect(handle_deleted_object, dispatch_uid='cache_deleted_object')
|
|
|
|
|
|
|
+ post_save.connect(handle_changed_object, dispatch_uid='handle_changed_object')
|
|
|
|
|
+ pre_delete.connect(handle_deleted_object, dispatch_uid='handle_deleted_object')
|
|
|
|
|
|
|
|
# Provide a hook for purging the change cache
|
|
# Provide a hook for purging the change cache
|
|
|
purge_changelog.connect(purge_objectchange_cache)
|
|
purge_changelog.connect(purge_objectchange_cache)
|
|
@@ -98,22 +97,31 @@ class ObjectChangeMiddleware(object):
|
|
|
if not _thread_locals.changed_objects:
|
|
if not _thread_locals.changed_objects:
|
|
|
return response
|
|
return response
|
|
|
|
|
|
|
|
- # Create records for any cached objects that were created/updated.
|
|
|
|
|
- for obj, objectchange in _thread_locals.changed_objects:
|
|
|
|
|
|
|
+ # Create records for any cached objects that were changed.
|
|
|
|
|
+ for instance, action in _thread_locals.changed_objects:
|
|
|
|
|
+
|
|
|
|
|
+ # Refresh cached custom field values
|
|
|
|
|
+ if action in [OBJECTCHANGE_ACTION_CREATE, OBJECTCHANGE_ACTION_UPDATE]:
|
|
|
|
|
+ if hasattr(instance, 'cache_custom_fields'):
|
|
|
|
|
+ instance.cache_custom_fields()
|
|
|
|
|
|
|
|
- # Record the change
|
|
|
|
|
- objectchange.user = request.user
|
|
|
|
|
- objectchange.request_id = request.id
|
|
|
|
|
- objectchange.save()
|
|
|
|
|
|
|
+ # Record an ObjectChange if applicable
|
|
|
|
|
+ if hasattr(instance, 'to_objectchange'):
|
|
|
|
|
+ objectchange = instance.to_objectchange(action)
|
|
|
|
|
+ objectchange.user = request.user
|
|
|
|
|
+ objectchange.request_id = request.id
|
|
|
|
|
+ objectchange.save()
|
|
|
|
|
|
|
|
# Enqueue webhooks
|
|
# Enqueue webhooks
|
|
|
- enqueue_webhooks(obj, request.user, request.id, objectchange.action)
|
|
|
|
|
|
|
+ enqueue_webhooks(instance, request.user, request.id, action)
|
|
|
|
|
|
|
|
# Increment metric counters
|
|
# Increment metric counters
|
|
|
- if objectchange.action == OBJECTCHANGE_ACTION_CREATE:
|
|
|
|
|
- model_inserts.labels(obj._meta.model_name).inc()
|
|
|
|
|
- elif objectchange.action == OBJECTCHANGE_ACTION_UPDATE:
|
|
|
|
|
- model_updates.labels(obj._meta.model_name).inc()
|
|
|
|
|
|
|
+ if action == OBJECTCHANGE_ACTION_CREATE:
|
|
|
|
|
+ model_inserts.labels(instance._meta.model_name).inc()
|
|
|
|
|
+ elif action == OBJECTCHANGE_ACTION_UPDATE:
|
|
|
|
|
+ model_updates.labels(instance._meta.model_name).inc()
|
|
|
|
|
+ elif action == OBJECTCHANGE_ACTION_DELETE:
|
|
|
|
|
+ model_deletes.labels(instance._meta.model_name).inc()
|
|
|
|
|
|
|
|
# Housekeeping: 1% chance of clearing out expired ObjectChanges. This applies only to requests which result in
|
|
# Housekeeping: 1% chance of clearing out expired ObjectChanges. This applies only to requests which result in
|
|
|
# one or more changes being logged.
|
|
# one or more changes being logged.
|