Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add csv export to proposals admin

  • Loading branch information...
commit 0b33ce93ccfc8fa43197382d2596eb698db7ba48 1 parent c818e1c
Luke Hatcher lukeman authored
36 symposion/proposals/actions.py
View
@@ -0,0 +1,36 @@
+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(mimetype='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
15 symposion/proposals/admin.py
View
@@ -1,13 +1,26 @@
from django.contrib import admin
+from symposion.proposals.actions import export_as_csv_action
from symposion.proposals.models import Proposal
admin.site.register(Proposal,
list_display = [
"title",
+ "speaker",
+ "speaker_email",
"kind",
"audience_level",
"cancelled",
- ]
+ ],
+ list_filter = [
+ "kind__name",
+ "result__accepted",
+ ],
+ actions = [export_as_csv_action("CSV Export", fields=[
+ "title",
+ "speaker",
+ "speaker_email",
+ "kind",
+ ])]
)
4 symposion/proposals/models.py
View
@@ -57,6 +57,10 @@ def can_edit(self):
return True
@property
+ def speaker_email(self):
+ return self.speaker.email
+
+ @property
def number(self):
return str(self.pk).zfill(3)
Please sign in to comment.
Something went wrong with that request. Please try again.