diff --git a/example/example.py b/example/example.py index 270ca2e..e049a74 100644 --- a/example/example.py +++ b/example/example.py @@ -1,13 +1,14 @@ import sys sys.path.insert(0, '.') -from flask import Flask, render_template +from flask import Flask, render_template, redirect, url_for from flaskext.script import Manager from flaskext.sqlalchemy import SQLAlchemy from flaskext.debugtoolbar import DebugToolbarExtension app = Flask(__name__) +app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = SQLAlchemy(app) @@ -23,7 +24,16 @@ def index(): ExampleModel.query.get(1) return render_template('index.html') +@app.route('/redirect') +def redirect_example(): + + response = redirect(url_for('index')) + response.set_cookie('test_cookie', '1') + return response + if __name__ == "__main__": + db.create_all() + manager = Manager(app) manager.run() diff --git a/example/templates/index.html b/example/templates/index.html index cb9f722..e091e1a 100644 --- a/example/templates/index.html +++ b/example/templates/index.html @@ -4,5 +4,6 @@

Flask-debug-toolbar

+ Redirect example diff --git a/flaskext/debugtoolbar/__init__.py b/flaskext/debugtoolbar/__init__.py index 29bc52e..8cd6b29 100644 --- a/flaskext/debugtoolbar/__init__.py +++ b/flaskext/debugtoolbar/__init__.py @@ -25,6 +25,8 @@ class DebugToolbarExtension(object): _static_dir = os.path.realpath( os.path.join(os.path.dirname(__file__), 'static')) + _redirect_codes = [301, 302, 303, 304] + def __init__(self, app): self.app = app self.debug_toolbars = {} @@ -60,6 +62,18 @@ class DebugToolbarExtension(object): if request not in self.debug_toolbars: return response + if self.debug_toolbars[request].config['DEBUG_TB_INTERCEPT_REDIRECTS']: + if response.status_code in self._redirect_codes: + redirect_to = response.location + redirect_code = response.status_code + if redirect_to: + response.location = None + response.status_code = 200 + response.response = [ + self.render('redirect.html', { + 'redirect_to': redirect_to, + 'redirect_code': redirect_code})] + if response.status_code == 200: for panel in self.debug_toolbars[request].panels: panel.process_response(request, response) @@ -73,3 +87,6 @@ class DebugToolbarExtension(object): '', toolbar_html + '')] + def render(self, template_name, context): + template = self.jinja_env.get_template(template_name) + return template.render(**context) diff --git a/flaskext/debugtoolbar/templates/redirect.html b/flaskext/debugtoolbar/templates/redirect.html new file mode 100644 index 0000000..6f49fe5 --- /dev/null +++ b/flaskext/debugtoolbar/templates/redirect.html @@ -0,0 +1,15 @@ + + + Redirect intercepted + + +

Redirect ({{ redirect_code }})

+

Location: {{ redirect_to }}

+

+ The Flask Debug Toolbar has intercepted a redirect to the above URL for + debug viewing purposes. You can click the above link to continue with the + redirect as normal. If you'd like to disable this feature, you can set the + config variable DEBUG_TB_INTERCEPT_REDIRECTS to False. +

+ + diff --git a/flaskext/debugtoolbar/toolbar.py b/flaskext/debugtoolbar/toolbar.py index 444301f..2ff556f 100644 --- a/flaskext/debugtoolbar/toolbar.py +++ b/flaskext/debugtoolbar/toolbar.py @@ -1,10 +1,16 @@ -from flask import url_for +from flask import url_for, current_app class DebugToolbar(object): def __init__(self, request, jinja_env): self.jinja_env = jinja_env self.request = request self.panels = [] + + # default config settings + self.config = { + 'DEBUG_TB_INTERCEPT_REDIRECTS': True, + } + self.config.update(current_app.config) self.template_context = { 'static_path': url_for('_debug_toolbar.static', filename='')