1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #5913: Improve change logging (#5924)

* Initial work on #5913
* Provide per-line diff highlighting
* BulkDeteView should delete objects individually to secure a pre-change snapshot
* Add changelog tests for bulk operations
This commit is contained in:
Jeremy Stretch
2021-03-04 13:06:04 -05:00
committed by GitHub
parent f495addb1e
commit 9c967ee3ea
21 changed files with 439 additions and 124 deletions

View File

@ -7,6 +7,7 @@ from django.core.serializers import serialize
from django.db.models import Count, OuterRef, Subquery
from django.db.models.functions import Coalesce
from jinja2 import Environment
from mptt.models import MPTTModel
from dcim.choices import CableLengthUnitChoices
from extras.utils import is_taggable
@ -83,7 +84,7 @@ def count_related(model, field):
return Coalesce(subquery, 0)
def serialize_object(obj, extra=None, exclude=None):
def serialize_object(obj, extra=None):
"""
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
@ -93,6 +94,11 @@ def serialize_object(obj, extra=None, exclude=None):
json_str = serialize('json', [obj])
data = json.loads(json_str)[0]['fields']
# Exclude any MPTTModel fields
if issubclass(obj.__class__, MPTTModel):
for field in ['level', 'lft', 'rght', 'tree_id']:
data.pop(field)
# Include custom_field_data as "custom_fields"
if hasattr(obj, 'custom_field_data'):
data['custom_fields'] = data.pop('custom_field_data')
@ -112,10 +118,6 @@ def serialize_object(obj, extra=None, exclude=None):
if isinstance(key, str) and key.startswith('_'):
data.pop(key)
# Explicitly excluded keys
if isinstance(exclude, (list, tuple)) and key in exclude:
data.pop(key)
return data