<s> from __future__ import division <EOL> from csv import DictWriter <EOL> from csv import writer <EOL> from StringIO import StringIO <EOL> from django . http import HttpResponse <EOL> from django import template as django_template <EOL> from django import VERSION <EOL> class CsvDataMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . out = StringIO ( ) <EOL> super ( CsvDataMixin , self ) . __init__ ( ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> self . writer = DictWriter ( self . out , map ( self . encode , self . columns ) ) <EOL> self . is_dict = True <EOL> else : <EOL> self . writer = writer ( self . out ) <EOL> self . is_dict = False <EOL> def write_csv_header ( self ) : <EOL> if self . is_dict : <EOL> try : <EOL> self . writer . writeheader ( ) <EOL> except AttributeError : <EOL> self . writer . writerow ( dict ( zip ( <EOL> self . writer . fieldnames , <EOL> self . writer . fieldnames ) ) ) <EOL> def write_csv_row ( self , args ) : <EOL> if self . is_dict : <EOL> self . writer . writerow ( dict ( zip ( <EOL> self . writer . fieldnames , map ( self . encode , args ) ) ) ) <EOL> else : <EOL> self . writer . writerow ( map ( self . encode , args ) ) <EOL> def encode ( self , value ) : <EOL> return unicode ( value ) . encode ( '<STR_LIT:utf-8>' ) <EOL> class BaseCsvResponse ( CsvDataMixin , HttpResponse ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , template , context , content_type , ** kwargs ) : <EOL> super ( BaseCsvResponse , self ) . __init__ ( ) <EOL> self [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> kwargs . get ( "<STR_LIT:filename>" , "<STR_LIT>" ) , ) <EOL> self [ '<STR_LIT:Content-Type>' ] = content_type <EOL> self . context = context <EOL> self . header = None <EOL> if template : <EOL> header_template = django_template . loader . get_template ( template ) <EOL> context = django_template . RequestContext ( request , self . context ) <EOL> self . header = header_template . render ( context ) <EOL> if self . header : <EOL> self . out . write ( self . encode ( self . header ) ) <EOL> self . write_csv_header ( ) <EOL> for row in self . get_row_data ( ) : <EOL> self . write_csv_row ( row ) <EOL> self . out . flush ( ) <EOL> self . content = self . out . getvalue ( ) <EOL> self . out . close ( ) <EOL> def get_row_data ( self ) : <EOL> return [ ] <EOL> if VERSION >= ( <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:0> ) : <EOL> from django . http import StreamingHttpResponse <EOL> class BaseCsvStreamingResponse ( CsvDataMixin , StreamingHttpResponse ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , template , context , content_type , ** kwargs ) : <EOL> super ( BaseCsvStreamingResponse , self ) . __init__ ( ) <EOL> self [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> kwargs . get ( "<STR_LIT:filename>" , "<STR_LIT>" ) , ) <EOL> self [ '<STR_LIT:Content-Type>' ] = content_type <EOL> self . context = context <EOL> self . header = None <EOL> if template : <EOL> header_template = django_template . loader . get_template ( template ) <EOL> context = django_template . RequestContext ( request , self . context ) <EOL> self . header = header_template . render ( context ) <EOL> self . _closable_objects . append ( self . out ) <EOL> self . streaming_content = self . get_content ( ) <EOL> def buffer ( self ) : <EOL> buf = self . out . getvalue ( ) <EOL> self . out . truncate ( <NUM_LIT:0> ) <EOL> return buf <EOL> def get_content ( self ) : <EOL> if self . header : <EOL> self . out . write ( self . encode ( self . header ) ) <EOL> self . write_csv_header ( ) <EOL> yield self . buffer ( ) <EOL> for row in self . get_row_data ( ) : <EOL> self . write_csv_row ( row ) <EOL> yield self . buffer ( ) <EOL> def get_row_data ( self ) : <EOL> return [ ] </s>
