# look at http://code.google.com/p/debugview/ from django.conf import settings # default _DEFAULT_HTML_TYPES = ('text/html','text/plain') _DEFAULT_DEBUG_USER_AGENT = 'DEBUG' _INCLUDE_URL = None _EXCLUDE_URL = None _IP_FILTER = None # try to get the list from settings.py # if want to change the list try: _HTML_TYPES = settings.HTML_TYPES except Exception ,e: _HTML_TYPES = _DEFAULT_HTML_TYPES # get user agent from settings file if does not exists use default try: _DEBUG_USER_AGENT = settings.DEBUG_USER_AGENT except Exception ,e: _DEBUG_USER_AGENT = None #_DEFAULT_DEBUG_USER_AGENT try: _IP_FILTER = settings.DEBUG_IP_LIST except Exception ,e: _IP_FILTER = None def rendering_method(debug_info=None): if debug_info == None: return '' return '' _DEFAULT_RENDERING_METHOD = rendering_method try: _RENDERING_METHOD = settings.RENDERING_METHOD except Exception ,e: _RENDERING_METHOD = _DEFAULT_RENDERING_METHOD ''' Append Debug information logic * check content type (from specified list) * check if user agent containe _DEBUG_USER_AGENT if _DEBUG_USER_AGENT == None do not perform this check * * ''' class DebugView: def process_response(self, request, response): # check client user_agent if response['Content-Type'].split(';')[0] not in _HTML_TYPES: return response agent = client_ip = url = None #print request.META if _DEBUG_USER_AGENT != None: agent = request.META.get('HTTP_USER_AGENT',None) if agent.find(_DEBUG_USER_AGENT) == -1: return response if _IP_FILTER != None: # check client ip client_ip = request.META.get('REMOTE_ADDR',None) if client_ip not in _IP_FILTER: return response print 'client ip was found in the ip filter list' if _INCLUDE_URL != None or _EXCLUDE_URL != None: url = request.get_full_path() # call method that gets url # here we need to check url # check if variables got values if agent == None and client_ip == None and url == None: return response print 'Agent' , agent , 'IP' , client_ip , 'URL' ,url try: debug_info = request.DEBUG except Exception ,e: # debug info not found just return the response return response # run method if defined to create the string that will be added as debug info #response.content = response.content + rendering_method(debug_info) try: response.content = response.content + _RENDERING_METHOD(debug_info) except Exception ,e: print e return response def append_debug_info(request,key,value): debug = None try: debug = request.DEBUG except Exception ,e: # init dic for debug info debug = request.DEBUG = {} debug[key] = value