""" Debug Toolbar middleware """ import os from django.conf import settings from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from django.utils.encoding import smart_unicode from django.conf.urls.defaults import include, patterns import debug_toolbar.urls from debug_toolbar.toolbar.loader import DebugToolbar _HTML_TYPES = ('text/html', 'application/xhtml+xml') def replace_insensitive(string, target, replacement): """ Similar to string.replace() but is case insensitive Code borrowed from: http://forums.devshed.com/python-programming-11/case-insensitive-string-replace-490921.html """ no_case = string.lower() index = no_case.rfind(target.lower()) if index >= 0: return string[:index] + replacement + string[index + len(target):] else: # no results so return the original string return string class DebugToolbarMiddleware(object): """ Middleware to set up Debug Toolbar on incoming request and render toolbar on outgoing response. """ def __init__(self): self.debug_toolbar = None self.original_urlconf = settings.ROOT_URLCONF self.original_pattern = patterns('', ('', include(self.original_urlconf)),) self.override_url = True # Set method to use to decide to show toolbar self.show_toolbar = self._show_toolbar # default if hasattr(settings, 'DEBUG_TOOLBAR_CONFIG'): show_toolbar_callback = settings.DEBUG_TOOLBAR_CONFIG.get( 'SHOW_TOOLBAR_CALLBACK', None) if show_toolbar_callback: self.show_toolbar = show_toolbar_callback def _show_toolbar(self, request): if not settings.DEBUG: return False if request.is_ajax() and not \ request.path.startswith(os.path.join('/', debug_toolbar.urls._PREFIX)): # Allow ajax requests from the debug toolbar return False if not request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: return False return True def process_request(self, request): if self.show_toolbar(request): if self.override_url: debug_toolbar.urls.urlpatterns += self.original_pattern self.override_url = False request.urlconf = 'debug_toolbar.urls' self.debug_toolbar = DebugToolbar(request) for panel in self.debug_toolbar.panels: panel.process_request(request) return None def process_view(self, request, view_func, view_args, view_kwargs): if self.debug_toolbar: for panel in self.debug_toolbar.panels: panel.process_view(request, view_func, view_args, view_kwargs) def process_response(self, request, response): if not self.debug_toolbar: return response if self.debug_toolbar.config['INTERCEPT_REDIRECTS']: if isinstance(response, HttpResponseRedirect): redirect_to = response.get('Location', None) if redirect_to: response = render_to_response( 'debug_toolbar/redirect.html', {'redirect_to': redirect_to} ) if response.status_code != 200: return response for panel in self.debug_toolbar.panels: panel.process_response(request, response) if response['Content-Type'].split(';')[0] in _HTML_TYPES: response.content = replace_insensitive(smart_unicode(response.content), u'', smart_unicode(self.debug_toolbar.render_toolbar() + u'')) return response 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138