From 28995c9c7b73cf760ff6f011041899249a0e95f8 Mon Sep 17 00:00:00 2001 From: JCWasmx86 Date: Sat, 1 Jun 2024 10:01:05 +0200 Subject: [PATCH] More --- netbox/templates/extras/objectchange.html | 12 +--- netbox/utilities/data.py | 69 +++++++++++------------ 2 files changed, 35 insertions(+), 46 deletions(-) diff --git a/netbox/templates/extras/objectchange.html b/netbox/templates/extras/objectchange.html index 560ef665e69..c8af017f453 100644 --- a/netbox/templates/extras/objectchange.html +++ b/netbox/templates/extras/objectchange.html @@ -110,12 +110,8 @@
{% trans "Pre-Change Data" %}
{% if object.prechange_data %} - {% spaceless %} -
{% for text, changed in text_before %}{% spaceless %}
-                      {{ text }}
-                      {% endspaceless %}{% endfor %}
+                  
{% for text, changed in text_before %}{{ text }}
{% endfor %}
- {% endspaceless %} {% elif non_atomic_change %} {% trans "Warning: Comparing non-atomic change to previous change record" %} ({{ prev_change.pk }}) {% else %} @@ -129,12 +125,8 @@
{% trans "Pre-Change Data" %}
{% trans "Post-Change Data" %}
{% if object.postchange_data %} - {% spaceless %} -
{% for text,changed in text_after %}{% spaceless %}
-                      {{ text }}
-                      {% endspaceless %}{% endfor %}
+                    
{% for text,changed in text_after %}{{ text }}
{% endfor %}
- {% endspaceless %} {% else %} {% trans "None" %} {% endif %} diff --git a/netbox/utilities/data.py b/netbox/utilities/data.py index 952d91e3aac..00254c17b48 100644 --- a/netbox/utilities/data.py +++ b/netbox/utilities/data.py @@ -132,18 +132,20 @@ def drange(start, end, step=decimal.Decimal(1)): # # String utilities # + + def regular_line_ending(s: str): - if s.endswith(","): + if s.endswith(','): return True - return not (s.endswith("{") or s.endswith("[")) + return not (s.endswith('{') or s.endswith('[')) def begin_of_complex(s: str): - return s.endswith("[") or s.endswith("{") + return s.endswith('[') or s.endswith('{') def has_indent(s: str, indent: str): - return not s.removeprefix(indent).startswith(" ") + return not s.removeprefix(indent).startswith(' ') def extract_key(s: str): @@ -153,6 +155,7 @@ def extract_key(s: str): substr = s[:idx].replace('"', '').strip() return substr + def make_diff(old: str, new: str): old_lines = old.splitlines(False) new_lines = new.splitlines(False) @@ -166,13 +169,13 @@ def make_diff(old: str, new: str): if old_idx == len(old_lines): while new_idx != len(new_lines): new_list.append((new_lines[new_idx], True)) - old_list.append(("", False)) + old_list.append(('', False)) new_idx += 1 break if new_idx == len(new_lines): while old_idx != len(old_lines): old_list.append((old_lines[old_idx], True)) - new_list.append(("", False)) + new_list.append(('', False)) old_idx += 1 break old_s = old_lines[old_idx] @@ -183,20 +186,17 @@ def make_diff(old: str, new: str): new_idx += 1 # Handle additions of keys if old_k is not None and new_k is not None and old_k != new_k: - print("Found mismatch:", old_k, new_k) found_it = False for old_idx2 in range(old_idx, len(old_lines)): old_s2 = old_lines[old_idx2] old_k2 = extract_key(old_s2) - print(">>", old_k2, new_k) if old_k2 == new_k: - print("Found it", old_k2, old_idx2, new_k) old_list.append((old_s, False)) - new_list.append((" ", False)) - old_s = "" if old_idx >= len(old_lines) else old_lines[old_idx] + new_list.append(('', False)) + old_s = '' if old_idx >= len(old_lines) else old_lines[old_idx] for _ in range(old_idx + 1, old_idx2 + 1): - old_s = "" if old_idx >= len(old_lines) else old_lines[old_idx] - new_list.append((" <", False)) + old_s = '' if old_idx >= len(old_lines) else old_lines[old_idx] + new_list.append(('', False)) old_list.append((old_s, True)) old_idx += 1 old_list.append((old_s, False)) @@ -209,15 +209,13 @@ def make_diff(old: str, new: str): for new_idx2 in range(new_idx, len(new_lines)): new_s2 = new_lines[new_idx2] new_k2 = extract_key(new_s2) - print(">>22", old_k, new_k2) if new_k2 == old_k: - print("NEW Found it", new_k2, new_idx2, old_k) - old_list.append((" ", False)) + old_list.append(('', False)) new_list.append((new_s, False)) - new_s = "" if new_idx >= len(new_lines) else new_lines[new_idx] + new_s = '' if new_idx >= len(new_lines) else new_lines[new_idx] for _ in range(new_idx + 1, new_idx2 + 1): - new_s = "" if new_idx >= len(new_lines) else new_lines[new_idx] - old_list.append((" <", False)) + new_s = '' if new_idx >= len(new_lines) else new_lines[new_idx] + old_list.append(('', False)) new_idx += 1 new_list.append((new_s, True)) old_list.append((old_s, False)) @@ -226,13 +224,13 @@ def make_diff(old: str, new: str): found_it = True break continue - if "{" == old_s.strip() and "{" == new_s.strip(): - old_list.append(("{", False)) - new_list.append(("{", False)) + if '{' == old_s.strip() and '{' == new_s.strip(): + old_list.append(('{', False)) + new_list.append(('{', False)) continue - if "}" == old_s.strip() and "}" == new_s.strip(): - old_list.append(("}", False)) - new_list.append(("}", False)) + if '}' == old_s.strip() and '}' == new_s.strip(): + old_list.append(('}', False)) + new_list.append(('}', False)) continue # Handle: # "foo": null "foo": [ @@ -240,14 +238,14 @@ def make_diff(old: str, new: str): # "baz", # "]" if regular_line_ending(old_s) and begin_of_complex(new_s): - indent_of_new = new_s.replace(new_s.strip(), "") + indent_of_new = new_s.replace(new_s.strip(), '') new_list.append((new_s, True)) old_list.append((old_s, True)) while not has_indent(new_lines[new_idx], indent_of_new): - old_list.append(("", True)) + old_list.append(('', True)) new_list.append((new_lines[new_idx], True)) new_idx += 1 - old_list.append(("", True)) + old_list.append(('', True)) new_list.append((new_lines[new_idx], True)) new_idx += 1 continue @@ -256,20 +254,20 @@ def make_diff(old: str, new: str): # "foo": "bar" # } if begin_of_complex(old_s) and regular_line_ending(new_s): - indent_of_old = old_s.replace(old_s.strip(), "") + indent_of_old = old_s.replace(old_s.strip(), '') old_list.append((old_s, True)) new_list.append((new_s, True)) while not has_indent(old_lines[old_idx], indent_of_old): - new_list.append(("", True)) + new_list.append(('', True)) old_list.append((old_lines[old_idx], True)) old_idx += 1 - new_list.append(("", True)) + new_list.append(('', True)) old_list.append((old_lines[old_idx], True)) old_idx += 1 continue if begin_of_complex(old_s) and begin_of_complex(new_s): - indent_of_old = old_s.replace(old_s.strip(), "") - indent_of_new = new_s.replace(new_s.strip(), "") + indent_of_old = old_s.replace(old_s.strip(), '') + indent_of_new = new_s.replace(new_s.strip(), '') old_list.append((old_s, False)) new_list.append((new_s, False)) old_tmp = [] @@ -280,7 +278,7 @@ def make_diff(old: str, new: str): while not has_indent(new_lines[new_idx], indent_of_new): new_tmp.append(new_lines[new_idx]) new_idx += 1 - a, b = make_diff("\n".join(old_tmp), "\n".join(new_tmp)) + a, b = make_diff('\n'.join(old_tmp), '\n'.join(new_tmp)) assert len(a) == len(b) old_list += a new_list += b @@ -299,6 +297,5 @@ def make_diff(old: str, new: str): old_list.append((old_s, True)) new_list.append((new_s, True)) continue - print(len(old_list), len(new_list)) assert len(old_list) == len(new_list) - return old_list, new_list \ No newline at end of file + return old_list, new_list