mirror of
https://github.com/fergalmoran/dss.git
synced 2026-02-26 18:06:39 +00:00
Added sql printing middleware
This commit is contained in:
@@ -131,6 +131,7 @@ MIDDLEWARE_CLASSES = (
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'django.middleware.gzip.GZipMiddleware',
|
||||
'spa.middleware.uploadify.SWFUploadMiddleware',
|
||||
'spa.middleware.sqlprinter.SqlPrintingMiddleware',
|
||||
#'debug_toolbar.middleware.DebugToolbarMiddleware',
|
||||
)
|
||||
|
||||
|
||||
53
spa/middleware/sqlprinter.py
Normal file
53
spa/middleware/sqlprinter.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import os
|
||||
|
||||
from django.db import connection
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def terminal_width():
|
||||
"""
|
||||
Function to compute the terminal width.
|
||||
WARNING: This is not my code, but I've been using it forever and
|
||||
I don't remember where it came from.
|
||||
"""
|
||||
width = 0
|
||||
try:
|
||||
import struct, fcntl, termios
|
||||
|
||||
s = struct.pack('HHHH', 0, 0, 0, 0)
|
||||
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
|
||||
width = struct.unpack('HHHH', x)[1]
|
||||
except:
|
||||
pass
|
||||
if width <= 0:
|
||||
try:
|
||||
width = int(os.environ['COLUMNS'])
|
||||
except:
|
||||
pass
|
||||
if width <= 0:
|
||||
width = 80
|
||||
return width
|
||||
|
||||
|
||||
class SqlPrintingMiddleware(object):
|
||||
"""
|
||||
Middleware which prints out a list of all SQL queries done
|
||||
for each view that is processed. This is only useful for debugging.
|
||||
"""
|
||||
|
||||
def process_response(self, request, response):
|
||||
indentation = 2
|
||||
if len(connection.queries) > 0 and settings.DEBUG:
|
||||
width = terminal_width()
|
||||
total_time = 0.0
|
||||
for query in connection.queries:
|
||||
nice_sql = query['sql'].replace('"', '').replace(',', ', ')
|
||||
sql = "\033[1;31m[%s]\033[0m %s" % (query['time'], nice_sql)
|
||||
total_time = total_time + float(query['time'])
|
||||
while len(sql) > width - indentation:
|
||||
print "%s%s" % (" " * indentation, sql[:width - indentation])
|
||||
sql = sql[width - indentation:]
|
||||
print "%s%s\n" % (" " * indentation, sql)
|
||||
replace_tuple = (" " * indentation, str(total_time))
|
||||
print "%s\033[1;32m[TOTAL TIME: %s seconds]\033[0m" % replace_tuple
|
||||
return response
|
||||
@@ -21,7 +21,7 @@ class _Activity(_BaseModel):
|
||||
app_label = 'spa'
|
||||
|
||||
def __unicode__(self):
|
||||
return "%s" % (self.date)
|
||||
return "%s" % self.date
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_verb_passed(self):
|
||||
|
||||
Reference in New Issue
Block a user