This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
70 lines (60 loc) · 2.96 KB
/
views.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
import os
from flask import render_template, redirect, request, \
url_for, flash, current_app, make_response
from werkzeug.utils import secure_filename
from . import o2sc
from converter import Converter
from zip import Zip
ALLOWED_EXTENSIONS = set(['txt', 'csv', 'zip'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@o2sc.route('/o2sc', methods=['GET', 'POST'])
def o2sc():
app = current_app._get_current_object()
if request.method == 'POST':
# get the form data
file = request.files['file']
delimiter = str(request.form['delimiter'])
quote_char = str(request.form['quote-char'])
doc_type = str(request.form['doc-type'])
file_path_field = str(request.form['file-path-field'])
unique_id = str(request.form['unique-id'])
omit = str(request.form['omit'])
header_mapping = str(request.form['header-mapping'])
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
c = Converter()
if '.zip' in filename:
z = Zip()
extracted_list = z.unzip(app.config['UPLOAD_FOLDER'], filename)
for filename in extracted_list:
c.convert_file(app.config['UPLOAD_FOLDER'],
app.config['DOWNLOAD_FOLDER'],
filename,
delimiter,
quote_char,
doc_type,
file_path_field,
unique_id,
omit,
header_mapping)
zip_archive = z.zip(app.config['DOWNLOAD_FOLDER'], extracted_list)
response = make_response(zip_archive)
response.headers["Content-Disposition"] = "attachment; filename=_converted_zip_archive.zip"
return response
else:
converted_file = c.convert_file(app.config['UPLOAD_FOLDER'],
app.config['DOWNLOAD_FOLDER'],
filename,
delimiter,
quote_char,
doc_type,
file_path_field,
unique_id,
omit,
header_mapping)
response = make_response(converted_file)
response.headers["Content-Disposition"] = "attachment; filename=_converted_" + filename
return response
return render_template('o2sc/upload.html')