-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
125 lines (115 loc) · 4.05 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
import sys
import os
import random
import time
from flask import Flask, request, render_template, jsonify, make_response, send_file, flash, url_for, redirect, Markup, Response
import pyexcel
import HTML
import pdfkit
import zipfile
import datetime
from celery import Celery
import uuid
app = Flask(__name__)
app.secret_key = 'some_secret'
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
app.config['UPLOAD_FOLDER'] = 'uploads/'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@celery.task(bind=True)
def long_task(self):
"""Background task that runs a long function with progress reports."""
array = os.listdir(app.config['UPLOAD_FOLDER'])
self.update_state(state='PROGRESS',meta={'current': 0, 'total': 1, 'status': 'working...'})
for i in array:
if '.xlsx' in i:
filename = i
wb = pyexcel.get_sheet(file_name=str(os.path.join(app.config['UPLOAD_FOLDER'], filename)))
for i in range(0,10):
number = i * i
self.update_state(state='PROGRESS',meta={'current': i, 'total': 10, 'status': number})
time.sleep(1)
return {'current': 100, 'total': 100, 'status': 'Task completed!','result': 200}
@app.route('/longtask', methods=['POST'])
def longtask():
task = long_task.apply_async()
return jsonify({}), 202, {'Location': url_for('taskstatus', task_id=task.id)}
@app.route('/status/<task_id>')
def taskstatus(task_id):
task = long_task.AsyncResult(task_id)
if task.state == 'PENDING':
response = {
'state': task.state,
'current': 0,
'total': 1,
'status': 'Pending...'
}
elif task.state != 'FAILURE':
response = {
'state': task.state,
'current': task.info.get('current', 0),
'total': task.info.get('total', 1),
'status': task.info.get('status', '')
}
if 'result' in task.info:
response['result'] = task.info['result']
else:
# something went wrong in the background job
response = {
'state': task.state,
'current': 1,
'total': 1,
'status': str(task.info), # this is the exception raised
}
return jsonify(response)
@app.route('/', methods=['GET', 'POST'])
def upload():
if request.method == 'POST' and 'excel' in request.files:
file = request.files['excel']
filename = request.files['excel'].filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return render_template("upload.html")
@app.route("/checker", methods=['GET', 'POST'])
def checker():
today = datetime.date.today().strftime("%m-%d-%Y")
if request.method == 'POST':
numbers = request.form.getlist('accounts')
current_date = request.form.getlist('date')
accountlist = numbers[0].splitlines()
accountlist = [i.strip() for i in accountlist]
missing_account = []
missing_count = 0
for i in range(0,len(accountlist)):
if os.path.exists('/mnt/consentorders/' + str(current_date[0]) + '/' + str(accountlist[i]) + '.pdf') == False:
flash(Markup(str(accountlist[i]).strip()))
missing_count += 1
flash(Markup("There was " + str(missing_count) + " missing account(s)"))
return render_template('checker.html')
return render_template('checker.html', today=today)
@app.route("/delete", methods=['GET', 'POST'])
def delete():
today = datetime.date.today().strftime("%m-%d-%Y")
if request.method == 'POST':
numbers = request.form.getlist('accounts')
current_date = request.form.getlist('date')
accountlist = numbers[0].splitlines()
accountlist = [i.strip() for i in accountlist]
delete_account = []
delete_count = 0
for i in range(0,len(accountlist)):
if os.path.exists('/mnt/consentorders/' + str(current_date[0]) + '/' + str(accountlist[i]) + '.pdf') == True:
os.remove('/mnt/consentorders/' + str(current_date[0]) + '/' + str(accountlist[i]) + '.pdf')
flash(Markup(str(accountlist[i]).strip()))
delete_count += 1
flash(Markup("There was " + str(delete_count) + " account(s) deleted."))
return render_template('delete.html')
return render_template('delete.html', today=today)
if __name__ == "__main__":
# start web server
app.run(
#debug=True
threaded=True,
host='0.0.0.0',
port=80
)