From 795e5f4dc046856bf8d7e3a7b37d57985b0e99b0 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 15 Nov 2022 15:05:23 +0000 Subject: [PATCH] geojson: Can upload nodes or spans by themselves https://github.com/Open-Telecoms-Data/cove-ofds/issues/5#issuecomment-1311008618 --- cove_ofds/forms.py | 2 ++ cove_ofds/process.py | 21 ++++++++++++--------- cove_ofds/views.py | 41 ++++++++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/cove_ofds/forms.py b/cove_ofds/forms.py index 049f1e4..00f1968 100644 --- a/cove_ofds/forms.py +++ b/cove_ofds/forms.py @@ -13,6 +13,7 @@ class NewGeoJSONUploadForm(forms.Form): ) } ), + required=False, ) spans_file_upload = forms.FileField( label="Select GeoJSON Spans file", @@ -24,4 +25,5 @@ class NewGeoJSONUploadForm(forms.Form): ) } ), + required=False, ) diff --git a/cove_ofds/process.py b/cove_ofds/process.py index 02992cd..b897575 100644 --- a/cove_ofds/process.py +++ b/cove_ofds/process.py @@ -184,18 +184,21 @@ def process(self, process_data: dict) -> dict: f for f in supplied_data_json_files if f.meta.get("geojson") == "spans" ] - if len(nodes_data_json_files) != 1 or len(spans_data_json_files) != 1: + if len(nodes_data_json_files) != 1 and len(spans_data_json_files) != 1: raise Exception("Can't find JSON original data!") # Get data from files - nodes_data_json_file = nodes_data_json_files[0] - spans_data_json_file = spans_data_json_files[0] - - with open(nodes_data_json_file.upload_dir_and_filename()) as fp: - nodes_data = json.load(fp) - - with open(spans_data_json_file.upload_dir_and_filename()) as fp: - spans_data = json.load(fp) + # (Or insert dummy data, if no file was uploaded) + if nodes_data_json_files: + with open(nodes_data_json_files[0].upload_dir_and_filename()) as fp: + nodes_data = json.load(fp) + else: + nodes_data = {"type": "FeatureCollection", "features": []} + if spans_data_json_files: + with open(spans_data_json_files[0].upload_dir_and_filename()) as fp: + spans_data = json.load(fp) + else: + spans_data = {"type": "FeatureCollection", "features": []} # Convert converter = GeoJSONToJSONConverter() diff --git a/cove_ofds/views.py b/cove_ofds/views.py index 3a74caf..619d693 100644 --- a/cove_ofds/views.py +++ b/cove_ofds/views.py @@ -67,17 +67,22 @@ def new_geojson(request): if form.is_valid(): # Extra Validation for field in ["nodes_file_upload", "spans_file_upload"]: - if ( - not request.FILES[field].content_type - in settings.ALLOWED_GEOJSON_CONTENT_TYPES - ): - form.add_error(field, "This does not appear to be a JSON file") - if not [ - e - for e in settings.ALLOWED_GEOJSON_EXTENSIONS - if str(request.FILES[field].name).lower().endswith(e) - ]: - form.add_error(field, "This does not appear to be a JSON file") + if field in request.FILES: + if ( + not request.FILES[field].content_type + in settings.ALLOWED_GEOJSON_CONTENT_TYPES + ): + form.add_error(field, "This does not appear to be a JSON file") + if not [ + e + for e in settings.ALLOWED_GEOJSON_EXTENSIONS + if str(request.FILES[field].name).lower().endswith(e) + ]: + form.add_error(field, "This does not appear to be a JSON file") + if not ("nodes_file_upload" in request.FILES) and not ( + "spans_file_upload" in request.FILES + ): + form.add_error("nodes_file_upload", "You must upload nodes or spans") # Process if form.is_valid(): @@ -85,12 +90,14 @@ def new_geojson(request): supplied_data.format = "geojson" supplied_data.save() - supplied_data.save_file( - request.FILES["nodes_file_upload"], meta={"geojson": "nodes"} - ) - supplied_data.save_file( - request.FILES["spans_file_upload"], meta={"geojson": "spans"} - ) + if "nodes_file_upload" in request.FILES: + supplied_data.save_file( + request.FILES["nodes_file_upload"], meta={"geojson": "nodes"} + ) + if "spans_file_upload" in request.FILES: + supplied_data.save_file( + request.FILES["spans_file_upload"], meta={"geojson": "spans"} + ) return HttpResponseRedirect(supplied_data.get_absolute_url())