-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
121 lines (109 loc) · 4.95 KB
/
webapp.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
import os
from flask import Flask, flash, request, redirect, url_for, Response, send_file, send_from_directory
from werkzeug.utils import secure_filename
from app import liftboy
#from redis import Redis
#import rq
# LATER TRY TO MAKE THIS PATH RELATIVE
UPLOAD_FOLDER = '/home/cristiano/devel/liftboy-python/input'
ALLOWED_EXTENSIONS = set(['xml'])
app = Flask(__name__) #create the Flask app
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "super secret key"
#job = None
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/liftboy-form', methods=['GET', 'POST'])
def form_example():
#global job
# if the request is a POST
if request.method == 'POST':
# check if the post request has the file part
if 'metadata' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['metadata']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# connect to the 'liftboy-tasks' queue
#queue = rq.Queue('liftboy-tasks', connection=Redis.from_url('redis://'))
# istantiate the liftboy task
#job = queue.enqueue_call(func='app.liftboy_v0.10.do_lift', args=(filename,))
# get the job ID
#jID = job.get_id()
# add job metadata containing filename and progress status
#job.meta['filename'] = filename
#job.meta['progress'] = 0
#job.save_meta()
# redirect to URL containing the jID
#return redirect('/liftboy-form?jID='+jID, code=302)
return redirect('/liftboy-form?metadata=' + filename, code=302)
# if the request is a GET one but contains the job ID
#if request.args.get('jID'):
# if the request is a GET one but contains the input file name
if request.args.get('metadata'):
metadata = request.args.get('metadata')
# execute liftboy
liftboy.do_lift(metadata)
# connect to the 'liftboy-tasks' queue
#queue = rq.Queue('liftboy-tasks', connection=Redis.from_url('redis://'))
#task = queue.fetch_job(request.args.get('jID'))
#filename = job.meta.get('filename')
#progress = job.meta.get('progress')
# if execution of liftboy still in progress
#if not os.path.isfile('output/' + metadata + '_transformed.ediml'):
data = '''
<!doctype html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<!-- <meta http-equiv="refresh" content="2" /> -->
<title>Test liftboy-python interface</title>
</head>
<body>
<div>Processing file ''' + metadata + '''</div>
</body>
</html>
'''
#return data
#else:
#return send_file(data, attachment_filename='output/' + metadata + '_transformed.ediml')
#return send_file('output/' + metadata[:metadata.rfind('.')] + '_transformed.ediml', attachment_filename=metadata[:metadata.rfind('.')] + '_transformed.ediml')
return send_from_directory('output', metadata[:metadata.rfind('.')] + '_transformed.ediml', as_attachment=True)
# request is GET and no parameters are contained in the URL
data = '''
<!doctype html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<script>
var uID = Math.floor((Math.random()*100)+1);
function doSubmit() {
document.forms.liftboy_input.submit();
}
</script>
<title>Test liftboy-python interface</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" id="liftboy_input" name="liftboy_input">
Metadata:<br/>
<input type="file" id="metadata" name="metadata" accept="text/xml, application/xml"><br/>
Template: (optional)<br/>
<input type="file" id="template" name="template" accept="text/xml, application/xml"><br/>
<input type="submit" onClick="doSubmit()"><br>
</form>
</body>
</html>
'''
#res = Response(data, mimetype='text/html')
#res.headers["Content-Type"] = "text/html; charset=utf-8"
#return render_template('static/input.html')
return data
if __name__ == "__main__":
app.run()