Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hacky first patch for better diffs of custom fields
Browse files Browse the repository at this point in the history
JCWasmx86 committed Nov 22, 2023
1 parent d52a6d3 commit 4a74dee
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 3 additions & 1 deletion netbox/extras/models/reports.py
Original file line number Diff line number Diff line change
@@ -59,9 +59,11 @@ def _get_name(cls):
# For child objects in submodules use the full import path w/o the root module as the name
return cls.full_name.split(".", maxsplit=1)[1]

self.exception_info = None
try:
module = self.get_module()
except (ImportError, SyntaxError) as e:
except Exception as e:
self.exception_info = e
logger.error(f"Unable to load report module {self.name}, exception: {e}")
return {}
reports = {}
34 changes: 26 additions & 8 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
@@ -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
@@ -661,14 +684,9 @@ 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)
del diff_added["last_updated"]
del diff_removed["last_updated"]
else:
diff_added = None
diff_removed = None
2 changes: 1 addition & 1 deletion netbox/templates/extras/report_list.html
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ <h5 class="card-header" id="module{{ module.pk }}">
</table>
{% else %}
<div class="alert alert-warning" role="alert">
<i class="mdi mdi-alert"></i> Could not load reports from {{ module.name }}
<i class="mdi mdi-alert"></i> Could not load reports from {{ module.name }}: {{ module.exception_info }}
</div>
{% endif %}
</div>

0 comments on commit 4a74dee

Please sign in to comment.