change the way we handle last chunk, on't break but instead test if last

was an empty string. So we allows idiot content to send empty string at
first.
This commit is contained in:
benoitc 2010-03-19 10:00:15 +01:00
parent 771d44902a
commit 514a0ba0e3
5 changed files with 29 additions and 8 deletions

View File

@ -1,5 +1,7 @@
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('',
url(r'^acsv$', 'testing.views.acsv'),
url(r'^$', 'testing.views.home'),
)
)

View File

@ -1,7 +1,9 @@
# Create your views here.
import csv
import os
from django import forms
from django.http import HttpResponse
from django.shortcuts import render_to_response
import tempfile
@ -40,4 +42,19 @@ def home(request):
})
def acsv(request):
rows = [
{'a': 1, 'b': 2},
{'a': 3, 'b': 3}
]
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=report.csv'
writer = csv.writer(response)
writer.writerow(['a', 'b'])
for r in rows:
writer.writerow([r['a'], r['b']])
return response

View File

@ -2,5 +2,5 @@
from django.conf.urls.defaults import patterns,include
urlpatterns = patterns('',
('^$', include("testing.urls")),
)
(r'^', include("testing.urls")),
)

View File

@ -170,5 +170,6 @@ class Request(object):
self.response_chunked = True
if not isinstance(value, basestring):
value = str(value)
self.response_headers.append((name.title(), value.strip()))
self.response_headers.append((name.title(), value.strip()))
self.start_response_called = True

View File

@ -30,11 +30,12 @@ class Response(object):
resp_head.extend(["%s: %s\r\n" % (n, v) for n, v in self.headers])
write(self._sock, "%s\r\n" % "".join(resp_head))
for chunk in list(self.data):
if chunk == "": break
last_chunk = None
for chunk in self.data:
last_chunk = chunk
write(self._sock, chunk, self.chunked)
if self.chunked:
if self.chunked and last_chunk != "":
# send last chunk
write_chunk(self._sock, "")