mirror of
https://github.com/pallets-eco/flask-debugtoolbar.git
synced 2026-01-05 13:09:45 -06:00
Merge pull request #8 from sbook/nosqlalchemy
Remove SQLAlchemy as a dependency of flask-debugtoolbar.
This commit is contained in:
@@ -8,7 +8,10 @@ from werkzeug.exceptions import HTTPException
|
||||
from werkzeug.urls import url_quote_plus
|
||||
|
||||
from flaskext.debugtoolbar.toolbar import DebugToolbar
|
||||
from flaskext.debugtoolbar.views import module
|
||||
from flask import Module
|
||||
|
||||
|
||||
module = Module(__name__)
|
||||
|
||||
def replace_insensitive(string, target, replacement):
|
||||
"""Similar to string.replace() but is case insensitive
|
||||
@@ -55,7 +58,6 @@ class DebugToolbarExtension(object):
|
||||
loader=PackageLoader(__name__, 'templates'))
|
||||
self.jinja_env.filters['urlencode'] = url_quote_plus
|
||||
|
||||
app.register_module(module, url_prefix='/_debug_toolbar/views')
|
||||
app.add_url_rule('/_debug_toolbar/static/<path:filename>',
|
||||
'_debug_toolbar.static', self.send_static_file)
|
||||
|
||||
@@ -70,6 +72,10 @@ class DebugToolbarExtension(object):
|
||||
"""
|
||||
req = _request_ctx_stack.top.request
|
||||
app = current_app
|
||||
|
||||
if 'debugtoolbar' not in app.modules:
|
||||
app.register_module(module, url_prefix='/_debug_toolbar/views')
|
||||
|
||||
try:
|
||||
if req.routing_exception is not None:
|
||||
raise req.routing_exception
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
import hashlib
|
||||
|
||||
import simplejson
|
||||
|
||||
try:
|
||||
from flaskext.sqlalchemy import get_debug_queries
|
||||
except ImportError:
|
||||
get_debug_queries = None
|
||||
|
||||
|
||||
from flask import current_app, json
|
||||
from flask import request, current_app, abort, json
|
||||
from flaskext.debugtoolbar.panels import DebugPanel
|
||||
from flaskext.debugtoolbar.utils import format_fname, format_sql
|
||||
from flaskext.sqlalchemy import SQLAlchemy
|
||||
|
||||
from flaskext.debugtoolbar import module
|
||||
|
||||
|
||||
_ = lambda x: x
|
||||
|
||||
@@ -71,4 +77,62 @@ class SQLAlchemyDebugPanel(DebugPanel):
|
||||
})
|
||||
return self.render('panels/sqlalchemy.html', { 'queries': data})
|
||||
|
||||
# Panel views
|
||||
|
||||
@module.route('/sqlalchemy/sql_select', methods=['GET', 'POST'])
|
||||
def sql_select(render):
|
||||
statement = request.args['sql']
|
||||
params = request.args['params']
|
||||
|
||||
# Validate hash
|
||||
hash = hashlib.sha1(
|
||||
current_app.config['SECRET_KEY'] + statement + params).hexdigest()
|
||||
if hash != request.args['hash']:
|
||||
return abort(406)
|
||||
|
||||
# Make sure it is a select statement
|
||||
if not statement.lower().strip().startswith('select'):
|
||||
return abort(406)
|
||||
|
||||
params = simplejson.loads(params)
|
||||
|
||||
db = SQLAlchemy(current_app)
|
||||
|
||||
result = db.engine.execute(statement, params)
|
||||
return render('panels/sqlalchemy_select.html', {
|
||||
'result': result.fetchall(),
|
||||
'headers': result.keys(),
|
||||
'sql': format_sql(statement, params),
|
||||
'duration': float(request.args['duration']),
|
||||
})
|
||||
|
||||
@module.route('/sqlalchemy/sql_explain', methods=['GET', 'POST'])
|
||||
def sql_explain(render):
|
||||
statement = request.args['sql']
|
||||
params = request.args['params']
|
||||
|
||||
# Validate hash
|
||||
hash = hashlib.sha1(
|
||||
current_app.config['SECRET_KEY'] + statement + params).hexdigest()
|
||||
if hash != request.args['hash']:
|
||||
return abort(406)
|
||||
|
||||
# Make sure it is a select statement
|
||||
if not statement.lower().strip().startswith('select'):
|
||||
return abort(406)
|
||||
|
||||
params = json.loads(params)
|
||||
|
||||
db = SQLAlchemy(current_app)
|
||||
if db.engine.driver == 'pysqlite':
|
||||
query = 'EXPLAIN QUERY PLAN %s' % statement
|
||||
else:
|
||||
query = 'EXPLAIN %s' % statement
|
||||
|
||||
result = db.engine.execute(query, params)
|
||||
return render('panels/sqlalchemy_explain.html', {
|
||||
'result': result.fetchall(),
|
||||
'headers': result.keys(),
|
||||
'sql': format_sql(statement, params),
|
||||
'duration': float(request.args['duration']),
|
||||
})
|
||||
@@ -14,8 +14,8 @@
|
||||
<td>
|
||||
{% if query.params %}
|
||||
{% if query.is_select %}
|
||||
<a class="remoteCall" href="/_debug_toolbar/views/sql_select?sql={{ query.raw_sql|urlencode }}&params={{ query.params|urlencode }}&duration={{ query.duration|urlencode }}&hash={{ query.hash }}">SELECT</a><br />
|
||||
<a class="remoteCall" href="/_debug_toolbar/views/sql_explain?sql={{ query.raw_sql|urlencode }}&params={{ query.params|urlencode }}&duration={{ query.duration|urlencode }}&hash={{ query.hash }}">EXPLAIN</a><br />
|
||||
<a class="remoteCall" href="/_debug_toolbar/views/sqlalchemy/sql_select?sql={{ query.raw_sql|urlencode }}&params={{ query.params|urlencode }}&duration={{ query.duration|urlencode }}&hash={{ query.hash }}">SELECT</a><br />
|
||||
<a class="remoteCall" href="/_debug_toolbar/views/sqlalchemy/sql_explain?sql={{ query.raw_sql|urlencode }}&params={{ query.params|urlencode }}&duration={{ query.duration|urlencode }}&hash={{ query.hash }}">EXPLAIN</a><br />
|
||||
{% if is_mysql %}
|
||||
<a class="remoteCall" href="/__debug__/sql_profile/?sql={{ query.raw_sql|urlencode }}&params={{ query.params|urlencode }}&duration={{ query.duration|urlencode }}&hash={{ query.hash }}">PROFILE</a><br />
|
||||
{% endif %}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import hashlib
|
||||
|
||||
from flask import Module, request, current_app, abort, json
|
||||
from flaskext.sqlalchemy import SQLAlchemy
|
||||
from flaskext.debugtoolbar.utils import format_sql
|
||||
|
||||
|
||||
module = Module(__name__)
|
||||
|
||||
|
||||
|
||||
@module.route('/sql_select', methods=['GET', 'POST'])
|
||||
def sql_select(render):
|
||||
statement = request.args['sql']
|
||||
params = request.args['params']
|
||||
|
||||
# Validate hash
|
||||
hash = hashlib.sha1(
|
||||
current_app.config['SECRET_KEY'] + statement + params).hexdigest()
|
||||
if hash != request.args['hash']:
|
||||
return abort(406)
|
||||
|
||||
# Make sure it is a select statement
|
||||
if not statement.lower().strip().startswith('select'):
|
||||
return abort(406)
|
||||
|
||||
params = simplejson.loads(params)
|
||||
|
||||
db = SQLAlchemy(current_app)
|
||||
|
||||
result = db.engine.execute(statement, params)
|
||||
return render('panels/sqlalchemy_select.html', {
|
||||
'result': result.fetchall(),
|
||||
'headers': result.keys(),
|
||||
'sql': format_sql(statement, params),
|
||||
'duration': float(request.args['duration']),
|
||||
})
|
||||
|
||||
@module.route('/sql_explain', methods=['GET', 'POST'])
|
||||
def sql_explain(render):
|
||||
statement = request.args['sql']
|
||||
params = request.args['params']
|
||||
|
||||
# Validate hash
|
||||
hash = hashlib.sha1(
|
||||
current_app.config['SECRET_KEY'] + statement + params).hexdigest()
|
||||
if hash != request.args['hash']:
|
||||
return abort(406)
|
||||
|
||||
# Make sure it is a select statement
|
||||
if not statement.lower().strip().startswith('select'):
|
||||
return abort(406)
|
||||
|
||||
params = json.loads(params)
|
||||
|
||||
db = SQLAlchemy(current_app)
|
||||
if db.engine.driver == 'pysqlite':
|
||||
query = 'EXPLAIN QUERY PLAN %s' % statement
|
||||
else:
|
||||
query = 'EXPLAIN %s' % statement
|
||||
|
||||
result = db.engine.execute(query, params)
|
||||
return render('panels/sqlalchemy_explain.html', {
|
||||
'result': result.fetchall(),
|
||||
'headers': result.keys(),
|
||||
'sql': format_sql(statement, params),
|
||||
'duration': float(request.args['duration']),
|
||||
})
|
||||
Reference in New Issue
Block a user