From 3b07c65cba74b904cefdb61e2f4bab3f092b9692 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Thu, 19 Dec 2024 21:09:35 +0100 Subject: [PATCH] Refactor label upload logic to include AOI ID directly in feature properties --- backend/core/views.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/backend/core/views.py b/backend/core/views.py index 3ad2e49c..3b224a2a 100644 --- a/backend/core/views.py +++ b/backend/core/views.py @@ -435,12 +435,8 @@ def validate_geojson(self, geojson_data): ) # Validate the first feature with the serializer - label_data = { - "aoi": self.kwargs.get("aoi_id"), - "geom": first_feature["geometry"], - **first_feature["properties"], - } - serializer = LabelSerializer(data=label_data) + first_feature["properties"]["aoi"] = self.kwargs.get("aoi_id") + serializer = LabelSerializer(data=first_feature) if not serializer.is_valid(): raise ValidationError(serializer.errors) @@ -452,21 +448,21 @@ def process_labels_geojson(geojson_data, aoi_id): obj.save() for feature in geojson_data["features"]: geom = feature["geometry"] - properties = feature["properties"] - label_data = {"aoi": aoi_id, "geom": geom, **properties} + # properties = feature["properties"] + feature["properties"]["aoi"] = aoi_id + # label_data = {"aoi": aoi_id, "geom": geom, **properties} existing_label = Label.objects.filter(aoi=aoi_id, geom=geom).first() if existing_label: - serializer = LabelSerializer(existing_label, data=label_data) + serializer = LabelSerializer(existing_label, data=feature) else: - serializer = LabelSerializer(data=label_data) + serializer = LabelSerializer(data=feature) if serializer.is_valid(): serializer.save() obj.label_status = AOI.DownloadStatus.DOWNLOADED obj.label_fetched = datetime.utcnow() obj.save() - return Response("Success", status=status.HTTP_201_CREATED) except Exception as ex: obj.label_status = AOI.DownloadStatus.NOT_DOWNLOADED obj.save()