-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.py
64 lines (48 loc) · 1.66 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
57
58
59
60
61
62
63
64
# coding: utf8
"""
core logic for export xls sheet to mysql table.
"""
import os
from flask import Flask, request, render_template
from werkzeug.utils import secure_filename
import tablib
import resource
UPLOAD_FOLDER = 'tmp'
ALLOWED_EXTENSIONS = ('xls', 'xlsx')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.debug = True
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
mappings = {
'sample(sample.xls)': 'sample',
}
@app.route('/upload', methods=['POST', 'GET'])
def upload():
result = False
errmsg = ''
if request.method == 'POST':
f = request.files['file']
m = request.form['model']
if f and allowed_file(f.filename):
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.mkdir(app.config['UPLOAD_FOLDER'])
path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename))
f.save(path)
model = resource.sources.get(m)
if model is None:
errmsg = 'model %s not found' % m
else:
with open(path, 'rb') as fs:
data_book = tablib.import_book(fs.read())
model.import_data(data_book)
result = True
else:
errmsg = 'only xls, xlsx file allowed'
return render_template('index.html', mappings=mappings, status='success' if result else 'failure', request=request,
errmsg=errmsg)
@app.route('/')
def index():
return render_template('index.html', mappings=mappings)
if __name__ == '__main__':
app.run(port=5001)