Skip to content

Commit

Permalink
Hacky first patch for better diffs of custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JCWasmx86 committed Nov 22, 2023
1 parent d52a6d3 commit 3649b05
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,29 @@ class ObjectChangeListView(generic.ObjectListView):
class ObjectChangeView(generic.ObjectView):
queryset = ObjectChange.objects.valid_models()

def floating_point_hack(self, old, new):
if not isinstance(old, float) or not isinstance(new, float):
return True
return abs(old - new) <= 0.0001

def calculate_diff(self, old, new):
added_diffs = {}
removed_diffs = {}
for k in sorted(old.keys()):
old_data = old[k]
new_data = new[k]
if str(old_data) != str(new_data) and self.floating_point_hack(old_data, new_data):
if isinstance(old_data, dict) and isinstance(new_data, dict):
(a, r) = self.calculate_diff(old_data, new_data)
if len(r) > 0:
removed_diffs[k] = r
if len(a) > 0:
added_diffs[k] = a
else:
removed_diffs[k] = old_data
added_diffs[k] = new_data
return added_diffs, removed_diffs

def get_extra_context(self, request, instance):
related_changes = ObjectChange.objects.valid_models().restrict(request.user, 'view').filter(
request_id=instance.request_id
Expand Down Expand Up @@ -661,14 +684,11 @@ def get_extra_context(self, request, instance):
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 = self.calculate_diff(prechange_data, instance.postchange_data)
if "last_updated" in diff_added:
del diff_added["last_updated"]
if "last_updated" in diff_removed:
del diff_removed["last_updated"]
else:
diff_added = None
diff_removed = None
Expand Down

0 comments on commit 3649b05

Please sign in to comment.