-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
executable file
·373 lines (317 loc) · 11.6 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from flask import (
Flask,
render_template,
send_from_directory,
request,
jsonify,
session,
url_for,
Response,
)
from flask_cors import CORS, cross_origin # Import CORS
import json, os
from utils import (
fetch_data,
get_filtered_rows_count,
add_data_request,
get_request_data_from_storage,
get_requests,
get_summarized_data,
send_email,
get_boolean_data_from_file,
get_authors_list,
get_formatted_authors_response,
fetch_pdf_data,
fetch_qc_data,
update_qc_csv_data,
)
import collaborators_utils
app_mode = os.getenv("FLASK_APP_MODE", "user")
application = Flask(__name__, static_folder="static/build")
# CORS(application, resources={r"/*": {"origins": "http://localhost:3000"}})
CORS(application)
@application.route("/config", methods=["GET"])
@cross_origin()
def get_config():
return jsonify({"mode": app_mode}), 200
@application.route("/view", methods=["POST"])
def view_data():
if app_mode == "admin":
data = request.data
filters = json.loads(data)
result = fetch_data(filters["data"])
if result["success"]:
if "data" in result:
return jsonify(
{
"status": "success",
"data": result["data"],
"redirect_url": url_for("display"),
}
)
else:
return jsonify({"status": "success"})
else:
error_message = result.get("error", "An error has occurred")
return jsonify({"error": error_message}), 500
else:
return jsonify({"error": "Invalid operation"}), 401
@application.route("/download", methods=["POST"])
@cross_origin()
def download_data():
if app_mode == "admin":
data = request.data
filters = json.loads(data)
result = fetch_data(filters["data"], download=True)
if result["success"] and "data" in result:
csv = result["data"]
response = Response(csv, mimetype="text/csv")
response.headers["Content-Disposition"] = "attachment; filename=data.csv"
return response
elif result["success"]:
return Response(
json.dumps({"error": "No data was fetched"}),
status=404,
mimetype="application/json",
)
else:
error_message = result.get("error", "An error has occurred")
return Response(
json.dumps({"error": error_message}),
status=500,
mimetype="application/json",
)
else:
return jsonify({"error": "Invalid operation"}), 401
@application.route("/rows-count", methods=["POST", "OPTIONS"])
@cross_origin()
def get_rows_count():
if request.method == "OPTIONS":
# Specific handling for preflight request if needed
return _build_cors_preflight_response()
filters = json.loads(request.data)
print(filters)
result = get_filtered_rows_count(application.static_folder, filters)
if result["success"] and "count" in result:
return jsonify({"count": result["count"]}), 200
return jsonify({"error": "Error"}), 500
@application.route("/boolean-data", methods=["GET", "OPTIONS"])
@cross_origin()
def get_boolean_data():
if request.method == "OPTIONS":
# Specific handling for preflight request if needed
return _build_cors_preflight_response()
staticPath = application.static_folder
result = get_boolean_data_from_file(staticPath)
return result
@application.route("/metrics", methods=["GET", "OPTIONS"])
@cross_origin()
def get_metrics():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
try:
with open(application.static_folder + "/anonymized_data/metrics_data.json", "r") as file:
behavioral_data = json.loads(file.read())
data = {
"behavioral": behavioral_data,
"imaging": [
"T1",
"T2",
"DWI",
"FLAIR",
"Native_Lesion",
"MNI_T1",
"MNI_Lesion_Mask",
],
}
return data, 200
except FileNotFoundError:
return jsonify({"error": "File not found"}), 404
@application.route("/get-results", methods=["GET"])
@cross_origin()
def get_results():
if "result_data" in session:
return jsonify(session["result_data"])
return jsonify({"error": "No data found"}), 404
@application.route("/submit-request", methods=["POST"])
@cross_origin()
def submit_request():
data = json.loads(request.data)
response = add_data_request(data)
if response["success"]:
return jsonify({"status": "success", "message": response["message"]}), 200
else:
return (
jsonify(
{
"error": "There was an error submitting your request",
"message": response["message"],
}
),
500,
)
@application.route("/get-requests", methods=["GET", "OPTIONS"])
@cross_origin()
def fetch_requests():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
if app_mode == "admin":
files_data = get_requests()
print(files_data)
return jsonify(files_data), 200
else:
return jsonify({"error": "Invalid operation"}), 401
@application.route("/get-request/<filename>")
@cross_origin()
def get_request_data(filename):
if app_mode == "admin":
data = get_request_data_from_storage(filename)
return data, 200
else:
return jsonify({"error": "Invalid operation"}), 401
@application.route("/data-summary", methods=["POST"])
@cross_origin()
def get_data_and_summary():
response = {}
if app_mode == "admin":
data = json.loads(request.data)
response = get_summarized_data(data)
json_data = json.dumps(response, default=str)
return json_data, 200
else:
return jsonify({"error": "Invalid operation"}), 401
@application.route("/data-summary/<filename>")
@cross_origin()
def get_data_and_summary_by_filename(filename):
response = {}
if app_mode == "admin":
data_request = get_request_data_from_storage(filename)
response = get_summarized_data(data_request["data"])
json_data = json.dumps(response, default=str)
return json_data, 200
else:
return jsonify({"error": "Invalid operation"}), 401
@application.route("/send-email", methods=["GET"])
@cross_origin()
def send_email_to():
print("sending")
response = send_email("lmuthyal@usc.edu")
return jsonify({"error": "Invalid operation", "response": response}), 200
@application.route("/get-authors-list", methods=["GET", "OPTIONS"])
@cross_origin()
def fetch_authors_list():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
if app_mode == "admin":
response = get_authors_list()
# json_data = json.dumps(response, default=str)
return response["data"], 200
else:
return jsonify({"error": "Invalid operation"}), 401
@application.route("/formatted-authors", methods=["POST", "OPTIONS"])
@cross_origin()
def get_formatted_authors():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
if app_mode == "admin":
request_data = json.loads(request.data)
response_txt = get_formatted_authors_response(request_data)
return jsonify({"formattedAuthors": response_txt}), 200
@application.route("/qc-pdf-data/<bids_id>/<ses_id>", methods=["GET"])
@cross_origin()
def get_qc_pdf(bids_id, ses_id):
return fetch_pdf_data(bids_id, ses_id)
@application.route("/qc-data", methods=["GET"])
@cross_origin()
def get_qc_subjects_data():
return fetch_qc_data(), 200
@application.route("/qc-pdf-data/<bids_id>/<ses_id>", methods=["POST"])
@cross_origin()
def update_qc_data(bids_id, ses_id):
data = json.loads(request.data)
res = update_qc_csv_data(bids_id, ses_id, data)
if res:
return jsonify({"success": True}), 200
return jsonify({"success": False}), 500
@application.route("/collaborators/get_user_details", methods=["GET", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def get_user_details():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.get_user_details(request)
@application.route("/collaborators/update_user_details", methods=["POST", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def update_user_details():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.update_user_details(request)
@application.route("/collaborators/delete_collaborator", methods=["DELETE", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def delete_collaborator():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.delete_collaborator(request)
@application.route("/collaborators/add_collaborator", methods=["POST", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def add_collaborator():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.add_collaborator(request)
@application.route("/collaborators/get_all_collaborators", methods=["GET", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def get_all_collaborators():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.get_all_collaborators(request)
@application.route("/collaborators/get_admins", methods=["GET", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def get_admins():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.get_admins(request)
@application.route("/collaborators/add_admin", methods=["POST", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def add_admin():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.add_admin(request)
@application.route("/collaborators/delete_admin", methods=["DELETE","OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def delete_admin():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.delete_admin(request)
@application.route("/collaborators/check_admin_status", methods=["GET", "OPTIONS"])
@cross_origin()
@collaborators_utils.authenticate
def check_admin_status():
if request.method == "OPTIONS":
return _build_cors_preflight_response()
return collaborators_utils.check_admin_status(request)
def _build_cors_preflight_response():
response = jsonify({"status": "success"})
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
response.headers.add("Access-Control-Max-Age", "10000")
response.headers.add(
"Access-Control-Allow-Headers", "Content-Type,Authorization, X-Requested-With"
)
return response
@application.route("/", defaults={"path": ""})
@application.route("/<path:path>")
@cross_origin()
def form(path):
if path != "" and os.path.exists(os.path.join(application.static_folder, path)):
return send_from_directory(application.static_folder, path)
else:
return send_from_directory(application.static_folder, 'index.html')
if __name__ == "__main__":
application.run(debug=True)