Skip to content

Commit a0022d8

Browse files
committed
geode_functions for refacto
1 parent 0e7d610 commit a0022d8

6 files changed

Lines changed: 86 additions & 70 deletions

File tree

src/opengeodeweb_back/geode_functions.py

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

89
# Local application imports
910
from .geode_objects import geode_objects_dict
@@ -37,6 +38,26 @@ def is_loadable(geode_object: str, file_absolute_path: str):
3738
def load(geode_object: str, file_absolute_path: str):
3839
return geode_object_value(geode_object)["load"](file_absolute_path)
3940

41+
def load_from_request(geode_object: str, data_folder_path: str, request_json: dict):
42+
file_absolute_path = os.path.join(
43+
data_folder_path,
44+
request_json["id"],
45+
werkzeug.utils.secure_filename(request_json["filename"]),
46+
)
47+
return load(geode_object, file_absolute_path)
48+
49+
def build_data_path(data_folder_path, request_json, filename):
50+
return os.path.join(
51+
data_folder_path,
52+
request_json["id"],
53+
werkzeug.utils.secure_filename(filename),
54+
)
55+
56+
def build_upload_file_path(upload_folder, filename):
57+
secure_filename = werkzeug.utils.secure_filename(filename)
58+
return os.path.abspath(os.path.join(upload_folder, secure_filename)
59+
)
60+
4061

4162
def is_saveable(geode_object: str, data, filename: str):
4263
return geode_object_value(geode_object)["is_saveable"](data, filename)

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def upload_file():
7575

7676
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
7777
if not os.path.exists(UPLOAD_FOLDER):
78-
os.makedirs(UPLOAD_FOLDER)
78+
os.mkdir(UPLOAD_FOLDER)
7979
file = flask.request.files["file"]
8080
filename = werkzeug.utils.secure_filename(os.path.basename(file.filename))
8181
file.save(os.path.join(UPLOAD_FOLDER, filename))
@@ -99,7 +99,8 @@ def allowed_objects():
9999

100100
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
101101
utils_functions.validate_request(flask.request, allowed_objects_json)
102-
file_absolute_path = os.path.join(UPLOAD_FOLDER, flask.request.json["filename"])
102+
file_absolute_path = geode_functions.build_upload_file_path(
103+
UPLOAD_FOLDER, flask.request.json["filename"])
103104
allowed_objects = geode_functions.list_geode_objects(
104105
file_absolute_path, flask.request.json["supported_feature"]
105106
)
@@ -123,7 +124,8 @@ def missing_files():
123124

124125
missing_files = geode_functions.missing_files(
125126
flask.request.json["input_geode_object"],
126-
os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]),
127+
geode_functions.build_upload_file_path(
128+
UPLOAD_FOLDER, flask.request.json["filename"]),
127129
)
128130
has_missing_files = missing_files.has_missing_files()
129131

