|
| 1 | +# Standard library imports |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +# Third party imports |
| 6 | +import flask |
| 7 | +import flask_cors |
| 8 | +from .. import geode_functions |
| 9 | +import werkzeug |
| 10 | + |
| 11 | + |
| 12 | +routes = flask.Blueprint("routes", __name__) |
| 13 | +flask_cors.CORS(routes) |
| 14 | + |
| 15 | + |
| 16 | +with open( |
| 17 | + "src/opengeodeweb_back/routes/schemas/allowed_files.json", |
| 18 | + "r", |
| 19 | +) as file: |
| 20 | + allowed_files_json = json.load(file) |
| 21 | + |
| 22 | + |
| 23 | +@routes.route( |
| 24 | + allowed_files_json["route"], |
| 25 | + methods=allowed_files_json["methods"], |
| 26 | +) |
| 27 | +def allowed_files(): |
| 28 | + geode_functions.validate_request(flask.request, allowed_files_json) |
| 29 | + extensions = geode_functions.list_input_extensions(flask.request.json["key"]) |
| 30 | + return {"status": 200, "extensions": extensions} |
| 31 | + |
| 32 | + |
| 33 | +with open( |
| 34 | + "src/opengeodeweb_back/routes/schemas/upload_file.json", |
| 35 | + "r", |
| 36 | +) as file: |
| 37 | + upload_file_json = json.load(file) |
| 38 | + |
| 39 | + |
| 40 | +@routes.route( |
| 41 | + upload_file_json["route"], |
| 42 | + methods=upload_file_json["methods"], |
| 43 | +) |
| 44 | +def upload_file(): |
| 45 | + if flask.request.method == "OPTIONS": |
| 46 | + return flask.make_response({}, 200) |
| 47 | + |
| 48 | + UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"] |
| 49 | + if not os.path.exists(UPLOAD_FOLDER): |
| 50 | + os.mkdir(UPLOAD_FOLDER) |
| 51 | + files = flask.request.files.getlist("content") |
| 52 | + |
| 53 | + for file in files: |
| 54 | + filename = werkzeug.utils.secure_filename(os.path.basename(file.filename)) |
| 55 | + print(f"{filename=}") |
| 56 | + file.save(os.path.join(UPLOAD_FOLDER, filename)) |
| 57 | + return flask.make_response({"message": "File uploaded"}, 201) |
| 58 | + |
| 59 | + |
| 60 | +with open( |
| 61 | + "src/opengeodeweb_back/routes/schemas/allowed_objects.json", |
| 62 | + "r", |
| 63 | +) as file: |
| 64 | + allowed_objects_json = json.load(file) |
| 65 | + |
| 66 | + |
| 67 | +@routes.route( |
| 68 | + allowed_objects_json["route"], |
| 69 | + methods=allowed_objects_json["methods"], |
| 70 | +) |
| 71 | +def allowed_objects(): |
| 72 | + geode_functions.validate_request(flask.request, allowed_objects_json) |
| 73 | + file_extension = os.path.splitext(flask.request.json["filename"])[1][1:] |
| 74 | + allowed_objects = geode_functions.list_geode_objects( |
| 75 | + file_extension, flask.request.json["key"] |
| 76 | + ) |
| 77 | + |
| 78 | + return flask.make_response({"allowed_objects": allowed_objects}, 200) |
| 79 | + |
| 80 | + |
| 81 | +with open( |
| 82 | + "src/opengeodeweb_back/routes/schemas/missing_files.json", |
| 83 | + "r", |
| 84 | +) as file: |
| 85 | + missing_files_json = json.load(file) |
| 86 | + |
| 87 | + |
| 88 | +@routes.route( |
| 89 | + missing_files_json["route"], |
| 90 | + methods=missing_files_json["methods"], |
| 91 | +) |
| 92 | +def missing_files(): |
| 93 | + UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"] |
| 94 | + geode_functions.validate_request(flask.request, missing_files_json) |
| 95 | + |
| 96 | + missing_files = geode_functions.missing_files( |
| 97 | + flask.request.json["input_geode_object"], |
| 98 | + os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]), |
| 99 | + ) |
| 100 | + has_missing_files = missing_files.has_missing_files() |
| 101 | + mandatory_files = missing_files.mandatory_files |
| 102 | + additional_files = missing_files.additional_files |
| 103 | + |
| 104 | + mandatory_files_list = [] |
| 105 | + for mandatory_file in mandatory_files: |
| 106 | + mandatory_files_list.append(os.path.basename(mandatory_file)) |
| 107 | + |
| 108 | + additional_files_list = [] |
| 109 | + for additional_file in additional_files: |
| 110 | + additional_files_list.append(os.path.basename(additional_file)) |
| 111 | + |
| 112 | + return flask.make_response( |
| 113 | + { |
| 114 | + "has_missing_files": has_missing_files, |
| 115 | + "mandatory_files": mandatory_files_list, |
| 116 | + "additional_files": additional_files_list, |
| 117 | + }, |
| 118 | + 200, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +with open( |
| 123 | + "src/opengeodeweb_back/routes/schemas/geographic_coordinate_systems.json", |
| 124 | + "r", |
| 125 | +) as file: |
| 126 | + geographic_coordinate_systems_json = json.load(file) |
| 127 | + |
| 128 | + |
| 129 | +@routes.route( |
| 130 | + geographic_coordinate_systems_json["route"], |
| 131 | + methods=geographic_coordinate_systems_json["methods"], |
| 132 | +) |
| 133 | +def crs_converter_geographic_coordinate_systems(): |
| 134 | + geode_functions.validate_request(flask.request, geographic_coordinate_systems_json) |
| 135 | + infos = geode_functions.geographic_coordinate_systems( |
| 136 | + flask.request.json["input_geode_object"] |
| 137 | + ) |
| 138 | + crs_list = [] |
| 139 | + |
| 140 | + for info in infos: |
| 141 | + crs = {} |
| 142 | + crs["name"] = info.name |
| 143 | + crs["code"] = info.code |
| 144 | + crs["authority"] = info.authority |
| 145 | + crs_list.append(crs) |
| 146 | + |
| 147 | + return flask.make_response({"crs_list": crs_list}, 200) |
| 148 | + |
| 149 | + |
| 150 | +with open( |
| 151 | + "src/opengeodeweb_back/routes/schemas/geode_objects_and_output_extensions.json", |
| 152 | + "r", |
| 153 | +) as file: |
| 154 | + geode_objects_and_output_extensions_json = json.load(file) |
| 155 | + |
| 156 | + |
| 157 | +@routes.route( |
| 158 | + geode_objects_and_output_extensions_json["route"], |
| 159 | + methods=geode_objects_and_output_extensions_json["methods"], |
| 160 | +) |
| 161 | +def geode_objects_and_output_extensions(): |
| 162 | + UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"] |
| 163 | + |
| 164 | + geode_functions.validate_request( |
| 165 | + flask.request, geode_objects_and_output_extensions_json |
| 166 | + ) |
| 167 | + |
| 168 | + data = geode_functions.load( |
| 169 | + flask.request.json["input_geode_object"], |
| 170 | + os.path.join( |
| 171 | + os.path.abspath(UPLOAD_FOLDER), |
| 172 | + flask.request.json["filename"], |
| 173 | + ), |
| 174 | + ) |
| 175 | + geode_objects_and_output_extensions = ( |
| 176 | + geode_functions.geode_objects_output_extensions( |
| 177 | + flask.request.json["input_geode_object"], data |
| 178 | + ) |
| 179 | + ) |
| 180 | + |
| 181 | + print(geode_objects_and_output_extensions) |
| 182 | + return flask.make_response( |
| 183 | + {"geode_objects_and_output_extensions": geode_objects_and_output_extensions}, |
| 184 | + 200, |
| 185 | + ) |
0 commit comments