Skip to content

Commit 7796aeb

Browse files
authored
Merge pull request #166 from Geode-solutions/feat/folder_path
Feat/folder path
2 parents 6b451e1 + e74b50f commit 7796aeb

14 files changed

+167
-98
lines changed

src/opengeodeweb_back/geode_functions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Third party imports
55
import opengeode_geosciences as og_gs
66
import opengeode as og
7+
import werkzeug
8+
import flask
79

810
# Local application imports
911
from .geode_objects import geode_objects_dict
@@ -38,6 +40,26 @@ def load(geode_object: str, file_absolute_path: str):
3840
return geode_object_value(geode_object)["load"](file_absolute_path)
3941

4042

43+
def data_file_path(data_id: str, filename: str) -> str:
44+
data_folder_path = flask.current_app.config["DATA_FOLDER_PATH"]
45+
return os.path.join(
46+
data_folder_path,
47+
data_id,
48+
werkzeug.utils.secure_filename(filename),
49+
)
50+
51+
52+
def load_data(geode_object: str, data_id: str, filename: str):
53+
file_absolute_path = data_file_path(data_id, filename)
54+
return load(geode_object, file_absolute_path)
55+
56+
57+
def upload_file_path(filename):
58+
upload_folder = flask.current_app.config["UPLOAD_FOLDER"]
59+
secure_filename = werkzeug.utils.secure_filename(filename)
60+
return os.path.abspath(os.path.join(upload_folder, secure_filename))
61+
62+
4163
def is_saveable(geode_object: str, data, filename: str):
4264
return geode_object_value(geode_object)["is_saveable"](data, filename)
4365

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ def allowed_objects():
9797
if flask.request.method == "OPTIONS":
9898
return flask.make_response({}, 200)
9999

100-
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
101100
utils_functions.validate_request(flask.request, allowed_objects_json)
102-
file_absolute_path = os.path.join(UPLOAD_FOLDER, flask.request.json["filename"])
101+
file_absolute_path = geode_functions.upload_file_path(
102+
flask.request.json["filename"]
103+
)
103104
allowed_objects = geode_functions.list_geode_objects(
104105
file_absolute_path, flask.request.json["supported_feature"]
105106
)
@@ -118,12 +119,11 @@ def allowed_objects():
118119
methods=missing_files_json["methods"],
119120
)
120121
def missing_files():
121-
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
122122
utils_functions.validate_request(flask.request, missing_files_json)
123-
123+
file_path = geode_functions.upload_file_path(flask.request.json["filename"])
124124
missing_files = geode_functions.missing_files(
125125
flask.request.json["input_geode_object"],
126-
os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]),
126+
file_path,
127127
)
128128
has_missing_files = missing_files.has_missing_files()
129129

@@ -184,11 +184,9 @@ def crs_converter_geographic_coordinate_systems():
184184
methods=inspect_file_json["methods"],
185185
)
186186
def inspect_file():
187-
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
188187
utils_functions.validate_request(flask.request, inspect_file_json)
189188

