From 0516f34456841ac8528802f9b539540eabb2d599 Mon Sep 17 00:00:00 2001 From: Tobias Girstmair Date: Mon, 18 Nov 2024 09:38:55 +0100 Subject: [PATCH] Bug 42: don't create empty changelog when viewing floorplan --- netbox_floorplan/models.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/netbox_floorplan/models.py b/netbox_floorplan/models.py index 3f8ac7a..1048c10 100644 --- a/netbox_floorplan/models.py +++ b/netbox_floorplan/models.py @@ -98,6 +98,7 @@ def mapped_devices(self): return drawn_devices def resync_canvas(self): + changed = False if self.canvas: if self.canvas.get("objects"): for index, obj in enumerate(self.canvas["objects"]): @@ -108,6 +109,7 @@ def resync_canvas(self): rack_qs = Rack.objects.filter(pk=rack_id) if not rack_qs.exists(): self.canvas["objects"].remove(obj) + changed = True else: rack = rack_qs.first() self.canvas["objects"][index]["custom_meta"]["object_name"] = rack.name @@ -115,17 +117,22 @@ def resync_canvas(self): for subcounter, subobj in enumerate(obj["objects"]): if subobj.get("type") == "i-text": if subobj.get("custom_meta", {}).get("text_type") == "name": - self.canvas["objects"][index]["objects"][ - subcounter]["text"] = f"{rack.name}" + if subobj["text"] != f"{rack.name}": + self.canvas["objects"][index]["objects"][ + subcounter]["text"] = f"{rack.name}" + changed = True if subobj.get("custom_meta", {}).get("text_type") == "status": - self.canvas["objects"][index]["objects"][ - subcounter]["text"] = f"{rack.status}" + if subobj["text"] != f"{rack.status}": + self.canvas["objects"][index]["objects"][ + subcounter]["text"] = f"{rack.status}" + changed = True if obj["custom_meta"].get("object_type") == "device": device_id = int(obj["custom_meta"]["object_id"]) - # if rack is not in the database, remove it from the canvas + # if device is not in the database, remove it from the canvas device_qs = Device.objects.filter(pk=device_id) if not device_qs.exists(): self.canvas["objects"].remove(obj) + changed = True else: device = device_qs.first() self.canvas["objects"][index]["custom_meta"]["object_name"] = device.name @@ -133,12 +140,17 @@ def resync_canvas(self): for subcounter, subobj in enumerate(obj["objects"]): if subobj.get("type") == "i-text": if subobj.get("custom_meta", {}).get("text_type") == "name": - self.canvas["objects"][index]["objects"][ - subcounter]["text"] = f"{device.name}" + if subobj["text"] != f"{device.name}": + self.canvas["objects"][index]["objects"][ + subcounter]["text"] = f"{device.name}" + changed = True if subobj.get("custom_meta", {}).get("text_type") == "status": - self.canvas["objects"][index]["objects"][ - subcounter]["text"] = f"{device.status}" - self.save() + if subobj["text"] != f"{device.status}": + self.canvas["objects"][index]["objects"][ + subcounter]["text"] = f"{device.status}" + changed = True + if changed: + self.save() def save(self, *args, **kwargs): if self.site and self.location: