Skip to content

Commit f0c9f55

Browse files
Merge pull request #84 from Geode-solutions/feat_viewable_file
feat(save_viewable): Added function
2 parents 426dfab + ef529a5 commit f0c9f55

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import flask_cors
88
from .. import geode_functions
99
import werkzeug
10+
import uuid
1011

1112

1213
routes = flask.Blueprint("routes", __name__)
@@ -204,3 +205,57 @@ def geode_objects_and_output_extensions():
204205
{"geode_objects_and_output_extensions": geode_objects_and_output_extensions},
205206
200,
206207
)
208+
209+
210+
with open(
211+
os.path.join(schemas, "save_viewable_file.json"),
212+
"r",
213+
) as file:
214+
save_viewable_file_json = json.load(file)
215+
216+
217+
@routes.route(
218+
save_viewable_file_json["route"],
219+
methods=save_viewable_file_json["methods"],
220+
)
221+
def save_viewable_file():
222+
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
223+
geode_functions.validate_request(flask.request, save_viewable_file_json)
224+
225+
secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
226+
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
227+
data = geode_functions.load(flask.request.json["input_geode_object"], file_path)
228+
generated_id = str(uuid.uuid4()).replace("-", "")
229+
230+
if geode_functions.is_viewable(flask.request.json["input_geode_object"]):
231+
name = data.name()
232+
else:
233+
name = flask.request.json["filename"]
234+
235+
native_extension = data.native_extension()
236+
237+
absolute_native_file_path = os.path.join(
238+
UPLOAD_FOLDER, generated_id + "." + native_extension
239+
)
240+
241+
saved_viewable_file_path = geode_functions.save_viewable(
242+
flask.request.json["input_geode_object"], data, UPLOAD_FOLDER, generated_id
243+
)
244+
geode_functions.save(
245+
flask.request.json["input_geode_object"],
246+
data,
247+
UPLOAD_FOLDER,
248+
generated_id + "." + native_extension,
249+
)
250+
251+
native_file_name = os.path.basename(absolute_native_file_path)
252+
viewable_file_name = os.path.basename(saved_viewable_file_path)
253+
return flask.make_response(
254+
{
255+
"name": name,
256+
"native_file_name": native_file_name,
257+
"viewable_file_name": viewable_file_name,
258+
"id": generated_id,
259+
},
260+
200,
261+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"route": "/save_viewable_file",
3+
"methods": [
4+
"POST"
5+
],
6+
"type": "object",
7+
"properties": {
8+
"input_geode_object": {
9+
"type": "string"
10+
},
11+
"filename": {
12+
"type": "string"
13+
}
14+
},
15+
"required": [
16+
"input_geode_object",
17+
"filename"
18+
],
19+
"additionalProperties": false
20+
}

tests/test_routes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,33 @@ def get_full_data():
155155
assert (
156156
error_message == "Validation error: 'input_geode_object' is a required property"
157157
)
158+
159+
160+
def test_save_viewable_file(client):
161+
route = f"/save_viewable_file"
162+
163+
def get_full_data():
164+
return {
165+
"input_geode_object": "BRep",
166+
"filename": "corbi.og_brep",
167+
}
168+
169+
# Normal test with filename 'corbi.og_brep'
170+
response = client.post(route, json=get_full_data())
171+
assert response.status_code == 200
172+
name = response.json["name"]
173+
assert type(name) is str
174+
native_file_name = response.json["native_file_name"]
175+
assert type(native_file_name) is str
176+
viewable_file_name = response.json["viewable_file_name"]
177+
assert type(viewable_file_name) is str
178+
id = response.json["id"]
179+
assert type(id) is str
180+
181+
for key, value in get_full_data().items():
182+
json = get_full_data()
183+
json.pop(key)
184+
response = client.post(route, json=json)
185+
assert response.status_code == 400
186+
error_description = response.json["description"]
187+
assert error_description == f"Validation error: '{key}' is a required property"

0 commit comments

Comments
 (0)