Skip to content

Commit

Permalink
geojson: Can upload nodes or spans by themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
odscjames committed Nov 15, 2022
1 parent 0fb4de0 commit 795e5f4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
2 changes: 2 additions & 0 deletions cove_ofds/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class NewGeoJSONUploadForm(forms.Form):
)
}
),
required=False,
)
spans_file_upload = forms.FileField(
label="Select GeoJSON Spans file",
Expand All @@ -24,4 +25,5 @@ class NewGeoJSONUploadForm(forms.Form):
)
}
),
required=False,
)
21 changes: 12 additions & 9 deletions cove_ofds/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
41 changes: 24 additions & 17 deletions cove_ofds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,37 @@ 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():
supplied_data = SuppliedData()
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())

Expand Down

0 comments on commit 795e5f4

Please sign in to comment.