webfront.py

Tue, 23 Jan 2018 14:09:52 +0200

author
Santeri Piippo
date
Tue, 23 Jan 2018 14:09:52 +0200
changeset 29
db6ca177c6c4
child 32
75f44d3063da
permissions
-rw-r--r--

added a simple web frontend

29
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
1 from flask import Flask, render_template, redirect, request
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
2 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
3 from ldcheck import read_ldraw
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
4 from testsuite import load_tests, check_model, format_report_html
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
5
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
6 app = Flask('LDCheck')
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
7
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
8 @app.route('/', methods = ['GET', 'POST'])
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
9 def web_front():
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
10 if request.method == 'POST':
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
11 # check if the post request has the file part
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
12 if 'file' not in request.files or not request.files['file'].filename:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
13 return redirect(request.url)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
14 file = request.files['file']
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
15 print(type(file))
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
16 config = load_config('ldcheck.cfg')
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
17 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
18 with ldconfig_ldr_path.open() as ldconfig_ldr:
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
19 load_colours(ldconfig_ldr)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
20 model = read_ldraw(
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
21 file.stream,
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
22 name = file.filename,
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
23 config = config,
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
24 )
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
25 test_suite = load_tests()
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
26 report = check_model(model, test_suite)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
27 return str.format(
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
28 '<!doctype html><html><body><ul>{report}</ul></body></html>',
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
29 report = format_report_html(report, model, test_suite)
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
30 )
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
31 return '''
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
32 <!doctype html>
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
33 <title>Upload new File</title>
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
34 <h1>Upload new File</h1>
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
35 <form method=post enctype=multipart/form-data>
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
36 <p><input type=file name=file>
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
37 <input type=submit value=Upload>
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
38 </form>
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
39 '''
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
40
db6ca177c6c4 added a simple web frontend
Santeri Piippo
parents:
diff changeset
41 app.run()

mercurial