示例#1
0
    def process_response(self, request, response):
        """
        Store the DebugToolbarMiddleware rendered toolbar into a cache store.

        The data stored in the cache are then reachable from an URL that is appened
        to the HTTP response header under the 'X-debug-data-url' key.
        """
        toolbar = self.__class__.debug_toolbars.get(
            threading.current_thread().ident, None)

        response = super(DebugPanelMiddleware,
                         self).process_response(request, response)

        if toolbar:
            # for django-debug-toolbar >= 1.4
            for panel in reversed(toolbar.enabled_panels):
                if hasattr(panel, 'generate_stats'):
                    panel.generate_stats(request, response)

            cache_key = "%f" % time.time()
            cache.set(cache_key, toolbar.render_toolbar())

            response['X-debug-data-url'] = request.build_absolute_uri(
                reverse('debug_data',
                        urlconf=debug_panel.urls,
                        kwargs={'cache_key': cache_key}))

        return response
    def process_response(self, request, response):
        # only valid for superuser
        if not hasattr(request, "user") or not request.user.is_superuser:
            response.set_cookie('djdt', 'hide', 864000)
            return response

        toolbar = self.__class__.debug_toolbars.pop(
            threading.current_thread().ident, None)
        if not toolbar:
            return response

        # Run process_response methods of panels like Django middleware.
        for panel in reversed(toolbar.enabled_panels):
            new_response = panel.process_response(request, response)
            if new_response:
                response = new_response

        # Deactivate instrumentation ie. monkey-unpatch. This must run
        # regardless of the response. Keep 'return' clauses below.
        # (NB: Django's model for middleware doesn't guarantee anything.)
        for panel in reversed(toolbar.enabled_panels):
            panel.disable_instrumentation()

        # Collapse the toolbar by default if SHOW_COLLAPSED is set.
        if toolbar.config['SHOW_COLLAPSED'] and 'djdt' not in request.COOKIES:
            response.set_cookie('djdt', 'hide', 864000)

        # When the toolbar will be inserted for sure, generate the stats.
        for panel in reversed(toolbar.enabled_panels):
            panel.generate_stats(request, response)

        cache_key = "%f" % time.time()
        cache.set(cache_key, toolbar.render_toolbar())

        response['X-debug-data-url'] = request.build_absolute_uri(
            reverse('debug_data',
                    urlconf=debug_panel.urls,
                    kwargs={'cache_key': cache_key}))

        logger.info("django-debug-toolbar: \nrequest_url: {request_url}\n"
                    "debug_url: {debug_url}".format(
                        request_url=request.path,
                        debug_url=response['X-debug-data-url']))

        return response
示例#3
0
    def process_response(self, request, response):
        """
        Store the DebugToolbarMiddleware rendered toolbar into a cache store.

        The data stored in the cache are then reachable from an URL that is appened
        to the HTTP response header under the 'X-debug-data-url' key.
        """
        toolbar = self.__class__.debug_toolbars.get(threading.current_thread().ident, None)

        response = super(DebugPanelMiddleware, self).process_response(request, response)

        if toolbar:
            cache_key = "%f" % time.time()
            cache.set(cache_key, toolbar.render_toolbar())

            response['X-debug-data-url'] = request.build_absolute_uri(
                reverse('debug_data', urlconf=debug_panel.urls, kwargs={'cache_key': cache_key}))

        return response
示例#4
0
    def process_response(self, request, response):
        """
        Since there is no hook to intercept and change rendering of the default
        debug_toolbar middleware, this is mostly a copy the original debug_toolbar
        middleware.

        Instead of rendering the debug_toolbar inside the response HTML, it's stored
        in the Django cache.

        The data stored in the cache are then reachable from an URL that is appened
        to the HTTP response header under the 'X-debug-data-url' key.
        """
        __traceback_hide__ = True
        ident = threading.current_thread().ident
        toolbar = self.__class__.debug_toolbars.get(ident)
        if not toolbar:
            return response
        if isinstance(response, HttpResponseRedirect):
            if not toolbar.config['INTERCEPT_REDIRECTS']:
                return response
            redirect_to = response.get('Location', None)
            if redirect_to:
                cookies = response.cookies
                response = render(
                    request,
                    'debug_toolbar/redirect.html',
                    {'redirect_to': redirect_to}
                )
                response.cookies = cookies

        for panel in toolbar.panels:
            panel.process_response(request, response)

        cache_key = "%f" % time.time()
        cache.set(cache_key, toolbar.render_toolbar())

        response['X-debug-data-url'] = request.build_absolute_uri(
            reverse('debug_data', urlconf=debug_panel.urls, kwargs={'cache_key': cache_key}))

        del self.__class__.debug_toolbars[ident]
        return response