comparison piecrust/admin/web.py @ 958:e1cadbfddb48

admin: Move 404 debugging into a separate function.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 07 Oct 2017 12:11:46 -0700
parents 7ecb946bfafd
children 45ad976712ec
comparison
equal deleted inserted replaced
957:84d8fadf9e67 958:e1cadbfddb48
32 app.config['SECRET_KEY'] = 'temp-key' 32 app.config['SECRET_KEY'] = 'temp-key'
33 33
34 # Register extensions and blueprints. 34 # Register extensions and blueprints.
35 app.register_blueprint(foodtruck_bp, url_prefix=url_prefix) 35 app.register_blueprint(foodtruck_bp, url_prefix=url_prefix)
36 36
37 # ---------------
38 # Debugging stuff 37 # Debugging stuff
39 @app.errorhandler(404) 38 if app.config.get('FOODTRUCK_DEBUG_404'):
40 def page_not_found(e): 39 @app.errorhandler(404)
41 from flask import request, url_for 40 def page_not_found(e):
42 output = [] 41 return _debug_page_not_found(e)
43 for rule in app.url_map.iter_rules():
44 options = {}
45 for arg in rule.arguments:
46 options[arg] = "[{0}]".format(arg)
47 methods = ','.join(rule.methods)
48 try:
49 url = url_for(rule.endpoint, **options)
50 except:
51 url = '???'
52 line = ("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
53 output.append(line)
54
55 resp = request.path + '<br/>\n'
56 resp += str(request.environ) + '<br/>\n'
57 resp += str(app.config['FOODTRUCK_ROOT_URL']) + '<br/>\n'
58 resp += '<br/>\n'.join(sorted(output))
59 return resp, 404
60 # ---------------
61 42
62 logger.debug("Created FoodTruck app with admin root: %s" % root_dir) 43 logger.debug("Created FoodTruck app with admin root: %s" % root_dir)
63 44
64 return app 45 return app
65 46
47
48 def _debug_page_not_found(e):
49 from flask import request, url_for
50 output = []
51 for rule in app.url_map.iter_rules():
52 options = {}
53 for arg in rule.arguments:
54 options[arg] = "[{0}]".format(arg)
55 methods = ','.join(rule.methods)
56 try:
57 url = url_for(rule.endpoint, **options)
58 except:
59 url = '???'
60 line = ("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
61 output.append(line)
62
63 resp = 'FOODTRUCK_ROOT_URL=%s<br/>\n' % str(app.config['FOODTRUCK_ROOT_URL'])
64 resp += 'PATH=%s<br/>\n' % request.path
65 resp += 'ENVIRON=%s<br/>\n' % str(request.environ)
66 resp += 'URL RULES:<br/>\n'
67 resp += '<br/>\n'.join(sorted(output))
68 return resp, 404