From fdcaeee8ee0058ecef38e23b4d550c0aaf20d51b Mon Sep 17 00:00:00 2001 From: dreng Date: Sat, 6 May 2023 22:35:11 +0200 Subject: [PATCH] moved default group check to model function --- netbox_topology_views/api/views.py | 19 ++----------------- netbox_topology_views/models.py | 21 +++++++++++++++++++++ netbox_topology_views/views.py | 23 +++++------------------ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/netbox_topology_views/api/views.py b/netbox_topology_views/api/views.py index bb6d965..6ba2585 100644 --- a/netbox_topology_views/api/views.py +++ b/netbox_topology_views/api/views.py @@ -65,23 +65,8 @@ class SaveCoordsViewSet(ReadOnlyModelViewSet): {"status": "coords custom field could not be saved"}, status=500 ) - # Default group named "default" must exist in order to make sure that - # coordinate values can be stored even if no coordinate group has been - # selected. The default group will be added automatically if it does not exist. - try: - if CoordinateGroup.objects.filter(name="default"): - group = CoordinateGroup.objects.get(name="default") - group_id = group.pk - else: - group = CoordinateGroup( - name="default", - description="Automatically generated default group. If you delete " - "this group, all default coordinates are gone for good but " - "the group itself will be re-created." - ) - group.save() - group_id = group.pk - except: + group_id = Coordinate.get_or_create_default_group(group_id) + if not group_id: return Response( {"status": "Error while creating default group."}, status=500 ) diff --git a/netbox_topology_views/models.py b/netbox_topology_views/models.py index 0f13a50..5f9132b 100644 --- a/netbox_topology_views/models.py +++ b/netbox_topology_views/models.py @@ -137,6 +137,27 @@ class Coordinate(NetBoxModel): 'Smaller values correspond to a position further to the left on the monitor.', ) + def get_or_create_default_group(group_id): + # Default group named "default" must always exist in order to make sure + # that coordinate values can be stored even if no coordinate group has been + # selected. The default group will be added automatically if it does not exist. + try: + if CoordinateGroup.objects.filter(name="default"): + group = CoordinateGroup.objects.get(name="default") + group_id = group.pk + else: + group = CoordinateGroup( + name="default", + description="Automatically generated default group. If you delete " + "this group, all default coordinates are gone for good but " + "the group itself will be re-created." + ) + group.save() + group_id = group.pk + except: + return False + return group_id + class Meta: ordering = ['group', 'device'] unique_together = ('device', 'group') diff --git a/netbox_topology_views/views.py b/netbox_topology_views/views.py index 5a1e76e..ffe5383 100644 --- a/netbox_topology_views/views.py +++ b/netbox_topology_views/views.py @@ -157,24 +157,11 @@ def create_node( node["image"] = get_image_for_entity(device) if group_id is None or group_id == "default": - # Default group named "default" must always exist in order to make sure - # that coordinate values can be stored even if no coordinate group has been - # selected. The default group will be added automatically if it does not exist. - try: - if CoordinateGroup.objects.filter(name="default"): - group = CoordinateGroup.objects.get(name="default") - group_id = group.pk - else: - group = CoordinateGroup( - name="default", - description="Automatically generated default group. If you delete " - "this group, all default coordinates are gone for good but " - "the group itself will be re-created." - ) - group.save() - group_id = group.pk - except: - pass + group_id = Coordinate.get_or_create_default_group(group_id) + if not group_id: + node["x"] = 0 + node["y"] = 0 + return node group = get_object_or_404(CoordinateGroup, pk=group_id)