webfront.py

changeset 100
62759e5c4554
parent 99
01941a811b5a
child 112
d997456985b2
equal deleted inserted replaced
99:01941a811b5a 100:62759e5c4554
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 from flask import Flask, render_template, redirect, request 2 from flask import Flask, render_template, redirect, request
3 from ldcheck import appname, version, version_string
3 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths 4 from ldcheck import load_config, load_colours, find_ldconfig_ldr_paths
4 from parse import read_ldraw 5 from parse import read_ldraw
5 from testsuite import load_tests, check_model, problem_text, all_problem_types 6 from testsuite import load_tests, check_model, problem_text, all_problem_types
6 7
7 app = Flask('LDCheck') 8 app = Flask(appname)
8 9
9 def format_report_html(report, model, test_suite): 10 def format_report_html(report, model, test_suite):
10 messages = [] 11 messages = []
11 for problem in report['problems']: 12 for problem in report['problems']:
12 ldraw_code = model.body[problem.body_index].textual_representation() 13 ldraw_code = model.body[problem.body_index].textual_representation()
23 return '\n'.join(messages) 24 return '\n'.join(messages)
24 25
25 @app.route('/', methods = ['GET', 'POST']) 26 @app.route('/', methods = ['GET', 'POST'])
26 def web_front(): 27 def web_front():
27 test_suite = load_tests() 28 test_suite = load_tests()
29 common_args = {
30 'appname': appname,
31 'version': version_string,
32 'is_debug': max(version) == 9999,
33 }
28 if request.method == 'POST': 34 if request.method == 'POST':
29 # check if the post request has the file part 35 # check if the post request has the file part
30 if 'file' not in request.files or not request.files['file'].filename: 36 if 'file' not in request.files or not request.files['file'].filename:
31 return redirect(request.url) 37 return redirect(request.url)
32 file = request.files['file'] 38 file = request.files['file']
37 load_colours(ldconfig_ldr) 43 load_colours(ldconfig_ldr)
38 model = read_ldraw( 44 model = read_ldraw(
39 file.stream, 45 file.stream,
40 name = filename, 46 name = filename,
41 ldraw_directories = config['libraries'], 47 ldraw_directories = config['libraries'],
48 **common_args,
42 ) 49 )
43 report = check_model(model, test_suite) 50 report = check_model(model, test_suite)
44 51
45 # Amend human-readable messages into the report 52 # Amend human-readable messages into the report
46 for problem in report['problems']: 53 for problem in report['problems']:
48 problem.message_str = problem_text(problem, test_suite) 55 problem.message_str = problem_text(problem, test_suite)
49 problem.ldraw_code = object.textual_representation() 56 problem.ldraw_code = object.textual_representation()
50 return render_template('webfront.html', 57 return render_template('webfront.html',
51 report = report, 58 report = report,
52 name = filename, 59 name = filename,
53 problem_types = all_problem_types(test_suite) 60 problem_types = all_problem_types(test_suite),
61 **common_args,
54 ) 62 )
55 else: 63 else:
56 test_suite = load_tests() 64 test_suite = load_tests()
57 return render_template('webfront.html', 65 return render_template('webfront.html',
58 report = None, 66 report = None,
59 name = None, 67 name = None,
60 problem_types = all_problem_types(test_suite) 68 problem_types = all_problem_types(test_suite),
69 **common_args,
61 ) 70 )
62 71
63 @app.route('/static/<path:path>') 72 @app.route('/static/<path:path>')
64 def static_file(path): 73 def static_file(path):
65 from flask import send_from_directory 74 from flask import send_from_directory

mercurial