def stream(queryset_or_adapter, basename=None):
    """Stream a csv file from an object list,

    a queryset or an instanciated adapter.
    """
    if isinstance(queryset_or_adapter, Adapter):
        adapter = queryset_or_adapter
    elif isinstance(queryset_or_adapter, (list, tuple)):
        if not queryset_or_adapter:
            raise ValueError(
                'Type detection is not possible with an empty list')
        cls = _adapters.get(queryset_or_adapter[0].__class__)
        adapter = cls(queryset_or_adapter)
    elif isinstance(queryset_or_adapter, db.BaseQuerySet):
        cls = _adapters.get(queryset_or_adapter._document)
        adapter = cls(queryset_or_adapter)
    else:
        raise ValueError('Unsupported object type')

    timestamp = datetime.now().strftime('%Y-%m-%d-%H-%M')
    headers = {
        b'Content-Disposition': 'attachment; filename={0}-{1}.csv'.format(
            basename or 'export', timestamp),
    }
    streamer = stream_with_context(yield_rows(adapter))
    return Response(streamer, mimetype="text/csv", headers=headers)
def stream ( queryset_or_adapter , basename = None ) : if isinstance ( queryset_or_adapter , Adapter ) : adapter = queryset_or_adapter elif isinstance ( queryset_or_adapter , ( list , tuple ) ) : if not queryset_or_adapter : raise ValueError ( 'Type detection is not possible with an empty list' ) cls = _adapters . get ( queryset_or_adapter [ 0 ] . __class__ ) adapter = cls ( queryset_or_adapter ) elif isinstance ( queryset_or_adapter , db . BaseQuerySet ) : cls = _adapters . get ( queryset_or_adapter . _document ) adapter = cls ( queryset_or_adapter ) else : raise ValueError ( 'Unsupported object type' ) timestamp = datetime . now ( ) . strftime ( '%Y-%m-%d-%H-%M' ) headers = { b'Content-Disposition' : 'attachment; filename={0}-{1}.csv' . format ( basename or 'export' , timestamp ) , } streamer = stream_with_context ( yield_rows ( adapter ) ) return Response ( streamer , mimetype = "text/csv" , headers = headers )
Stream a csv file from an object list,

    a queryset or an instanciated adapter.