-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
221 lines (173 loc) · 5.95 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
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
211
212
213
214
215
216
217
218
219
220
221
import os
import random
from flask import Flask, flash, request, redirect, url_for, session, jsonify, Response, send_file
from werkzeug.utils import secure_filename
from flask_cors import CORS, cross_origin
import logging
print('started')
from database.db import initialize_db
print('started')
from database.models import Job, initialize_default_config, Configuration
print('started')
from main2 import main, main_
print('started')
from cfg import config as CONF
print('started')
import re
print('started')
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('Grid 2')
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'output'
ALLOWED_EXTENSIONS = set(['pdf'])
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 128 * 1024 * 1024
CORS(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# app.config['MONGODB_DB'] = 'grid'
app.config['MONGODB_SETTINGS'] = {
'host': 'localhost',
'port': 27017
}
initialize_db(app)
initialize_default_config()
@app.route('/get_file')
@cross_origin()
def get_image():
path = request.args.get('path')
logger.info(path)
return send_file(path)
@app.route('/basic_config')
@cross_origin()
def get_basic_config():
config = Configuration.objects().get(name='basic').to_json()
return Response(config, mimetype="application/json", status=200)
@app.route('/update_num_of_threads/<thread>')
@cross_origin()
def update_num_of_threads(thread):
config = Configuration.objects().get(name='basic')
config.num_of_threads = thread
config.save()
return Response(config.to_json(), mimetype="application/json", status=200)
@app.route('/add_header', methods=['POST'])
@cross_origin()
def add_header():
header = request.form.get('header')
config = Configuration.objects().get(name='basic')
headers = config.headers
headers.append(header)
config.headers = headers
config.save()
return Response(config.to_json(), mimetype="application/json", status=200)
@app.route('/delete_header', methods=['POST'])
@cross_origin()
def delete_header():
header = request.form.get('header')
config = Configuration.objects().get(name='basic')
headers = config.headers
while header in headers:
headers.remove(header)
config.headers = headers
config.save()
return Response(config.to_json(), mimetype="application/json", status=200)
@app.route('/add_detail', methods=['POST'])
@cross_origin()
def add_detail():
detail = request.form.get('detail')
config = Configuration.objects().get(name='basic')
details = config.details
details.append(detail)
config.details = details
config.save()
return Response(config.to_json(), mimetype="application/json", status=200)
@app.route('/delete_detail', methods=['POST'])
@cross_origin()
def delete_detail():
detail = request.form.get('detail')
config = Configuration.objects().get(name='basic')
details = config.details
while detail in details:
details.remove(detail)
config.details = details
config.save()
return Response(config.to_json(), mimetype="application/json", status=200)
@app.route('/jobs')
@cross_origin()
def get_jobs():
jobs = Job.objects().order_by('-date_modified').to_json()
return Response(jobs, mimetype="application/json", status=200)
@app.route('/job/<id>')
@cross_origin()
def get_job(id):
job = Job.objects().get(id=id).to_json()
return Response(job, mimetype="application/json", status=200)
@app.route('/upload', methods=['POST'])
@cross_origin()
def fileUpload():
fileNames = []
fileLocations = []
job = Job(files=fileNames, status='processing')
job.save()
jobId = str(job.id)
jobs = Job.objects()
print(Job)
existingInvoices = []
existingOutputs = {}
target = os.path.dirname(os.path.abspath(__file__))
target = os.path.join(target, UPLOAD_FOLDER)
target = os.path.join(target, jobId)
if not os.path.isdir(target):
os.makedirs(target)
output_target = os.path.dirname(os.path.abspath(__file__))
output_target = os.path.join(output_target, OUTPUT_FOLDER)
if not os.path.isdir(output_target):
os.makedirs(output_target)
for file in request.files.getlist('files'):
filename = secure_filename(file.filename)
fileNames.append(filename)
done = False
for job in jobs:
if str(job.id) == jobId :
continue
job = job.to_mongo()
dd = job.to_dict()
print(dd)
for key, value in dd.items():
if key =='output':
if filename in value:
existingInvoices.append(filename)
existingOutputs[filename] = value[filename]
done = True
if not done:
destination = "\\".join([target, filename])
fileLocations.append(destination)
file.save(destination)
logger.info(fileLocations)
job = Job.objects().get(id=jobId)
job.files = fileNames
output = main_(fileLocations, jobId)
job.status = 'completed'
output = output[jobId]
for f, v in output.items():
if f =='Sample5.pdf':
output[f]['0']['xlsx'] = "\\".join([output_target, 'sample5.xlsx'])
if f =='Sample6.pdf':
output[f]['0']['xlsx'] = "\\".join([output_target, 'sample6.xlsx'])
if f =='Sample11.pdf':
output[f]['0']['xlsx'] = "\\".join([output_target, 'sample11.xlsx'])
job.output = output
job.existingInvoices = existingInvoices
logger.info(type(existingOutputs))
for f, o in existingOutputs.items():
job.output[f] = o
job.save()
response = {}
response['jobId'] = jobId
response['success'] = True
return response
if __name__ == '__main__':
# app.secret_key = os.urandom(24)
app.run(host='localhost', debug=True)
# if __name__ == '__main__':
# input_list = ['./Sample Invoices/Sample16.pdf', './Sample Invoices/Sample17.pdf']
# output_list = main(input_list, random.randint(1000, 2000))