Created
March 14, 2011 13:38
-
-
Save sergray/869121 to your computer and use it in GitHub Desktop.
Tracing SQL queries of Django Command.handle with trace_sql decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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