Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8 from sbook/nosqlalchemy

Remove SQLAlchemy as a dependency of flask-debugtoolbar.
  • Loading branch information...
commit baecf852dda0f5b6c4700fb6deaeda4068b3a19c 2 parents fb11199 + 7dda475
@mvantellingen mvantellingen authored
View
10 flaskext/debugtoolbar/__init__.py
@@ -8,7 +8,10 @@
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 @@ def __init__(self, app):
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 @@ def dispatch_request(self):
"""
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
View
68 flaskext/debugtoolbar/panels/sqlalchemy.py
@@ -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 @@ def content(self):
})
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']),
+ })
View
4 flaskext/debugtoolbar/templates/panels/sqlalchemy.html
@@ -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 }}&amp;params={{ query.params|urlencode }}&amp;duration={{ query.duration|urlencode }}&amp;hash={{ query.hash }}">SELECT</a><br />
- <a class="remoteCall" href="/_debug_toolbar/views/sql_explain?sql={{ query.raw_sql|urlencode }}&amp;params={{ query.params|urlencode }}&amp;duration={{ query.duration|urlencode }}&amp;hash={{ query.hash }}">EXPLAIN</a><br />
+ <a class="remoteCall" href="/_debug_toolbar/views/sqlalchemy/sql_select?sql={{ query.raw_sql|urlencode }}&amp;params={{ query.params|urlencode }}&amp;duration={{ query.duration|urlencode }}&amp;hash={{ query.hash }}">SELECT</a><br />
+ <a class="remoteCall" href="/_debug_toolbar/views/sqlalchemy/sql_explain?sql={{ query.raw_sql|urlencode }}&amp;params={{ query.params|urlencode }}&amp;duration={{ query.duration|urlencode }}&amp;hash={{ query.hash }}">EXPLAIN</a><br />
{% if is_mysql %}
<a class="remoteCall" href="/__debug__/sql_profile/?sql={{ query.raw_sql|urlencode }}&amp;params={{ query.params|urlencode }}&amp;duration={{ query.duration|urlencode }}&amp;hash={{ query.hash }}">PROFILE</a><br />
{% endif %}
View
68 flaskext/debugtoolbar/views.py
@@ -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']),
- })
Please sign in to comment.
Something went wrong with that request. Please try again.