Login

Incrementally Return CSV

Author:
davidblewett
Posted:
August 31, 2007
Language:
Python
Version:
.96
Score:
5 (after 5 ratings)

The first function (ftype_batch) is a view that passes the first part of the CSV filename and a queryset intended to write out to the file.

run_batch prepares the HttpResponse for incrementally writing, and write_batch actually writes out the data.

The logic in write_batch is custom to what I need done, but as long as the csv writer receives a sequence to write, it should work.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import csv
import datetime
from cStringIO import StringIO
from django.http import HttpResponse
from signet.canvas.models import Submission

@login_required
def ftype_batch(request, ftype):
    cv_id = FTYPE_SETS[ftype]['cv'].id
    return run_batch(fname_pre=ftype,
                     qs=Submission.objects.filter(responder__id=1,
                                                  chart__id=cv_id,
                                                  submit_date__isnull=True))

def run_batch(fname_pre, qs):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(mimetype='text/csv')
    sd = datetime.datetime.now()
    fname = '%s-%s.csv' % (fname_pre, sd.strftime('%Y%m%d-%H%M-%s'))
    response['Content-Disposition'] = 'attachment; filename=%s' % fname
    response._container = write_batch(qs, sd)
    response._is_string = False

    return response

def write_batch(qs, d):
    i = 0
    sf = StringIO()
    writer = csv.writer(sf, quoting=csv.QUOTE_NONE)
    for s in qs:
        print 'Row #%d' % (i + 1,)
        #Clear contents so we don't return duplicate rows (and conserve memory)
        sf.truncate(0)
        s_responses = s.responses()
        srk = sorted(s_responses.keys())
        row = [repr(s_responses[k]).strip() for k in srk]

        writer.writerow(row)
        s.submit_date = d
        s.save()
        i += 1
        yield sf.getvalue()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

pigletto (on August 31, 2007):

I'll soon have to deal with csv so this is something for me :)

Would be nice if you can include necessary imports at the begining of the code.

#

bikeshedder (on November 15, 2007):

Instead of manipulating the private _container and _is_string attributes of the HttpResponse it would be better to pass the generator as content argument to the HttpResponse constructor.

response = HttpResponse(content=write_batch(qs, sd), mimetype='text/csv')

#

davidblewett (on June 10, 2008):

It's been awhile since I did this one. If I recall correctly, the reason why I did it the way I did was that I couldn't find a way to specify the filename for the attachment by specifying all the arguments in the response constructor.

#

Please login first before commenting.