moved default group check to model function

This commit is contained in:
dreng
2023-05-06 23:01:08 +02:00
parent dbf2d23905
commit fdcaeee8ee
3 changed files with 28 additions and 35 deletions
+2 -17
View File
@@ -65,23 +65,8 @@ class SaveCoordsViewSet(ReadOnlyModelViewSet):
{"status": "coords custom field could not be saved"}, status=500 {"status": "coords custom field could not be saved"}, status=500
) )
# Default group named "default" must exist in order to make sure that group_id = Coordinate.get_or_create_default_group(group_id)
# coordinate values can be stored even if no coordinate group has been if not group_id:
# 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 Response( return Response(
{"status": "Error while creating default group."}, status=500 {"status": "Error while creating default group."}, status=500
) )
+21
View File
@@ -137,6 +137,27 @@ class Coordinate(NetBoxModel):
'Smaller values correspond to a position further to the left on the monitor.', '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: class Meta:
ordering = ['group', 'device'] ordering = ['group', 'device']
unique_together = ('device', 'group') unique_together = ('device', 'group')
+5 -18
View File
@@ -157,24 +157,11 @@ def create_node(
node["image"] = get_image_for_entity(device) node["image"] = get_image_for_entity(device)
if group_id is None or group_id == "default": if group_id is None or group_id == "default":
# Default group named "default" must always exist in order to make sure group_id = Coordinate.get_or_create_default_group(group_id)
# that coordinate values can be stored even if no coordinate group has been if not group_id:
# selected. The default group will be added automatically if it does not exist. node["x"] = 0
try: node["y"] = 0
if CoordinateGroup.objects.filter(name="default"): return node
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 = get_object_or_404(CoordinateGroup, pk=group_id) group = get_object_or_404(CoordinateGroup, pk=group_id)