<s> from __future__ import unicode_literals <EOL> import csv <EOL> from django . http import HttpResponse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> def export_as_csv_action ( description = None , fields = None , exclude = None , <EOL> header = True ) : <EOL> """<STR_LIT>""" <EOL> def export_as_csv ( modeladmin , request , queryset ) : <EOL> """<STR_LIT>""" <EOL> opts = modeladmin . model . _meta <EOL> if fields : <EOL> fieldset = set ( fields ) <EOL> field_names = fieldset <EOL> elif exclude : <EOL> excludeset = set ( exclude ) <EOL> field_names = field_names - excludeset <EOL> response = HttpResponse ( content_type = "<STR_LIT>" ) <EOL> response [ "<STR_LIT>" ] = "<STR_LIT>" % unicode ( opts ) . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) <EOL> writer = csv . writer ( response ) <EOL> if header : <EOL> writer . writerow ( list ( field_names ) ) <EOL> for obj in queryset : <EOL> writer . writerow ( <EOL> [ unicode ( getattr ( obj , field ) ) . encode ( "<STR_LIT:utf-8>" , "<STR_LIT:replace>" ) for field in field_names ] ) <EOL> return response <EOL> if description is None : <EOL> description = _ ( "<STR_LIT>" ) <EOL> export_as_csv . short_description = description <EOL> return export_as_csv </s>
