mirror of
https://github.com/pallets-eco/flask-debugtoolbar.git
synced 2025-12-30 18:19:31 -06:00
Implement the ability to intercept redirects (like in the django debug toolbar)
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
@@ -4,5 +4,6 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Flask-debug-toolbar</h1>
|
||||
<a href="{{ url_for('redirect_example') }}">Redirect example</a>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -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):
|
||||
'</body>',
|
||||
toolbar_html + '</body>')]
|
||||
|
||||
def render(self, template_name, context):
|
||||
template = self.jinja_env.get_template(template_name)
|
||||
return template.render(**context)
|
||||
|
||||
15
flaskext/debugtoolbar/templates/redirect.html
Normal file
15
flaskext/debugtoolbar/templates/redirect.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Redirect intercepted</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Redirect ({{ redirect_code }})</h1>
|
||||
<p>Location: <a href="{{ redirect_to }}">{{ redirect_to }}</a></p>
|
||||
<p class="notice">
|
||||
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 <code>DEBUG_TB_INTERCEPT_REDIRECTS</code> to <code>False</code>.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -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='')
|
||||
|
||||
Reference in New Issue
Block a user