Skip to content

Commit

Permalink
Merge pull request #42 from Open-Telecoms-Data/2022-11-15
Browse files Browse the repository at this point in the history
2022-11-15
  • Loading branch information
odscjames authored Nov 16, 2022
2 parents 184ff6d + 795e5f4 commit f16c097
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 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,
)
5 changes: 1 addition & 4 deletions cove_ofds/jsonschema_validation_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
# It could do with some testing, wherever it ends up
def add_type_to_json_schema_validation_error(data: dict) -> dict:

if data["validator"] == "prefixItems":
data["cove_type"] = "PrefixItems"

elif data["validator"] == "const":
if data["validator"] == "const":
data["cove_type"] = "Valuedoesnotmatchconstant"

elif data["validator"] == "minItems":
Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{% load i18n %}

{% if 'prefixItems' in validation_errors %}
<h4>{% trans 'prefixItems' %}</h4>
<p>DESCIRPTION</p>
{% include "cove_ofds/jsonschema_validation_table.html" with validation_errors_for_table=validation_errors.prefixItems %}
{% endif %}


{% if 'Valuedoesnotmatchconstant' in validation_errors %}
<h4>{% trans 'Value does not match constant' %}</h4>
<p>You must update each value to match the constant specified in the schema.</p>
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 f16c097

Please sign in to comment.