-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
252 lines (228 loc) · 10.1 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from flask import Flask, render_template, flash, request
import os
import threading
from wtforms import Form
from commander import CMDWriter
from report import Table, Item, PDFItem, PDFTable, CommanderTable, CommanderItem
# App config
app = Flask(__name__)
app.config.from_object('config.Config')
BASE_FOLDER = app.config['BASE_FOLDER']
REPORTS_FOLDER = app.config['REPORTS_FOLDER']
SPIDER = app.config['SPIDER']
REPORT_NAME = ''
EMAIL = ''
URL = ''
class ReusableForm(Form):
print(">>> REPORT STARTED!")
# REPORT_NAME = StringField('report_name', validators=[validators.DataRequired(), validators.Length(min=6, max=35)])
# EMAIL = StringField('email', validators=[validators.DataRequired(), validators.Length(min=6, max=35)])
# URL = StringField('url', validators=[validators.Length(min=-1, max=35)])
@app.route('/', methods=['GET', 'POST'])
def audit_request():
global REPORT_NAME, EMAIL, URL
form = ReusableForm(request.form)
print(form.errors)
if request.method == 'POST':
# Get form
form_response = request.form
# Report name
try:
REPORT_NAME = form_response['report_name'].replace(' ', '_')
except Exception as e:
print(e)
# Email
try:
EMAIL = form_response['email']
except Exception as e:
print(e)
# URL
try:
URL = form_response['url']
except Exception as e:
print(e)
# Handle form variables
if form.validate():
# SEO
try:
SEOInternal = form_response['SEOInternal']
except Exception as e:
SEOInternal = False
print(e)
try:
SEOExternal = form_response['SEOExternal']
except Exception as e:
SEOExternal = False
print(e)
# CSV Upload
CSVUpload = False
try:
# check if the post request has the file part
if 'UploadCSV' not in request.files:
print('No file part')
# return redirect(request.url)
file = request.files['UploadCSV']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
print('No selected file')
# return redirect(request.url)
if file and allowed_file(file.filename):
app.config['UPLOAD_FOLDER'] = os.path.join(app.config['UPLOAD_FOLDER'], REPORT_NAME, 'CSV')
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
os.chdir(REPORTS_FOLDER)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
CSVUpload = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
# return redirect(url_for('uploaded_file',
# filename=filename))
except Exception as e:
print(e)
# PDF Audit
try:
PDFAudit = form_response['PDFAudit']
except Exception as e:
PDFAudit = False
print(e)
# Lighthouse
try:
LighthouseMOBILE = form_response['lighthouse-mobile']
except Exception as e:
LighthouseMOBILE = False
print(e)
try:
LighthouseDESKTOP = form_response['lighthouse-desktop']
except Exception as e:
LighthouseDESKTOP = False
print(e)
# AXE
try:
AXEChrome = form_response['AXEChrome']
except Exception as e:
AXEChrome = False
print(e)
try:
AXEFirefox = form_response['AXEFirefox']
except Exception as e:
AXEFirefox = False
print(e)
try:
AXEEdge = form_response['AXEEdge']
except Exception as e:
AXEEdge = False
print(e)
# Take off
msg = ('Your request to run audits on', URL, 'for', REPORT_NAME, 'has been registered.',
'An email will be sent to', EMAIL, 'once the report is complete.')
flash(' '.join(msg))
# Start main thread
t = threading.Thread(target=CMDWriter, args=(REPORT_NAME, URL, EMAIL, SEOInternal, SEOExternal, CSVUpload,
PDFAudit, LighthouseMOBILE, LighthouseDESKTOP,
AXEChrome, AXEFirefox, AXEEdge))
print('REQUEST:before STARTING thread:', t.name)
t.setDaemon(True)
t.start()
else:
# Error for required fields
msg = 'Error: the following fields are required:'
if not REPORT_NAME:
msg += ' [Report Name] '
if not EMAIL:
msg += ' [Email] '
if SPIDER:
if not URL:
msg += ' [URL] '
flash(msg)
return render_template('request_form.html', form=form, spider=SPIDER)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/action_restart/', methods=['GET', 'POST'])
def action_restart():
# Set vars
# sort = request.args.get('sort', 'id')
# URL = ''
# email = ''
REPORT_NAME = request.args.get('id')
report_folder = os.path.join(REPORTS_FOLDER, REPORT_NAME)
request_log = os.path.join(report_folder, 'logs', '_request_log.tuple')
report_type = request.args.get('report_type')
# Check for request_log to get:
if os.path.exists(request_log):
with open(request_log, 'r') as file:
reader = file.read()
request_tuple = eval(reader)
for tup in request_tuple:
# get email
if tup[0] == 'email':
report_email = tup[1]
# get url
if tup[0] == 'url':
report_url = tup[1]
# csv_path = os.path.join(REPORTS_FOLDER, REPORT_NAME, 'CSV')
if report_type == 'spider':
t = threading.Thread(target=CMDWriter, args=(REPORT_NAME, URL, EMAIL, True, True, False, False, False, False,
False, False, False))
t.setDaemon(True)
t.start()
if report_type == 'axe':
t = threading.Thread(target=CMDWriter, args=(REPORT_NAME, URL, EMAIL, False, False, False, False, False, False,
True, True, False))
t.setDaemon(True)
t.start()
if report_type == 'lighthouse':
t = threading.Thread(target=CMDWriter, args=(REPORT_NAME, URL, EMAIL, False, False, False, False, True, True,
False, False, False))
t.setDaemon(True)
t.start()
if report_type == 'pdf':
t = threading.Thread(target=CMDWriter, args=(REPORT_NAME, URL, EMAIL, False, False, False, True, False, False,
False, False, False))
t.setDaemon(True)
t.start()
return reports()
@app.route('/reports/', methods=['GET', 'POST'])
def reports():
sort = request.args.get('sort', 'id')
id = request.args.get('id')
reverse = (request.args.get('direction', 'asc') == 'desc')
# REPORT_NAME = request.args.get('report_name')
if id:
return index(id)
else:
reports_list = CommanderTable(CommanderItem.get_sorted_by(sort, reverse),
sort_by=sort, sort_reverse=reverse)
return render_template("report.html", reports_list=reports_list)
@app.route('/report/', methods=['GET', 'POST'])
@app.route('/report/<int:id>')
def index(id):
sort = request.args.get('sort', 'id')
reverse = (request.args.get('direction', 'asc') == 'desc')
REPORT_NAME = request.args.get('id')
# dash = DashTable(DashItem.get_sorted_by(sort, 'dash', reverse), sort_by=sort, sort_reverse=reverse)
lighthouse = Table(Item.get_sorted_by(sort, 'lighthouse', reverse), sort_by=sort, sort_reverse=reverse)
axe_c = Table(Item.get_sorted_by(sort, 'axe_c', reverse), sort_by=sort, sort_reverse=reverse)
axe_c_summary = Table(Item.get_sorted_by(sort, 'axe_c_summary', reverse), sort_by=sort, sort_reverse=reverse)
pdf = PDFTable(PDFItem.get_sorted_by(sort, 'pdf', reverse), sort_by=sort, sort_reverse=reverse)
# pdf_external = PDFTable(PDFItem.get_sorted_by(sort, 'pdf_external', reverse), sort_by=sort, sort_reverse=reverse)
return render_template("report.html",
report_name=REPORT_NAME,
axe_u=axe_c_summary,
axe_u_title='Summary AXE errors:',
axe_u_desc='A summary AXE errors...',
pdf=pdf,
pdf_title='Summary PDF errors (INTERNAL):',
pdf_desc='A summary of PDF errors which are internally hosted.',
# pdf_external=pdf_external,
# pdf_external_title='Summary PDF errors (EXTERNAL):',
# pdf_external_desc='A summary pf PDF errors for documents externally hosted.',
axe=axe_c,
axe_title='Full AXE Accessibility Results',
axe_desc='Full, unfiltered AXE results',
lighthouse=lighthouse,
lighthouse_title='Lighthouse Accessibility Results',
lighthouse_desc='Lighthouse is a ... '
'The following violations were ...')
if __name__ == '__main__':
app.run(host=app.config['HOST'],
port=app.config['PORT'])