X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/85b57aa2d25552d82260ce40fd9b1063dd7c3dd5..bb68c8efe90a6f818c2c26f36ef0e02c91ed6158:/apps/debug_toolbar/middleware.py?ds=sidebyside diff --git a/apps/debug_toolbar/middleware.py b/apps/debug_toolbar/middleware.py new file mode 100644 index 000000000..2ef7f1b72 --- /dev/null +++ b/apps/debug_toolbar/middleware.py @@ -0,0 +1,61 @@ +""" +Debug Toolbar middleware +""" +import re +from django.conf import settings +from django.utils.encoding import smart_str +from debug_toolbar.toolbar.loader import DebugToolbar + +_HTML_TYPES = ('text/html', 'application/xhtml+xml') +_END_HEAD_RE = re.compile(r'', re.IGNORECASE) +_END_BODY_RE = re.compile(r'', re.IGNORECASE) + +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 + + def show_toolbar(self, request): + if not settings.DEBUG: + 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): + self.debug_toolbar = DebugToolbar(request) + self.debug_toolbar.load_panels() + debug = request.GET.get('djDebug') + # kinda ugly, needs changes to the loader to optimize + for panel in self.debug_toolbar.panels: + response = panel.process_request(request) + if not response: + if debug == panel.name: + response = panel.process_ajax(request) + if response: + response.skip_debug_response = True + return response + + def process_view(self, request, callback, callback_args, callback_kwargs): + if self.show_toolbar(request): + for panel in self.debug_toolbar.panels: + cb = panel.process_view(request, callback, callback_args, callback_kwargs) + if cb: + callback = cb + return callback + + def process_response(self, request, response): + if self.show_toolbar(request) and not getattr(response, 'skip_debug_response', False): + if response['Content-Type'].split(';')[0] in _HTML_TYPES and not request.is_ajax(): + # Saving this here in case we ever need to inject into + #response.content = _END_HEAD_RE.sub(smart_str(self.debug_toolbar.render_styles() + "%s" % match.group()), response.content) + for panel in self.debug_toolbar.panels: + nr = panel.process_response(request, response) + # Incase someone forgets `return response` + if nr: response = nr + response.content = _END_BODY_RE.sub(smart_str('' + self.debug_toolbar.render_toolbar()), response.content) + return response