symposion_app/symposion/proposals/actions.py
Patrick Altman f3614fcf52 Merge branch 'master' of https://github.com/pyohio/symposion into pyohio-master
Conflicts:
	README.md
	symposion/boxes/urls.py
	symposion/cms/urls.py
	symposion/proposals/actions.py
	symposion/proposals/urls.py
	symposion/proposals/views.py
	symposion/reviews/tests.py
	symposion/reviews/urls.py
	symposion/reviews/views.py
	symposion/schedule/forms.py
	symposion/schedule/models.py
	symposion/schedule/views.py
	symposion/speakers/fixture_gen.py
	symposion/sponsorship/urls.py
	symposion/templates/cms/file_create.html
	symposion/templates/cms/file_index.html
	symposion/templates/conference/user_list.html
	symposion/templates/dashboard.html
	symposion/templates/emails/proposal_new_message/message.html
	symposion/templates/emails/proposal_updated/message.html
	symposion/templates/emails/speaker_addition/message.html
	symposion/templates/emails/speaker_invite/message.html
	symposion/templates/proposals/_pending_proposal_row.html
	symposion/templates/proposals/_proposal_fields.html
	symposion/templates/proposals/_proposal_row.html
	symposion/templates/proposals/proposal_cancel.html
	symposion/templates/proposals/proposal_detail.html
	symposion/templates/proposals/proposal_edit.html
	symposion/templates/proposals/proposal_speaker_manage.html
	symposion/templates/proposals/proposal_submit.html
	symposion/templates/reviews/_review_table.html
	symposion/templates/reviews/base.html
	symposion/templates/reviews/result_notification.html
	symposion/templates/reviews/result_notification_prepare.html
	symposion/templates/reviews/review_admin.html
	symposion/templates/reviews/review_assignment.html
	symposion/templates/reviews/review_detail.html
	symposion/templates/reviews/review_review.html
	symposion/templates/reviews/review_stats.html
	symposion/templates/schedule/_edit_grid.html
	symposion/templates/schedule/_grid.html
	symposion/templates/schedule/_slot_edit.html
	symposion/templates/schedule/presentation_detail.html
	symposion/templates/schedule/schedule_list.html
	symposion/templates/speakers/speaker_create.html
	symposion/templates/speakers/speaker_edit.html
	symposion/templates/speakers/speaker_profile.html
	symposion/templates/sponsorship/add.html
	symposion/templates/sponsorship/apply.html
	symposion/templates/sponsorship/detail.html
	symposion/templates/sponsorship/list.html
	symposion/templates/teams/team_detail.html
2014-12-15 16:07:37 -06:00

35 lines
1.3 KiB
Python

import csv
from django.http import HttpResponse
def export_as_csv_action(description="Export selected objects as CSV file",
fields=None, exclude=None, header=True):
"""
This function returns an export csv action
'fields' and 'exclude' work like in Django ModelForm
'header' is whether or not to output the column names as the first row
"""
def export_as_csv(modeladmin, request, queryset):
"""
Generic csv export admin action.
based on http://djangosnippets.org/snippets/1697/
"""
opts = modeladmin.model._meta
if fields:
fieldset = set(fields)
field_names = fieldset
elif exclude:
excludeset = set(exclude)
field_names = field_names - excludeset
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = "attachment; filename=%s.csv" % unicode(opts).replace(".", "_")
writer = csv.writer(response)
if header:
writer.writerow(list(field_names))
for obj in queryset:
writer.writerow(
[unicode(getattr(obj, field)).encode("utf-8", "replace") for field in field_names])
return response
export_as_csv.short_description = description
return export_as_csv