-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
56 lines (40 loc) · 1.51 KB
/
app.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
import os
import time
from dotenv import load_dotenv
from flask import Flask, after_this_request, request, jsonify, render_template, send_from_directory
from werkzeug.utils import secure_filename
from convert import ldap_convert, process_csv
app = Flask(__name__)
load_dotenv()
@app.route("/")
def index():
return render_template("upload.html")
@app.post("/convert/csv")
def convert_csv():
file = request.files["file"]
if file:
orig_filename = secure_filename(file.filename)
new_filename = f"{orig_filename.split('.')[0]}_{str(time.time_ns() // 1_000_000)}.csv"
save_path = os.path.join("uploads", new_filename)
file.save(save_path)
converted_csv_filename = process_csv(new_filename)
@after_this_request
def remove_files(response):
try:
os.remove(save_path)
os.remove(os.path.join("outputs", converted_csv_filename))
except Exception as err:
print(f"Could not remove temp files: {err}")
pass
return response
return send_from_directory("outputs", converted_csv_filename)
else:
return handle_error(400, "No csv file found.")
@app.post("/convert/json")
def convert_json():
students = request.json["students"]
if students is None:
return handle_error(400, "No student list found in request body.")
return ldap_convert(students)
def handle_error(code, message):
return jsonify({"message": message, "code": code}), code