Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding support for processing the new StreamingHttpResponse from Djan…

…go 1.5.
  • Loading branch information...
commit a3d3791fb11c55efe797cb3cf3e9d404c8652f49 1 parent 6f79f0b
Lucas Inojosa authored

Showing 1 changed file with 21 additions and 7 deletions. Show diff stats Hide diff stats

  1. +21 7 debug_toolbar/middleware.py
28 debug_toolbar/middleware.py
@@ -5,7 +5,7 @@
5 5 import thread
6 6
7 7 from django.conf import settings
8   -from django.http import HttpResponseRedirect
  8 +from django.http import HttpResponseRedirect, StreamingHttpResponse
9 9 from django.shortcuts import render_to_response
10 10 from django.utils.encoding import smart_unicode
11 11 from django.utils.importlib import import_module
@@ -112,6 +112,15 @@ def process_view(self, request, view_func, view_args, view_kwargs):
112 112 result = response
113 113 return result
114 114
  115 + def _wrap_streaming_content(self, rendered_toolbar, streaming_content):
  116 + """
  117 + Wrap the stream with a new generator that looks for the replacement
  118 + tag. Used when the response is a generator for StreamingHttpResponse.
  119 + """
  120 + toolbar_and_tag = smart_unicode(rendered_toolbar + self.tag)
  121 + for chunk in streaming_content:
  122 + yield replace_insensitive(smart_unicode(chunk), self.tag, toolbar_and_tag)
  123 +
115 124 def process_response(self, request, response):
116 125 __traceback_hide__ = True
117 126 ident = thread.get_ident()
@@ -133,11 +142,16 @@ def process_response(self, request, response):
133 142 response.get('Content-Type', '').split(';')[0] in _HTML_TYPES):
134 143 for panel in toolbar.panels:
135 144 panel.process_response(request, response)
136   - response.content = replace_insensitive(
137   - smart_unicode(response.content),
138   - self.tag,
139   - smart_unicode(toolbar.render_toolbar() + self.tag))
140   - if response.get('Content-Length', None):
141   - response['Content-Length'] = len(response.content)
  145 + rendered_toolbar = toolbar.render_toolbar()
  146 + if response.streaming:
  147 + response.streaming_content = self._wrap_streaming_content(
  148 + rendered_toolbar, response.streaming_content)
  149 + else:
  150 + response.content = replace_insensitive(
  151 + smart_unicode(response.content),
  152 + self.tag,
  153 + smart_unicode(rendered_toolbar + self.tag))
  154 + if response.get('Content-Length', None):
  155 + response['Content-Length'] = len(response.content)
142 156 del self.__class__.debug_toolbars[ident]
143 157 return response

0 comments on commit a3d3791

Please sign in to comment.
Something went wrong with that request. Please try again.