From 8c2e8c20c63e548ede1ebe9a6c7487f4c2535a43 Mon Sep 17 00:00:00 2001 From: lastminutediorama Date: Mon, 19 Aug 2024 21:15:34 -0500 Subject: [PATCH] collect projectarea keys --- src/planscape/planning/services.py | 18 ++++++++++++++++-- src/planscape/planning/views_v2.py | 3 +-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/planscape/planning/services.py b/src/planscape/planning/services.py index ea8f3e3b1..6610356ff 100644 --- a/src/planscape/planning/services.py +++ b/src/planscape/planning/services.py @@ -128,6 +128,9 @@ def feature_to_project_area(idx: int, user_id: int, scenario, feature): action_object=proj_area_obj, target=scenario, ) + + return proj_area_obj + except Exception as e: logger.error(f"Unable to create project area for {scenario.name} {e}") raise e @@ -148,7 +151,11 @@ def create_scenario_from_upload( ) if "type" in uploaded_geom and uploaded_geom["type"] == "Polygon": - feature_to_project_area(1, scenario.user, scenario, json.dumps(uploaded_geom)) + new_feature = feature_to_project_area( + 1, scenario.user, scenario, json.dumps(uploaded_geom) + ) + uploaded_geom.setdefault("properties", {}) + uploaded_geom["properties"]["project_id"] = new_feature.pk # # this handles a format provided by shpjs when a shapefile has multiple features # if "geometry" in uploaded_geom and "coordinates" in uploaded_geom["geometry"]: @@ -159,9 +166,16 @@ def create_scenario_from_upload( # this handles a more standard FeatureCollection if "features" in uploaded_geom: for idx, f in enumerate(uploaded_geom["features"], 1): - feature_to_project_area( + print(f"do we have...mutliple feature? {f}") + new_feature = feature_to_project_area( idx, scenario.user, scenario, json.dumps(f["geometry"]) ) + # TODO: add new feature details back to uploaded_geom + f.setdefault("properties", {}) + f["properties"]["project_id"] = new_feature.pk + + # Store updated in ScenarioResult.result + ScenarioResult.objects.create(scenario=scenario, result=uploaded_geom) return scenario diff --git a/src/planscape/planning/views_v2.py b/src/planscape/planning/views_v2.py index d29f59e36..55fc9fbb1 100644 --- a/src/planscape/planning/views_v2.py +++ b/src/planscape/planning/views_v2.py @@ -173,11 +173,9 @@ def toggle_status(self, request, pk=None): @action(methods=["POST"], detail=False) def upload_shapefiles(self, request, pk=None, *args, **kwargs): - # stuff we expect stand_size = request.data["stand_size"] # TODO: cleanup dict vs string here - uploaded_geom = request.data["geometry"] if isinstance(uploaded_geom, str): uploaded_geojson = json.loads(uploaded_geom) @@ -222,6 +220,7 @@ def upload_shapefiles(self, request, pk=None, *args, **kwargs): "planning_area": pa.pk, "user": request.user.pk, "configuration": {"stand_size": stand_size}, + ## TODO: add an origin (e.g., uploaded) } scenario_serializer = UploadedScenarioSerializer(data=scenario_data)