|
| 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 | +schemas = os.path.join(os.path.dirname(__file__), "schemas") |
| 17 | + |
| 18 | +with open( |
| 19 | + os.path.join(schemas, "allowed_files.json"), |
| 20 | + "r", |
| 21 | +) as file: |
| 22 | + allowed_files_json = json.load(file) |
| 23 | + |
| 24 | + |
| 25 | +@routes.route( |
| 26 | + allowed_files_json["route"], |
| 27 | + methods=allowed_files_json["methods"], |
| 28 | +) |
| 29 | +def allowed_files(): |
| 30 | + geode_functions.validate_request(flask.request, allowed_files_json) |
| 31 | + extensions = geode_functions.list_input_extensions(flask.request.json["key"]) |
| 32 | + return {"status": 200, "extensions": extensions} |
| 33 | + |
| 34 | + |
| 35 | +with open( |
| 36 | + os.path.join(schemas, "upload_file.json"), |
| 37 | + "r", |
| 38 | +) as file: |
| 39 | + upload_file_json = json.load(file) |
| 40 | + |
| 41 | + |
| 42 | +@routes.route( |
| 43 | + upload_file_json["route"], |
| 44 | + methods=upload_file_json["methods"], |
| 45 | +) |
| 46 | +def upload_file(): |
| 47 | + if flask.request.method == "OPTIONS": |
| 48 | + return flask.make_response({}, 200) |
| 49 | + |
| 50 | + UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"] |
| 51 | + if not os.path.exists(UPLOAD_FOLDER): |
| 52 | + os.mkdir(UPLOAD_FOLDER) |
| 53 | + file = flask.request.files["file"] |
| 54 | + filename = werkzeug.utils.secure_filename(os.path.basename(file.filename)) |
| 55 | + file.save(os.path.join(UPLOAD_FOLDER, filename)) |
| 56 | + return flask.make_response({"message": "File uploaded"}, 201) |
| 57 | + |
| 58 | + |
| 59 | +with open( |
| 60 | + os.path.join(schemas, "allowed_objects.json"), |
| 61 | + "r", |
| 62 | +) as file: |
| 63 | + allowed_objects_json = json.load(file) |
| 64 | + |
| 65 | + |
| 66 | +@routes.route( |
| 67 | + allowed_objects_json["route"], |
| 68 | + methods=allowed_objects_json["methods"], |
| 69 | +) |
| 70 | +def allowed_objects(): |
| 71 | + if flask.request.method == "OPTIONS": |
| 72 | + return flask.make_response({}, 200) |
| 73 | + |
| 74 | + UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"] |
| 75 | + geode_functions.validate_request(flask.request, allowed_objects_json) |
| 76 | + file_absolute_path = os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]) |
| 77 | + allowed_objects = geode_functions.list_geode_objects( |
| 78 | + file_absolute_path, flask.request.json["key"] |
| 79 | + ) |
| 80 | + return flask.make_response({"allowed_objects": allowed_objects}, 200) |
| 81 | + |
| 82 | + |
| 83 | +with open( |
| 84 | + os.path.join(schemas, "missing_files.json"), |
| 85 | + "r", |
| 86 | +) as file: |
| 87 | + missing_files_json = json.load(file) |
| 88 | + |
| 89 | + |
| 90 | +@routes.route( |
| 91 | + missing_files_json["route"], |
| 92 | + methods=missing_files_json["methods"], |
| 93 | +) |
| 94 | +def missing_files(): |
| 95 | + UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"] |
| 96 | + geode_functions.validate_request(flask.request, missing_files_json) |
| 97 | + |
| 98 | + missing_files = geode_functions.missing_files( |
| 99 | + flask.request.json["input_geode_object"], |
| 100 | + os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]), |
| 101 | + ) |
| 102 | + has_missing_files = missing_files.has_missing_files() |
| 103 | + |
| 104 | + mandatory_files = [] |
| 105 | + for mandatory_file in missing_files.mandatory_files: |
| 106 | + mandatory_files.append(os.path.basename(mandatory_file)) |
| 107 | + |
| 108 | + additional_files = [] |
| 109 | + for additional_file in missing_files.additional_files: |
| 110 | + additional_files.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, |
| 116 | + "additional_files": additional_files, |
| 117 | + }, |
| 118 | + 200, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +with open( |
| 123 | + os.path.join(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 | + os.path.join(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 | + geode_functions.validate_request( |
| 164 | + flask.request, geode_objects_and_output_extensions_json |
| 165 | + ) |
| 166 | + data = geode_functions.load( |
| 167 | + flask.request.json["input_geode_object"], |
| 168 | + os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]), |
| 169 | + ) |
| 170 | + geode_objects_and_output_extensions = ( |
| 171 | + geode_functions.geode_objects_output_extensions( |
| 172 | + flask.request.json["input_geode_object"], data |
| 173 | + ) |
| 174 | + ) |
| 175 | + return flask.make_response( |
| 176 | + {"geode_objects_and_output_extensions": geode_objects_and_output_extensions}, |
| 177 | + 200, |
| 178 | + ) |
0 commit comments