-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
210 lines (164 loc) · 5.96 KB
/
app.py
File metadata and controls
210 lines (164 loc) · 5.96 KB
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
import tasks
import csv
import shutil
import os
from tasks import long_task
from flask import Flask, render_template, request, jsonify, Response, url_for
from utils import items_to_csv
# ---------------------------- #
# GLOBAL #
# ---------------------------- #
app = Flask(__name__)
app.secret_key = "EuropeanaOnTheFly"
RESULT = None
# ---------------------------- #
# VIEWS #
# ---------------------------- #
@app.route("/")
def index():
return render_template('index.html')
@app.route("/runTask", methods=['POST'])
def longtask():
if request.method == 'POST':
usr_data = request.json
usr_data.update({'root': app.root_path})
# Execute background task
task = tasks.long_task.delay(usr_data=request.json)
print('Background task just started')
print(usr_data)
return jsonify({}), 202, {'Location': url_for('taskstatus',
task_id=task.id)}
else:
render_template('index.html')
@app.route('/status/<task_id>')
def taskstatus(task_id):
task = long_task.AsyncResult(task_id)
# Background task at work
if task.state == 'working':
print('RECEIVED STATE : working')
response = {
'state': task.state,
'info': task.info
}
# Background task is done
elif task.state == 'SUCCESS':
print('RECEIVED STATE : SUCCESS')
result = task.info['info']
# The api call returned something
if result['query_status'] is True:
print('No data found in query result')
# No results found after api call
if result['data'] is None:
response = {
'state': 'empty',
'dir': result['dir']
}
# Data available after api call
else:
print('data available in query result')
print('SENDING TASK')
print(result['dir'])
print(os.listdir('{}/public/'.format(app.root_path)))
if not os.path.isdir('{}/public/{}/'.format(app.root_path, result['dir'])):
os.mkdir('{}/public/{}/'.format(app.root_path, result['dir']))
items_to_csv(result['data'],
'{}/public/{}/output.csv'.format(app.root_path, result['dir']))
response = {
'state': 'loaded',
'data': result['data'],
'dir': result['dir']
}
# There was an error during api call
else:
response = {
'state': 'error',
'dir': result['dir'],
'error': result['error']
}
# Something went wrong the the background task
else:
print('unseen query state :', task.state)
response = {
'state': 'error',
'dir': '',
'error': 'Problème lors de la gestion de la requête'
}
return jsonify(response)
@app.route('/display/<dir_name>', methods=['GET', 'POST'])
def display(dir_name):
# Read csv to display
f_path = app.root_path + '/public/' + dir_name + '/output.csv'
try:
with open(f_path) as f:
items = [i for i in csv.DictReader(f)]
except FileNotFoundError:
print(f_path)
print(os.listdir('{}/public/'.format(app.root_path)))
# Create download urls
csv_url = '/download-csv/' + dir_name
json_url = '/download-json/' + dir_name
xml_url = '/download-xml/' + dir_name
# Render template
return render_template('display.html',
items=items, dir=dir_name,
json=json_url, csv=csv_url, xml=xml_url,
len=len(items))
@app.route('/cleaning/<dir_name>', methods=['GET', 'POST'])
def clean(dir_name):
print('Cleaning demand received')
if request.method == 'POST':
try:
dir_path = app.root_path + '/public/' + dir_name
shutil.rmtree(dir_path)
print(dir_name, ' has been removed !')
except Exception as e:
print('An error occured during dir cleaning : ')
print(e)
return '', 204
# ---------------------------- #
# DOWLOAD VIEWS #
# ---------------------------- #
@app.route('/download-csv/<dir_name>', methods=['GET', 'POST'])
def download_csv(dir_name):
print('demande de téléchargement pour :', dir_name)
file_path = app.root_path + '/public/' + dir_name + '/output.csv'
with open(file_path) as f:
data = f.read()
return Response(
data,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=europeana_data.csv"})
@app.route('/download-json/<dir_name>', methods=['GET', 'POST'])
def download_json(dir_name):
print('demande de téléchargement pour :', dir_name)
file_path = app.root_path + '/public/' + dir_name + '/output.json'
with open(file_path) as f:
data = f.read()
return Response(
data,
mimetype="text/json",
headers={"Content-disposition":
"attachment; filename=europeana_data.json"})
@app.route('/download-xml/<dir_name>', methods=['GET', 'POST'])
def download_xml(dir_name):
print('demande de téléchargement pour :', dir_name)
file_path = app.root_path + '/public/' + dir_name + '/output.xml'
with open(file_path) as f:
data = f.read()
return Response(
data,
mimetype="text/xml",
headers={"Content-disposition":
"attachment; filename=europeana_data.xml"})
# @app.errorhandler(Exception)
# def handle_error(e):
# code = 500
# if isinstance(e, HTTPException):
# code = e.code
# return render_template('error.html')
# ---------------------------- #
# MAIN #
# ---------------------------- #
if __name__ == "__main__":
app.run(debug=True)