# HG changeset patch # User Ludovic Chabant # Date 1507403506 25200 # Node ID e1cadbfddb48d42e1940890e217c0c81f417188a # Parent 84d8fadf9e67c86b497e6bb9c9368c06733b1a12 admin: Move 404 debugging into a separate function. diff -r 84d8fadf9e67 -r e1cadbfddb48 piecrust/admin/web.py --- a/piecrust/admin/web.py Sat Oct 07 12:11:13 2017 -0700 +++ b/piecrust/admin/web.py Sat Oct 07 12:11:46 2017 -0700 @@ -34,32 +34,35 @@ # Register extensions and blueprints. app.register_blueprint(foodtruck_bp, url_prefix=url_prefix) - # --------------- # Debugging stuff - @app.errorhandler(404) - def page_not_found(e): - from flask import request, url_for - output = [] - for rule in app.url_map.iter_rules(): - options = {} - for arg in rule.arguments: - options[arg] = "[{0}]".format(arg) - methods = ','.join(rule.methods) - try: - url = url_for(rule.endpoint, **options) - except: - url = '???' - line = ("{:50s} {:20s} {}".format(rule.endpoint, methods, url)) - output.append(line) - - resp = request.path + '
\n' - resp += str(request.environ) + '
\n' - resp += str(app.config['FOODTRUCK_ROOT_URL']) + '
\n' - resp += '
\n'.join(sorted(output)) - return resp, 404 - # --------------- + if app.config.get('FOODTRUCK_DEBUG_404'): + @app.errorhandler(404) + def page_not_found(e): + return _debug_page_not_found(e) logger.debug("Created FoodTruck app with admin root: %s" % root_dir) return app + +def _debug_page_not_found(e): + from flask import request, url_for + output = [] + for rule in app.url_map.iter_rules(): + options = {} + for arg in rule.arguments: + options[arg] = "[{0}]".format(arg) + methods = ','.join(rule.methods) + try: + url = url_for(rule.endpoint, **options) + except: + url = '???' + line = ("{:50s} {:20s} {}".format(rule.endpoint, methods, url)) + output.append(line) + + resp = 'FOODTRUCK_ROOT_URL=%s
\n' % str(app.config['FOODTRUCK_ROOT_URL']) + resp += 'PATH=%s
\n' % request.path + resp += 'ENVIRON=%s
\n' % str(request.environ) + resp += 'URL RULES:
\n' + resp += '
\n'.join(sorted(output)) + return resp, 404