Skip to content

Commit 2f935be

Browse files
committed
refactored functions moved into utils_functions file. So we can import them into blueprint_route.py.
1 parent 01159cb commit 2f935be

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -235,43 +235,6 @@ def geode_objects_and_output_extensions():
235235
) as file:
236236
save_viewable_file_json = json.load(file)
237237

238-
def save_geode_object(geode_object, data, folder_absolute_path, id):
239-
saved_native_file_path = geode_functions.save(
240-
geode_object, data, folder_absolute_path, id + "." + data.native_extension()
241-
)
242-
saved_viewable_file_path = geode_functions.save_viewable(
243-
geode_object, data, folder_absolute_path, id
244-
)
245-
saved_light_viewable_file_path = geode_functions.save_light_viewable(
246-
geode_object, data, folder_absolute_path, "light_" + id
247-
)
248-
f = open(saved_light_viewable_file_path, "rb")
249-
binary_light_viewable = f.read()
250-
f.close()
251-
return {
252-
"native_file_name": os.path.basename(saved_native_file_path[0]),
253-
"viewable_file_name": os.path.basename(saved_viewable_file_path[0]),
254-
"binary_light_viewable": str(binary_light_viewable, "utf-8"),
255-
}
256-
257-
def create_geode_object_response(geode_object, data, folder_absolute_path):
258-
generated_id = str(uuid.uuid4()).replace("-", "")
259-
name = data.name()
260-
object_type = geode_functions.get_object_type(geode_object)
261-
262-
saved_files_info = save_geode_object(
263-
geode_object, data, folder_absolute_path, generated_id
264-
)
265-
266-
return {
267-
"name": name,
268-
"native_file_name": saved_files_info["native_file_name"],
269-
"viewable_file_name": saved_files_info["viewable_file_name"],
270-
"id": generated_id,
271-
"object_type": object_type,
272-
"binary_light_viewable": saved_files_info["binary_light_viewable"],
273-
"geode_object": geode_object,
274-
}
275238

276239
@routes.route(
277240
save_viewable_file_json["route"],
@@ -284,13 +247,11 @@ def save_viewable_file():
284247
secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
285248
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
286249
data = geode_functions.load(flask.request.json["input_geode_object"], file_path)
287-
generated_id = str(uuid.uuid4()).replace("-", "")
250+
288251
name = data.name()
289252
object_type = geode_functions.get_object_type(flask.request.json["input_geode_object"])
290-
return flask.make_response(
291-
create_geode_object_response(flask.request.json["input_geode_object"], data, DATA_FOLDER_PATH),
292-
200,
293-
)
253+
utils_functions.create_geode_object_response(flask.request.json["input_geode_object"], data, DATA_FOLDER_PATH),
254+
return flask.make_response(200, {})
294255

295256

296257
with open(os.path.join(schemas, "create_point.json"), "r") as file:
@@ -314,7 +275,7 @@ def create_point():
314275
generated_id = str(uuid.uuid4()).replace("-", "")
315276
object_type = geode_functions.get_object_type("PointSet3D")
316277
return flask.make_response(
317-
create_geode_object_response("PointSet3D", PointSet3D, DATA_FOLDER_PATH),
278+
utils_functions.create_geode_object_response("PointSet3D", PointSet3D, DATA_FOLDER_PATH),
318279
200,
319280
)
320281

src/opengeodeweb_back/utils_functions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import threading
44
import time
5+
import uuid
56
import zipfile
67

78
# Third party imports
@@ -10,6 +11,7 @@
1011
import importlib.metadata as metadata
1112

1213
# Local application imports
14+
from opengeodeweb_back import geode_functions
1315

1416

1517
def increment_request_counter(current_app):
@@ -147,3 +149,42 @@ def handle_exception(e):
147149
)
148150
response.content_type = "application/json"
149151
return response
152+
153+
154+
def save_native_viewable_binary_file_names(geode_object, data, folder_absolute_path):
155+
generated_id = str(uuid.uuid4()).replace("-", "")
156+
saved_native_file_path = geode_functions.save(
157+
geode_object, data, folder_absolute_path, generated_id + "." + data.native_extension()
158+
)
159+
saved_viewable_file_path = geode_functions.save_viewable(
160+
geode_object, data, folder_absolute_path, generated_id
161+
)
162+
saved_light_viewable_file_path = geode_functions.save_light_viewable(
163+
geode_object, data, folder_absolute_path, "light_" + generated_id
164+
)
165+
f = open(saved_light_viewable_file_path, "rb")
166+
binary_light_viewable = f.read()
167+
f.close()
168+
return {
169+
"native_file_name": os.path.basename(saved_native_file_path[0]),
170+
"viewable_file_name": os.path.basename(saved_viewable_file_path[0]),
171+
"binary_light_viewable": str(binary_light_viewable, "utf-8"),
172+
}
173+
def create_geode_object_response(geode_object, data, folder_absolute_path):
174+
generated_id = str(uuid.uuid4()).replace("-", "")
175+
name = data.name()
176+
object_type = geode_functions.get_object_type(geode_object)
177+
178+
native_file_name, viewable_file_name, binary_light_viewable = save_native_viewable_binary_file_names(
179+
geode_object, data, folder_absolute_path
180+
)
181+
182+
return {
183+
"name": name,
184+
"native_file_name": native_file_name,
185+
"viewable_file_name": viewable_file_name,
186+
"id": generated_id,
187+
"object_type": object_type,
188+
"binary_light_viewable": binary_light_viewable,
189+
"geode_object": geode_object,
190+
}

0 commit comments

Comments
 (0)