Login

Middelware to remember sql query log from before a redirect

Author:
miracle2k
Posted:
September 17, 2007
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

Simple middelware that listens for redirect responses, store the request's query log in the session when it finds one, and starts the next request's log off with those queries from before the redirect.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.http import HttpResponseRedirect
from django.core.exceptions import MiddlewareNotUsed

class SqlDebuggingRedirWrapMiddleware(object):
    def __init__(self):
        if not (getattr(settings, 'DEBUG', False)):
            raise django.exceptions.MiddlewareNotUsed()
        
    def process_response(self, request, response):
        if isinstance(response, HttpResponseRedirect):
            request.session['_redir_queries'] = connection.queries
        return response
    
    def process_request(self, request):        
        if '_redir_queries' in request.session:
            connection.queries[0:0] = request.session['_redir_queries']
            del request.session['_redir_queries']

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.