190-
secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
191-
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
189+
file_path = geode_functions.upload_file_path(flask.request.json["filename"])
192190
data = geode_functions.load(flask.request.json["input_geode_object"], file_path)
193191
class_inspector = geode_functions.inspect(
194192
flask.request.json["input_geode_object"], data
@@ -209,13 +207,13 @@ def inspect_file():
209207
methods=geode_objects_and_output_extensions_json["methods"],
210208
)
211209
def geode_objects_and_output_extensions():
212-
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
213210
utils_functions.validate_request(
214211
flask.request, geode_objects_and_output_extensions_json
215212
)
213+
file_path = geode_functions.upload_file_path(flask.request.json["filename"])
216214
data = geode_functions.load(
217215
flask.request.json["input_geode_object"],
218-
os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]),
216+
file_path,
219217
)
220218
geode_objects_and_output_extensions = (
221219
geode_functions.geode_objects_output_extensions(
@@ -241,14 +239,12 @@ def geode_objects_and_output_extensions():
241239
)
242240
def save_viewable_file():
243241
utils_functions.validate_request(flask.request, save_viewable_file_json)
244-
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
245-
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
246-
secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
247-
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
242+
243+
file_path = geode_functions.upload_file_path(flask.request.json["filename"])
248244
data = geode_functions.load(flask.request.json["input_geode_object"], file_path)
249245
return flask.make_response(
250246
utils_functions.generate_native_viewable_and_light_viewable(
251-
flask.request.json["input_geode_object"], data, DATA_FOLDER_PATH
247+
flask.request.json["input_geode_object"], data
252248
),
253249
200,
254250
)
@@ -261,7 +257,6 @@ def save_viewable_file():
261257
@routes.route(create_point_json["route"], methods=create_point_json["methods"])
262258
def create_point():
263259
utils_functions.validate_request(flask.request, create_point_json)
264-
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
265260
title = flask.request.json["title"]
266261
x = flask.request.json["x"]
267262
y = flask.request.json["y"]
@@ -273,7 +268,7 @@ def create_point():
273268
builder.set_name(title)
274269
return flask.make_response(
275270
utils_functions.generate_native_viewable_and_light_viewable(
276-
"PointSet3D", PointSet3D, DATA_FOLDER_PATH
271+
"PointSet3D", PointSet3D
277272
),
278273
200,
279274
)
@@ -288,12 +283,13 @@ def create_point():
288283
methods=texture_coordinates_json["methods"],
289284
)
290285
def texture_coordinates():
291-
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
292286
utils_functions.validate_request(flask.request, texture_coordinates_json)
293-
data = geode_functions.load(
287+
data = geode_functions.load_data(
294288
flask.request.json["input_geode_object"],
295-
os.path.join(DATA_FOLDER_PATH, flask.request.json["filename"]),
289+
flask.request.json["id"],
290+
flask.request.json["filename"],
296291
)
292+
297293
texture_coordinates = data.texture_manager().texture_names()
298294

299295
return flask.make_response({"texture_coordinates": texture_coordinates}, 200)
@@ -311,14 +307,13 @@ def texture_coordinates():
311307
methods=vertex_attribute_names_json["methods"],
312308
)
313309
def vertex_attribute_names():
314-
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
315310
utils_functions.validate_request(flask.request, vertex_attribute_names_json)
316-
file_absolute_path = os.path.join(
317-
DATA_FOLDER_PATH, werkzeug.utils.secure_filename(flask.request.json["filename"])
318-
)
319-
data = geode_functions.load(
320-
flask.request.json["input_geode_object"], file_absolute_path
311+
data = geode_functions.load_data(
312+
flask.request.json["input_geode_object"],
313+
flask.request.json["id"],
314+
flask.request.json["filename"],
321315
)
316+
322317
vertex_attribute_names = data.vertex_attribute_manager().attribute_names()
323318

324319
return flask.make_response(
@@ -341,14 +336,13 @@ def vertex_attribute_names():
341336
methods=polygon_attribute_names_json["methods"],
342337
)
343338
def polygon_attribute_names():
344-
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
345339
utils_functions.validate_request(flask.request, polygon_attribute_names_json)
346-
file_absolute_path = os.path.join(
347-
DATA_FOLDER_PATH, werkzeug.utils.secure_filename(flask.request.json["filename"])
348-
)
349-
data = geode_functions.load(
350-
flask.request.json["input_geode_object"], file_absolute_path
340+
data = geode_functions.load_data(
341+
flask.request.json["input_geode_object"],
342+
flask.request.json["id"],
343+
flask.request.json["filename"],
351344
)
345+
352346
polygon_attribute_names = data.polygon_attribute_manager().attribute_names()
353347

354348
return flask.make_response(
@@ -371,14 +365,13 @@ def polygon_attribute_names():
371365
methods=polyhedron_attribute_names_json["methods"],
372366
)
373367
def polyhedron_attribute_names():
374-
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
375-
utils_functions.validate_request(flask.request, vertex_attribute_names_json)
376-
file_absolute_path = os.path.join(
377-
DATA_FOLDER_PATH, werkzeug.utils.secure_filename(flask.request.json["filename"])
378-
)
379-
data = geode_functions.load(
380-
flask.request.json["input_geode_object"], file_absolute_path
368+
utils_functions.validate_request(flask.request, polyhedron_attribute_names_json)
369+
data = geode_functions.load_data(
370+
flask.request.json["input_geode_object"],
371+
flask.request.json["id"],
372+
flask.request.json["filename"],
381373
)
374+
382375
polyhedron_attribute_names = data.polyhedron_attribute_manager().attribute_names()
383376

384377
return flask.make_response(

src/opengeodeweb_back/routes/models/blueprint_models.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,27 @@
1818
)
1919
def uuid_to_flat_index():
2020
utils_functions.validate_request(flask.request, vtm_component_indices_json)
21-
vtm_file_path = os.path.join(
22-
flask.current_app.config["DATA_FOLDER_PATH"], flask.request.json["id"] + ".vtm"
21+
22+
vtm_file_path = geode_functions.data_file_path(
23+
flask.request.json["id"], "viewable.vtm"
2324
)
2425
tree = ET.parse(vtm_file_path)
2526
root = tree.find("vtkMultiBlockDataSet")
2627
uuid_to_flat_index = {}
2728
current_index = 0
28-
2929
for elem in root.iter():
3030
if "uuid" in elem.attrib and elem.tag == "DataSet":
3131
uuid_to_flat_index[elem.attrib["uuid"]] = current_index
32-
3332
current_index += 1
34-
35-
return flask.make_response(
36-
{"uuid_to_flat_index": uuid_to_flat_index},
37-
200,
38-
)
33+
return flask.make_response({"uuid_to_flat_index": uuid_to_flat_index}, 200)
3934

4035

41-
def extract_model_uuids(geode_object, file_path):
42-
model = geode_functions.load(geode_object, file_path)
36+
def extract_model_uuids(model):
4337
mesh_components = model.mesh_components()
44-
4538
uuid_dict = {}
46-
4739
for mesh_component, ids in mesh_components.items():
4840
component_name = mesh_component.get()
4941
uuid_dict[component_name] = [id.string() for id in ids]
50-
5142
return uuid_dict
5243

5344

@@ -58,10 +49,12 @@ def extract_model_uuids(geode_object, file_path):
5849
@routes.route(mesh_components_json["route"], methods=mesh_components_json["methods"])
5950
def extract_uuids_endpoint():
6051
utils_functions.validate_request(flask.request, mesh_components_json)
61-
file_path = os.path.join(
62-
flask.current_app.config["DATA_FOLDER_PATH"], flask.request.json["filename"]
52+
53+
model = geode_functions.load_data(
54+
flask.request.json["geode_object"],
55+
flask.request.json["id"],
56+
flask.request.json["filename"],
6357
)
64-
if not os.path.exists(file_path):
65-
return flask.make_response({"error": "File not found"}, 404)
66-
uuid_dict = extract_model_uuids(flask.request.json["geode_object"], file_path)
58+
59+
uuid_dict = extract_model_uuids(model)
6760
return flask.make_response({"uuid_dict": uuid_dict}, 200)

src/opengeodeweb_back/routes/models/schemas/mesh_components.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
],
66
"type": "object",
77
"properties": {
8+
"id": {
9+
"type": "string",
10+
"minLength": 1
11+
},
812
"filename": {
9-
"type": "string"
13+
"type": "string",
14+
"minLength": 1
1015
},
1116
"geode_object": {
12-
"type": "string"
17+
"type": "string",
18+
"minLength": 1
1319
}
1420
},
1521
"required": [
22+
"id",
1623
"filename",
1724
"geode_object"
1825
],

src/opengeodeweb_back/routes/models/schemas/vtm_component_indices.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"type": "object",
77
"properties": {
88
"id": {
9-
"type": "string"
9+
"type": "string",
10+
"minLength": 1
1011
}
1112
},
1213
"required": [

src/opengeodeweb_back/routes/schemas/polygon_attribute_names.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
"filename": {
1313
"type": "string",
1414
"minLength": 1
15+
},
16+
"id": {
17+
"type": "string",
18+
"minLength": 1
1519
}
1620
},
1721
"required": [
1822
"input_geode_object",
19-
"filename"
23+
"filename",
24+
"id"
2025
],
2126
"additionalProperties": false
2227
}

src/opengeodeweb_back/routes/schemas/polyhedron_attribute_names.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
"filename": {
1313
"type": "string",
1414
"minLength": 1
15+
},
16+
"id": {
17+
"type": "string",
18+
"minLength": 1
1519
}
1620
},
1721
"required": [
1822
"input_geode_object",
19-
"filename"
23+
"filename",
24+
"id"
2025
],
2126
"additionalProperties": false
2227
}

src/opengeodeweb_back/routes/schemas/texture_coordinates.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
"filename": {
1313
"type": "string",
1414
"minLength": 1
15+
},
16+
"id": {
17+
"type": "string",
18+
"minLength": 1
1519
}
1620
},
1721
"required": [
1822
"input_geode_object",
23+
"id",
1924
"filename"
2025
],
2126
"additionalProperties": false

src/opengeodeweb_back/routes/schemas/vertex_attribute_names.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
"filename": {
1313
"type": "string",
1414
"minLength": 1
15+
},
16+
"id": {
17+
"type": "string",
18+
"minLength": 1
1519
}
1620
},
1721
"required": [
1822
"input_geode_object",
19-
"filename"
23+
"filename",
24+
"id"
2025
],
2126
"additionalProperties": false
2227
}

0 commit comments

Comments
 (0)