Skip to content

Commit 03d121c

Browse files
authored
Merge pull request #210 from Geode-solutions/fix/refactor_model_components
fix(Models): single route for models mesh_components
2 parents 60dea88 + 5eeced4 commit 03d121c

File tree

8 files changed

+56
-139
lines changed

8 files changed

+56
-139
lines changed

opengeodeweb_back_schemas.json

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,6 @@
3333
}
3434
},
3535
"models": {
36-
"vtm_component_indices": {
37-
"$id": "opengeodeweb_back/models/vtm_component_indices",
38-
"route": "/vtm_component_indices",
39-
"methods": [
40-
"POST"
41-
],
42-
"type": "object",
43-
"properties": {
44-
"id": {
45-
"type": "string",
46-
"minLength": 1
47-
}
48-
},
49-
"required": [
50-
"id"
51-
],
52-
"additionalProperties": false
53-
},
5436
"mesh_components": {
5537
"$id": "opengeodeweb_back/models/mesh_components",
5638
"route": "/mesh_components",

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,3 @@ werkzeug==3.1.2
6060
# flask
6161
# flask-cors
6262

63-
opengeodeweb-microservice==1.*,>=1.0.12

src/opengeodeweb_back/routes/models/blueprint_models.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212

1313

1414
@routes.route(
15-
schemas_dict["vtm_component_indices"]["route"],
16-
methods=schemas_dict["vtm_component_indices"]["methods"],
15+
schemas_dict["mesh_components"]["route"],
16+
methods=schemas_dict["mesh_components"]["methods"],
1717
)
18-
def uuid_to_flat_index() -> flask.Response:
18+
def mesh_components() -> flask.Response:
1919
json_data = utils_functions.validate_request(
20-
flask.request, schemas_dict["vtm_component_indices"]
20+
flask.request, schemas_dict["mesh_components"]
2121
)
22-
params = schemas.VtmComponentIndices.from_dict(json_data)
22+
params = schemas.MeshComponents.from_dict(json_data)
23+
model = geode_functions.load_geode_object(params.id)
24+
if not isinstance(model, GeodeModel):
25+
flask.abort(400, f"{params.id} is not a GeodeModel")
26+
2327
vtm_file_path = geode_functions.data_file_path(params.id, "viewable.vtm")
2428
tree = ET.parse(vtm_file_path)
2529
root = tree.find("vtkMultiBlockDataSet")
@@ -32,24 +36,22 @@ def uuid_to_flat_index() -> flask.Response:
3236
if "uuid" in elem.attrib and elem.tag == "DataSet":
3337
uuid_to_flat_index[elem.attrib["uuid"]] = current_index
3438
current_index += 1
35-
return flask.make_response({"uuid_to_flat_index": uuid_to_flat_index}, 200)
39+
model_mesh_components = model.mesh_components()
40+
mesh_components = []
41+
for mesh_component, ids in model_mesh_components.items():
42+
component_type = mesh_component.get()
43+
for id in ids:
44+
geode_id = id.string()
45+
component_name = geode_id
46+
viewer_id = uuid_to_flat_index[geode_id]
3647

48+
mesh_component_object = {
49+
"id": params.id,
50+
"viewer_id": viewer_id,
51+
"geode_id": geode_id,
52+
"name": component_name,
53+
"type": component_type,
54+
}
55+
mesh_components.append(mesh_component_object)
3756

38-
@routes.route(
39-
schemas_dict["mesh_components"]["route"],
40-
methods=schemas_dict["mesh_components"]["methods"],
41-
)
42-
def extract_uuids_endpoint() -> flask.Response:
43-
json_data = utils_functions.validate_request(
44-
flask.request, schemas_dict["mesh_components"]
45-
)
46-
params = schemas.MeshComponents.from_dict(json_data)
47-
model = geode_functions.load_geode_object(params.id)
48-
if not isinstance(model, GeodeModel):
49-
flask.abort(400, f"{params.id} is not a GeodeModel")
50-
mesh_components = model.mesh_components()
51-
uuid_dict = {}
52-
for mesh_component, ids in mesh_components.items():
53-
component_name = mesh_component.get()
54-
uuid_dict[component_name] = [id.string() for id in ids]
55-
return flask.make_response({"uuid_dict": uuid_dict}, 200)
57+
return flask.make_response({"mesh_components": mesh_components}, 200)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
from .vtm_component_indices import *
21
from .mesh_components import *

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/test_models_routes.py

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,34 @@
1111
from opengeodeweb_back import geode_functions
1212
from opengeodeweb_back.geode_objects.geode_brep import GeodeBRep
1313

14-
base_dir = os.path.abspath(os.path.dirname(__file__))
15-
data_dir = os.path.join(base_dir, "data")
16-
1714

18-
def test_model_mesh_components(client: FlaskClient, test_id: str) -> None:
19-
route = "/opengeodeweb_back/models/vtm_component_indices"
20-
21-
with client.application.app_context():
22-
data_path = geode_functions.data_file_path(test_id, "viewable.vtm")
23-
os.makedirs(os.path.dirname(data_path), exist_ok=True)
24-
shutil.copy(os.path.join(data_dir, "cube.vtm"), data_path)
25-
26-
response = client.post(route, json={"id": test_id})
27-
assert response.status_code == 200
15+
from .test_routes import test_save_viewable_file
2816

29-
uuid_dict = response.get_json()["uuid_to_flat_index"]
30-
assert isinstance(uuid_dict, dict)
17+
base_dir = os.path.abspath(os.path.dirname(__file__))
18+
data_dir = os.path.join(base_dir, "data")
3119

32-
indices = list(uuid_dict.values())
33-
indices.sort()
34-
assert all(indices[i] > indices[i - 1] for i in range(1, len(indices)))
35-
for uuid in uuid_dict.keys():
36-
assert isinstance(uuid, str)
3720

21+
def test_mesh_components(client: FlaskClient) -> None:
22+
geode_object_type = "BRep"
23+
filename = "cube.og_brep"
24+
response = test_save_viewable_file(client, geode_object_type, filename)
3825

39-
def test_extract_brep_uuids(client: FlaskClient, test_id: str) -> None:
4026
route = "/opengeodeweb_back/models/mesh_components"
4127
brep_filename = os.path.join(data_dir, "cube.og_brep")
4228

43-
with client.application.app_context():
44-
data = Data.create(
45-
geode_object=GeodeBRep.geode_object_type(),
46-
viewer_object=GeodeBRep.viewer_type(),
47-
input_file=brep_filename,
48-
)
49-
data.native_file = brep_filename
50-
session = get_session()
51-
if session:
52-
session.commit()
53-
54-
response = client.post(route, json={"id": data.id})
55-
assert response.status_code == 200
56-
assert "uuid_dict" in response.get_json()
57-
uuid_dict = response.get_json()["uuid_dict"]
58-
assert isinstance(uuid_dict, dict)
29+
response = client.post(route, json={"id": response.get_json()["id"]})
30+
assert response.status_code == 200
31+
assert "mesh_components" in response.get_json()
32+
mesh_components = response.get_json()["mesh_components"]
33+
assert isinstance(mesh_components, list)
34+
assert len(mesh_components) > 0
35+
for mesh_component in mesh_components:
36+
assert isinstance(mesh_component, object)
37+
assert isinstance(mesh_component["id"], str)
38+
assert isinstance(mesh_component["geode_id"], str)
39+
assert isinstance(mesh_component["viewer_id"], int)
40+
assert isinstance(mesh_component["name"], str)
41+
assert isinstance(mesh_component["type"], str)
5942

6043

6144
def test_export_project_route(client: FlaskClient, tmp_path: Path) -> None:
@@ -178,33 +161,6 @@ def test_import_project_route(client: FlaskClient, tmp_path: Path) -> None:
178161
client.application.config["DATA_FOLDER_PATH"] = original_data_folder
179162

180163

181-
def test_save_viewable_workflow_from_file(client: FlaskClient) -> None:
182-
file = os.path.join(data_dir, "cube.og_brep")
183-
upload_resp = client.put(
184-
"/opengeodeweb_back/upload_file",
185-
data={"file": FileStorage(open(file, "rb"))},
186-
)
187-
assert upload_resp.status_code == 201
188-
189-
route = "/opengeodeweb_back/save_viewable_file"
190-
payload = {"geode_object_type": "BRep", "filename": "cube.og_brep"}
191-
192-
response = client.post(route, json=payload)
193-
assert response.status_code == 200
194-
195-
data_id = response.get_json()["id"]
196-
assert isinstance(data_id, str) and len(data_id) > 0
197-
assert response.get_json()["viewable_file"].endswith(".vtm")
198-
199-
comp_resp = client.post(
200-
"/opengeodeweb_back/models/vtm_component_indices", json={"id": data_id}
201-
)
202-
assert comp_resp.status_code == 200
203-
204-
refreshed = Data.get(data_id)
205-
assert refreshed is not None
206-
207-
208164
def test_save_viewable_workflow_from_object(client: FlaskClient) -> None:
209165
route = "/opengeodeweb_back/create/point"
210166
point_data = {

tests/test_routes.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Third party imports
55
from werkzeug.datastructures import FileStorage
66
from flask.testing import FlaskClient
7+
from werkzeug.test import TestResponse
78

89
# Local application imports
910
from opengeodeweb_microservice.database.data import Data
@@ -160,14 +161,18 @@ def get_full_data() -> test_utils.JsonData:
160161
test_utils.test_route_wrong_params(client, route, get_full_data)
161162

162163

163-
def test_save_viewable_file(client: FlaskClient) -> None:
164-
test_upload_file(client, filename="corbi.og_brep")
164+
def test_save_viewable_file(
165+
client: FlaskClient,
166+
geode_object_type: str = "BRep",
167+
filename: str = "corbi.og_brep",
168+
) -> TestResponse:
169+
test_upload_file(client, filename)
165170
route = f"/opengeodeweb_back/save_viewable_file"
166171

167172
def get_full_data() -> test_utils.JsonData:
168173
return {
169-
"geode_object_type": "BRep",
170-
"filename": "corbi.og_brep",
174+
"geode_object_type": geode_object_type,
175+
"filename": filename,
171176
}
172177

173178
# Normal test with filename 'corbi.og_brep'
@@ -187,6 +192,7 @@ def get_full_data() -> test_utils.JsonData:
187192

188193
# Test all params
189194
test_utils.test_route_wrong_params(client, route, get_full_data)
195+
return response
190196

191197

192198
def test_texture_coordinates(client: FlaskClient, test_id: str) -> None:

0 commit comments

Comments
 (0)