Skip to content

Instantly share code, notes, and snippets.

@sergray
Created March 14, 2011 13:38
Show Gist options
  • Save sergray/869121 to your computer and use it in GitHub Desktop.
Save sergray/869121 to your computer and use it in GitHub Desktop.
Tracing SQL queries of Django Command.handle with trace_sql decorator
class SQLDebugCommand(object):
def __init__(self):
from devserver.modules.sql import SQLSummaryModule, SQLRealTimeModule
from devserver.logger import GenericLogger
from mock import Mock
self.modules = [
#SQLRealTimeModule(GenericLogger(SQLRealTimeModule)),
SQLSummaryModule(GenericLogger(SQLSummaryModule)),
]
self.mock_request = Mock()
def before_handle(self):
for mod in self.modules:
if hasattr(mod, 'process_init'):
mod.process_init(self.mock_request)
def after_handle(self):
for mod in self.modules:
if hasattr(mod, 'process_complete'):
mod.process_complete(self.mock_request)
def trace_sql(func):
from django.conf import settings
if not settings.DEBUG:
return func
debugger = SQLDebugCommand()
def wrapper(*args, **kwargs):
debugger.before_handle()
ret = func(*args, **kwargs)
debugger.after_handle()
return ret
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment