Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions netbox_floorplan/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]):
Expand All @@ -108,37 +109,48 @@ 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
if obj.get("objects"):
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
if obj.get("objects"):
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:
Expand Down
Loading