mirror of
https://github.com/netbox-community/netbox.git
synced 2026-02-04 14:26:25 -06:00
* Closes #21303: Cache serialized post-change data on object * Set to_objectchange.alters_data * Restructure logic for determining post-change snapshot
This commit is contained in:
+19
-11
@@ -51,18 +51,26 @@ def serialize_for_event(instance):
|
|||||||
|
|
||||||
|
|
||||||
def get_snapshots(instance, event_type):
|
def get_snapshots(instance, event_type):
|
||||||
snapshots = {
|
"""
|
||||||
'prechange': getattr(instance, '_prechange_snapshot', None),
|
Return a dictionary of pre- and post-change snapshots for the given instance.
|
||||||
'postchange': None,
|
"""
|
||||||
}
|
if event_type == OBJECT_DELETED:
|
||||||
if event_type != OBJECT_DELETED:
|
# Post-change snapshot must be empty for deleted objects
|
||||||
# Use model's serialize_object() method if defined; fall back to serialize_object() utility function
|
postchange_snapshot = None
|
||||||
if hasattr(instance, 'serialize_object'):
|
elif hasattr(instance, '_postchange_snapshot'):
|
||||||
snapshots['postchange'] = instance.serialize_object()
|
# Use the cached post-change snapshot if one is available
|
||||||
else:
|
postchange_snapshot = instance._postchange_snapshot
|
||||||
snapshots['postchange'] = serialize_object(instance)
|
elif hasattr(instance, 'serialize_object'):
|
||||||
|
# Use model's serialize_object() method if defined
|
||||||
|
postchange_snapshot = instance.serialize_object()
|
||||||
|
else:
|
||||||
|
# Fall back to the serialize_object() utility function
|
||||||
|
postchange_snapshot = serialize_object(instance)
|
||||||
|
|
||||||
return snapshots
|
return {
|
||||||
|
'prechange': getattr(instance, '_prechange_snapshot', None),
|
||||||
|
'postchange': postchange_snapshot,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def enqueue_event(queue, instance, request, event_type):
|
def enqueue_event(queue, instance, request, event_type):
|
||||||
|
|||||||
@@ -121,9 +121,11 @@ class ChangeLoggingMixin(DeleteMixin, models.Model):
|
|||||||
if hasattr(self, '_prechange_snapshot'):
|
if hasattr(self, '_prechange_snapshot'):
|
||||||
objectchange.prechange_data = self._prechange_snapshot
|
objectchange.prechange_data = self._prechange_snapshot
|
||||||
if action in (ObjectChangeActionChoices.ACTION_CREATE, ObjectChangeActionChoices.ACTION_UPDATE):
|
if action in (ObjectChangeActionChoices.ACTION_CREATE, ObjectChangeActionChoices.ACTION_UPDATE):
|
||||||
objectchange.postchange_data = self.serialize_object(exclude=exclude)
|
self._postchange_snapshot = self.serialize_object(exclude=exclude)
|
||||||
|
objectchange.postchange_data = self._postchange_snapshot
|
||||||
|
|
||||||
return objectchange
|
return objectchange
|
||||||
|
to_objectchange.alters_data = True
|
||||||
|
|
||||||
|
|
||||||
class CloningMixin(models.Model):
|
class CloningMixin(models.Model):
|
||||||
|
|||||||
Reference in New Issue
Block a user