middleware.py 857 Bytes
#-*- coding:utf-8 -*-

from decimal import Decimal
from django.conf import settings

from django.db import connection

from accounts.util import set_history


class MyMiddleware(object):
    def process_request(self, request):
        if request.user.is_authenticated():
            set_history(request.user)

    def process_response(self, request, response):
        if settings.SHOW_SQL_QUERIES:
            if len(connection.queries) > 0:
                print 'Queries for %s:' % request.path_info
                for query in connection.queries:
                    print query['time'], query['sql']
                print 'Total of %s queries for %s.' % (
                    len(connection.queries), request.path)
                print 'Total time: %s' % sum(Decimal(q['time'])
                    for q in connection.queries)
        return response