14147 add exclude_fields to serialize_object

This commit is contained in:
Arthur 2023-12-14 10:35:26 -08:00
parent 68232511a0
commit c0e6f6172f
2 changed files with 17 additions and 7 deletions

View File

@ -64,19 +64,23 @@ class ChangeLoggingMixin(models.Model):
class Meta: class Meta:
abstract = True abstract = True
def serialize_object(self): def serialize_object(self, exclude_fields=[]):
""" """
Return a JSON representation of the instance. Models can override this method to replace or extend the default Return a JSON representation of the instance. Models can override this method to replace or extend the default
serialization logic provided by the `serialize_object()` utility function. serialization logic provided by the `serialize_object()` utility function.
""" """
return serialize_object(self) return serialize_object(self, exclude_fields=exclude_fields)
def snapshot(self): def snapshot(self):
""" """
Save a snapshot of the object's current state in preparation for modification. The snapshot is saved as Save a snapshot of the object's current state in preparation for modification. The snapshot is saved as
`_prechange_snapshot` on the instance. `_prechange_snapshot` on the instance.
""" """
self._prechange_snapshot = self.serialize_object() exclude_fields = []
if get_config().CHANGELOG_SKIP_EMPTY_CHANGES:
exclude_fields = ['last_updated',]
self._prechange_snapshot = self.serialize_object(exclude_fields=exclude_fields)
snapshot.alters_data = True snapshot.alters_data = True
def to_objectchange(self, action): def to_objectchange(self, action):
@ -86,9 +90,13 @@ class ChangeLoggingMixin(models.Model):
""" """
from extras.models import ObjectChange from extras.models import ObjectChange
exclude_fields = []
if get_config().CHANGELOG_SKIP_EMPTY_CHANGES:
exclude_fields = ['last_updated',]
postchange_data = None postchange_data = None
if action in (ObjectChangeActionChoices.ACTION_CREATE, ObjectChangeActionChoices.ACTION_UPDATE): if action in (ObjectChangeActionChoices.ACTION_CREATE, ObjectChangeActionChoices.ACTION_UPDATE):
postchange_data = self.serialize_object() postchange_data = self.serialize_object(exclude_fields=exclude_fields)
if get_config().CHANGELOG_SKIP_EMPTY_CHANGES and action == ObjectChangeActionChoices.ACTION_UPDATE and hasattr(self, '_prechange_snapshot'): if get_config().CHANGELOG_SKIP_EMPTY_CHANGES and action == ObjectChangeActionChoices.ACTION_UPDATE and hasattr(self, '_prechange_snapshot'):
if postchange_data == self._prechange_snapshot: if postchange_data == self._prechange_snapshot:

View File

@ -144,7 +144,7 @@ def count_related(model, field):
return Coalesce(subquery, 0) return Coalesce(subquery, 0)
def serialize_object(obj, resolve_tags=True, extra=None): def serialize_object(obj, resolve_tags=True, extra=None, exclude_fields=[]):
""" """
Return a generic JSON representation of an object using Django's built-in serializer. (This is used for things like Return a generic JSON representation of an object using Django's built-in serializer. (This is used for things like
change logging, not the REST API.) Optionally include a dictionary to supplement the object data. A list of keys change logging, not the REST API.) Optionally include a dictionary to supplement the object data. A list of keys
@ -159,8 +159,10 @@ def serialize_object(obj, resolve_tags=True, extra=None):
for field in ['level', 'lft', 'rght', 'tree_id']: for field in ['level', 'lft', 'rght', 'tree_id']:
data.pop(field) data.pop(field)
if get_config().CHANGELOG_SKIP_EMPTY_CHANGES and 'last_updated' in data: if exclude_fields:
data.pop('last_updated') for field in exclude_fields:
if field in data:
data.pop(field)
# Include custom_field_data as "custom_fields" # Include custom_field_data as "custom_fields"
if hasattr(obj, 'custom_field_data'): if hasattr(obj, 'custom_field_data'):