rework load/save of coords; add group coords save

This commit is contained in:
dreng
2023-05-06 23:01:08 +02:00
parent 9cc1607811
commit dbf2d23905
7 changed files with 103 additions and 82 deletions
+43 -36
View File
@@ -33,6 +33,7 @@ class SaveCoordsViewSet(ReadOnlyModelViewSet):
device_id: str = request.data.get("node_id", None)
x_coord = request.data.get("x", None)
y_coord = request.data.get("y", None)
group_id = request.data.get("group", "None")
actual_device = None
if device_id.startswith("c"):
@@ -50,53 +51,59 @@ class SaveCoordsViewSet(ReadOnlyModelViewSet):
if not actual_device:
return Response({"status": "invalid node_id in body"}, status=400)
# Storing coordinates in custom field is deprecated now.
# We preserve this for backwards compatibility.
try:
actual_device.custom_field_data["coordinates"] = "%s;%s" % (
x_coord,
y_coord,
)
actual_device.save()
except:
return Response(
{"status": "coords custom field could not be saved"}, status=500
)
# 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:
group = CoordinateGroup.objects.get(name="default")
except CoordinateGroup.DoesNotExist:
if group_id is None or group_id == "default":
# Storing coordinates in custom field is deprecated now.
# We preserve this for backwards compatibility.
try:
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."
actual_device.custom_field_data["coordinates"] = "%s;%s" % (
x_coord,
y_coord,
)
group.save()
actual_device.save()
except:
return Response(
{"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:
return Response(
{"status": "Error while creating default group."}, status=500
)
)
try:
# Hen-and-egg-problem. Thanks, Django! By default, Django updates records that
# already exist and inserts otherwise. This does not work with our
# unique_together key if no pk is given. But: No record, no pk.
if not Coordinate.objects.filter(group=group, device=actual_device):
# Unique group/device pair does not exist. Prepare new data set
coords = Coordinate(group=group, device=actual_device, x=x_coord, y=y_coord)
else:
# Unique group/device pair already exists. Update data
coords = Coordinate(pk=Coordinate.objects.get(group=group, device=actual_device).pk, group=group, device=actual_device, x=x_coord, y=y_coord)
coords.save()
if CoordinateGroup.objects.filter(pk=group_id):
group = CoordinateGroup.objects.get(pk=group_id)
# Hen-and-egg-problem. Thanks, Django! By default, Django updates records that
# already exist and inserts otherwise. This does not work with our
# unique_together key if no pk is given. But: No record, no pk.
if not Coordinate.objects.filter(group=group, device=actual_device):
# Unique group/device pair does not exist. Prepare new data set
coords = Coordinate(group=group, device=actual_device, x=x_coord, y=y_coord)
else:
# Unique group/device pair already exists. Update data
coords = Coordinate(pk=Coordinate.objects.get(group=group, device=actual_device).pk, group=group, device=actual_device, x=x_coord, y=y_coord)
coords.save()
except:
return Response(
{"status": "Coordinates could not be saved."}, status=500
)
return Response({"status": "saved coords"})
class ExportTopoToXML(PermissionRequiredMixin, ViewSet):