Incrementally Return CSV

 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. UnicodeWriter and UnicodeDictWriter - write unicode strings out to Excel compatible CSV files by simon 5 years, 8 months ago
  2. Django Incremental Counter Tag by gwrtheyrn 2 years, 4 months ago
  3. Prefetch id for Postgresql backend by bretth 2 years ago
  4. CSV to JSON Fixture by briangershon 4 years, 8 months ago
  5. Generic CSV export admin action factory with relationship spanning fields and labels, and many to many fields by selimnairb 2 months, 2 weeks 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.

#

(Forgotten your password?)