'Convenient function for handling the http response of a csv.
data should be iterable and is used to stream object over http'

def return_csv(self, filename, header, data):

    csv_file = StringIO.StringIO()
    writer = csv.writer(csv_file, dialect='excel', quotechar='"', quoting=csv.QUOTE_ALL)
    writer.writerow(header)
    def read_and_flush():
        'Read    and    clear    buffer    for    optimization'
        csv_file.seek(0)
        csv_data = csv_file.read()
        csv_file.seek(0)
        csv_file.truncate()
        return csv_data
    def csv_data():
        'Generator    for    handling    potentially    large    CSVs'
        for row in data:
            writer.writerow(row)
        csv_data = read_and_flush()
        (yield csv_data)
    response = HttpResponse(csv_data(), content_type='text/csv')
    response['Content-Disposition'] = 'attachment;    filename={0}'.format(filename)
    return response
