Fixes #14329: Improve diffs for custom fields
This commit is contained in:
parent
85db007ff5
commit
f149f1f994
|
@ -22,7 +22,7 @@ from utilities.htmx import is_htmx
|
|||
from utilities.paginator import EnhancedPaginator, get_paginate_count
|
||||
from utilities.rqworker import get_workers_for_queue
|
||||
from utilities.templatetags.builtins.filters import render_markdown
|
||||
from utilities.utils import copy_safe_request, count_related, get_viewname, normalize_querydict, shallow_compare_dict
|
||||
from utilities.utils import copy_safe_request, count_related, deep_compare_dict, get_viewname, normalize_querydict
|
||||
from utilities.views import ContentTypePermissionRequiredMixin, register_model_view
|
||||
from . import filtersets, forms, tables
|
||||
from .forms.reports import ReportForm
|
||||
|
@ -719,14 +719,7 @@ class ObjectChangeView(generic.ObjectView):
|
|||
prechange_data = instance.prechange_data
|
||||
|
||||
if prechange_data and instance.postchange_data:
|
||||
diff_added = shallow_compare_dict(
|
||||
prechange_data or dict(),
|
||||
instance.postchange_data or dict(),
|
||||
exclude=['last_updated'],
|
||||
)
|
||||
diff_removed = {
|
||||
x: prechange_data.get(x) for x in diff_added
|
||||
} if prechange_data else {}
|
||||
diff_added, diff_removed = deep_compare_dict(prechange_data, instance.postchange_data, exclude=('last_updated'))
|
||||
else:
|
||||
diff_added = None
|
||||
diff_removed = None
|
||||
|
|
|
@ -395,20 +395,34 @@ def prepare_cloned_fields(instance):
|
|||
return QueryDict(urlencode(params), mutable=True)
|
||||
|
||||
|
||||
def shallow_compare_dict(source_dict, destination_dict, exclude=tuple()):
|
||||
def deep_compare_dict(old, new, exclude=tuple()):
|
||||
"""
|
||||
Return a new dictionary of the different keys. The values of `destination_dict` are returned. Only the equality of
|
||||
the first layer of keys/values is checked. `exclude` is a list or tuple of keys to be ignored.
|
||||
Return a tuple of two dictionaries `(removed_diffs, added_diffs)` in a format
|
||||
that is compatible with the requirements of `ObjectChangeView`.
|
||||
`exclude` is a list or tuple of keys to be ignored.
|
||||
"""
|
||||
difference = {}
|
||||
added_diffs = {}
|
||||
removed_diffs = {}
|
||||
|
||||
for key, value in destination_dict.items():
|
||||
for key in old:
|
||||
if key in exclude:
|
||||
continue
|
||||
if source_dict.get(key) != value:
|
||||
difference[key] = value
|
||||
|
||||
return difference
|
||||
old_data = old[key]
|
||||
new_data = new[key]
|
||||
|
||||
if old_data != new_data:
|
||||
if isinstance(old_data, dict) and isinstance(new_data, dict):
|
||||
(sub_added, sub_removed) = deep_compare_dict(old_data, new_data, exclude=exclude)
|
||||
if len(sub_removed) > 0:
|
||||
removed_diffs[key] = sub_removed
|
||||
if len(sub_added) > 0:
|
||||
added_diffs[key] = sub_added
|
||||
else:
|
||||
removed_diffs[key] = old_data
|
||||
added_diffs[key] = new_data
|
||||
|
||||
return added_diffs, removed_diffs
|
||||
|
||||
|
||||
def flatten_dict(d, prefix='', separator='.'):
|
||||
|
|
Loading…
Reference in New Issue