@@ -215,7 +217,8 @@ def geode_objects_and_output_extensions():
215217
)
216218
data = geode_functions.load(
217219
flask.request.json["input_geode_object"],
218-
os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]),
220+
geode_functions.build_upload_file_path(
221+
UPLOAD_FOLDER, flask.request.json["filename"]),
219222
)
220223
geode_objects_and_output_extensions = (
221224
geode_functions.geode_objects_output_extensions(
@@ -288,12 +291,12 @@ def create_point():
288291
def texture_coordinates():
289292
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
290293
utils_functions.validate_request(flask.request, texture_coordinates_json)
291-
data = geode_functions.load(
292-
flask.request.json["input_geode_object"],
293-
os.path.join(
294-
DATA_FOLDER_PATH, flask.request.json["id"], flask.request.json["filename"]
295-
),
296-
)
294+
data = geode_functions.load_from_request(
295+
flask.request.json["input_geode_object"],
296+
DATA_FOLDER_PATH,
297+
flask.request.json,
298+
)
299+
297300
texture_coordinates = data.texture_manager().texture_names()
298301

299302
return flask.make_response({"texture_coordinates": texture_coordinates}, 200)
@@ -313,14 +316,12 @@ def texture_coordinates():
313316
def vertex_attribute_names():
314317
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
315318
utils_functions.validate_request(flask.request, vertex_attribute_names_json)
316-
file_absolute_path = os.path.join(
317-
DATA_FOLDER_PATH,
318-
flask.request.json["id"],
319-
werkzeug.utils.secure_filename(flask.request.json["filename"]),
320-
)
321-
data = geode_functions.load(
322-
flask.request.json["input_geode_object"], file_absolute_path
319+
data = geode_functions.load_from_request(
320+
flask.request.json["input_geode_object"],
321+
DATA_FOLDER_PATH,
322+
flask.request.json,
323323
)
324+
324325
vertex_attribute_names = data.vertex_attribute_manager().attribute_names()
325326

326327
return flask.make_response(
@@ -345,14 +346,12 @@ def vertex_attribute_names():
345346
def polygon_attribute_names():
346347
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
347348
utils_functions.validate_request(flask.request, polygon_attribute_names_json)
348-
file_absolute_path = os.path.join(
349-
DATA_FOLDER_PATH,
350-
flask.request.json["id"],
351-
werkzeug.utils.secure_filename(flask.request.json["filename"]),
352-
)
353-
data = geode_functions.load(
354-
flask.request.json["input_geode_object"], file_absolute_path
349+
data = geode_functions.load_from_request(
350+
flask.request.json["input_geode_object"],
351+
DATA_FOLDER_PATH,
352+
flask.request.json,
355353
)
354+
356355
polygon_attribute_names = data.polygon_attribute_manager().attribute_names()
357356

358357
return flask.make_response(
@@ -377,14 +376,12 @@ def polygon_attribute_names():
377376
def polyhedron_attribute_names():
378377
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
379378
utils_functions.validate_request(flask.request, vertex_attribute_names_json)
380-
file_absolute_path = os.path.join(
381-
DATA_FOLDER_PATH,
382-
flask.request.json["id"],
383-
werkzeug.utils.secure_filename(flask.request.json["filename"]),
384-
)
385-
data = geode_functions.load(
386-
flask.request.json["input_geode_object"], file_absolute_path
379+
data = geode_functions.load_from_request(
380+
flask.request.json["input_geode_object"],
381+
DATA_FOLDER_PATH,
382+
flask.request.json,
387383
)
384+
388385
polyhedron_attribute_names = data.polyhedron_attribute_manager().attribute_names()
389386

390387
return flask.make_response(

src/opengeodeweb_back/routes/models/blueprint_models.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,32 @@
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"],
23-
flask.request.json["id"],
24-
"viewable.vtm",
25-
)
26-
tree = ET.parse(vtm_file_path)
21+
try:
22+
vtm_file_path = geode_functions.build_data_path(
23+
flask.current_app.config["DATA_FOLDER_PATH"],
24+
flask.request.json,
25+
"viewable.vtm"
26+
)
27+
tree = ET.parse(vtm_file_path)
28+
except FileNotFoundError:
29+
return flask.make_response({"error": "VTM file not found"}, 404)
2730
root = tree.find("vtkMultiBlockDataSet")
2831
uuid_to_flat_index = {}
2932
current_index = 0
3033

3134
for elem in root.iter():
3235
if "uuid" in elem.attrib and elem.tag == "DataSet":
3336
uuid_to_flat_index[elem.attrib["uuid"]] = current_index
34-
3537
current_index += 1
36-
37-
return flask.make_response(
38-
{"uuid_to_flat_index": uuid_to_flat_index},
39-
200,
40-
)
38+
return flask.make_response({"uuid_to_flat_index": uuid_to_flat_index}, 200)
4139

4240

43-
def extract_model_uuids(geode_object, file_path):
44-
model = geode_functions.load(geode_object, file_path)
41+
def extract_model_uuids(model):
4542
mesh_components = model.mesh_components()
46-
4743
uuid_dict = {}
48-
4944
for mesh_component, ids in mesh_components.items():
5045
component_name = mesh_component.get()
5146
uuid_dict[component_name] = [id.string() for id in ids]
52-
5347
return uuid_dict
5448

5549

@@ -60,12 +54,13 @@ def extract_model_uuids(geode_object, file_path):
6054
@routes.route(mesh_components_json["route"], methods=mesh_components_json["methods"])
6155
def extract_uuids_endpoint():
6256
utils_functions.validate_request(flask.request, mesh_components_json)
63-
file_path = os.path.join(
64-
flask.current_app.config["DATA_FOLDER_PATH"],
65-
flask.request.json["id"],
66-
flask.request.json["filename"],
67-
)
68-
if not os.path.exists(file_path):
57+
try:
58+
model = geode_functions.load_from_request(
59+
flask.request.json["geode_object"],
60+
flask.current_app.config["DATA_FOLDER_PATH"],
61+
flask.request.json,
62+
)
63+
except FileNotFoundError:
6964
return flask.make_response({"error": "File not found"}, 404)
70-
uuid_dict = extract_model_uuids(flask.request.json["geode_object"], file_path)
65+
uuid_dict = extract_model_uuids(model)
7166
return flask.make_response({"uuid_dict": uuid_dict}, 200)

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
# Standard library imports
12
import os
23
import time
34
import shutil
45

6+
# Third party imports
57
import pytest
8+
9+
# Local application imports
610
from app import app
711

812

@@ -28,3 +32,7 @@ def client():
2832
def app_context():
2933
with app.app_context():
3034
yield
35+
36+
@pytest.fixture
37+
def test_id():
38+
return "1"

tests/test_models_routes.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22
import shutil
3-
4-
def test_model_mesh_components(client):
5-
test_id = "1"
3+
def test_model_mesh_components(client, test_id):
64
route = "/models/vtm_component_indices"
75

86
base_path = client.application.config["DATA_FOLDER_PATH"]
@@ -25,8 +23,7 @@ def test_model_mesh_components(client):
2523
assert isinstance(key, str)
2624

2725

28-
def test_extract_brep_uuids(client):
29-
test_id = "1"
26+
def test_extract_brep_uuids(client, test_id):
3027
route = "/models/mesh_components"
3128

3229
base_path = client.application.config["DATA_FOLDER_PATH"]
@@ -52,4 +49,3 @@ def test_extract_brep_uuids(client):
5249
for values in uuid_dict.values():
5350
assert isinstance(values, list)
5451
assert all(isinstance(v, str) for v in values)
55-

tests/test_routes.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Standard library imports
22
import os
3-
43
# Third party imports
54
from werkzeug.datastructures import FileStorage
65

@@ -170,12 +169,12 @@ def get_full_data():
170169
test_utils.test_route_wrong_params(client, route, get_full_data)
171170

172171

173-
def test_texture_coordinates(client):
172+
def test_texture_coordinates(client, test_id):
174173
response = client.post(
175174
"/texture_coordinates",
176175
json={
177176
"input_geode_object": "PolygonalSurface3D",
178-
"id": "1",
177+
"id": test_id,
179178
"filename": "hat.vtp",
180179
},
181180
)
@@ -186,7 +185,7 @@ def test_texture_coordinates(client):
186185
assert type(texture_coordinate) is str
187186

188187

189-
def test_vertex_attribute_names(client):
188+
def test_vertex_attribute_names(client, test_id):
190189
route = f"/vertex_attribute_names"
191190
for geode_object, value in geode_objects.geode_objects_dict().items():
192191
if value["object_type"] == "mesh":
@@ -206,7 +205,7 @@ def test_vertex_attribute_names(client):
206205
def get_full_data():
207206
return {
208207
"input_geode_object": geode_object,
209-
"id": "1",
208+
"id": test_id,
210209
"filename": f"test.{input_extension}",
211210
}
212211

@@ -223,7 +222,7 @@ def get_full_data():
223222
test_utils.test_route_wrong_params(client, route, get_full_data)
224223

225224

226-
def test_polygon_attribute_names(client):
225+
def test_polygon_attribute_names(client, test_id):
227226
route = f"/polygon_attribute_names"
228227
for geode_object, value in geode_objects.geode_objects_dict().items():
229228
if value["object_type"] == "mesh":
@@ -243,7 +242,7 @@ def test_polygon_attribute_names(client):
243242
def get_full_data():
244243
return {
245244
"input_geode_object": geode_object,
246-
"id": "1",
245+
"id": test_id,
247246
"filename": f"test.{input_extension}",
248247
}
249248

@@ -260,7 +259,7 @@ def get_full_data():
260259
test_utils.test_route_wrong_params(client, route, get_full_data)
261260

262261

263-
def test_polyhedron_attribute_names(client):
262+
def test_polyhedron_attribute_names(client, test_id):
264263
route = f"/polyhedron_attribute_names"
265264
for geode_object, value in geode_objects.geode_objects_dict().items():
266265
if value["object_type"] == "mesh":
@@ -280,7 +279,7 @@ def test_polyhedron_attribute_names(client):
280279
def get_full_data():
281280
return {
282281
"input_geode_object": geode_object,
283-
"id": "1",
282+
"id": test_id,
284283
"filename": f"test.{input_extension}",
285284
}
286285

0 commit comments

Comments
 (0)