Skip to content
This repository
Browse code

add csv export to proposals admin

  • Loading branch information...
commit 0b33ce93ccfc8fa43197382d2596eb698db7ba48 1 parent c818e1c
Luke Hatcher authored December 01, 2011
36  symposion/proposals/actions.py
... ...
@@ -0,0 +1,36 @@
  1
+import csv
  2
+
  3
+from django.http import HttpResponse
  4
+
  5
+
  6
+def export_as_csv_action(description="Export selected objects as CSV file",
  7
+                         fields=None, exclude=None, header=True):
  8
+    """
  9
+    This function returns an export csv action
  10
+    'fields' and 'exclude' work like in django ModelForm
  11
+    'header' is whether or not to output the column names as the first row
  12
+    """
  13
+    def export_as_csv(modeladmin, request, queryset):
  14
+        """
  15
+        Generic csv export admin action.
  16
+        based on http://djangosnippets.org/snippets/1697/
  17
+        """
  18
+        opts = modeladmin.model._meta
  19
+        if fields:
  20
+            fieldset = set(fields)
  21
+            field_names = fieldset
  22
+        elif exclude:
  23
+            excludeset = set(exclude)
  24
+            field_names = field_names - excludeset
  25
+
  26
+        response = HttpResponse(mimetype='text/csv')
  27
+        response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_')
  28
+
  29
+        writer = csv.writer(response)
  30
+        if header:
  31
+            writer.writerow(list(field_names))
  32
+        for obj in queryset:
  33
+            writer.writerow([unicode(getattr(obj, field)).encode("utf-8","replace") for field in field_names])
  34
+        return response
  35
+    export_as_csv.short_description = description
  36
+    return export_as_csv
15  symposion/proposals/admin.py
... ...
@@ -1,13 +1,26 @@
1 1
 from django.contrib import admin
2 2
 
  3
+from symposion.proposals.actions import export_as_csv_action
3 4
 from symposion.proposals.models import Proposal
4 5
 
5 6
 
6 7
 admin.site.register(Proposal,
7 8
     list_display = [
8 9
         "title",
  10
+        "speaker",
  11
+        "speaker_email",
9 12
         "kind",
10 13
         "audience_level",
11 14
         "cancelled",
12  
-    ]
  15
+    ],
  16
+    list_filter = [
  17
+        "kind__name",
  18
+        "result__accepted",
  19
+    ],
  20
+    actions = [export_as_csv_action("CSV Export", fields=[
  21
+        "title",
  22
+        "speaker",
  23
+        "speaker_email",
  24
+        "kind",
  25
+    ])]
13 26
 )
4  symposion/proposals/models.py
@@ -57,6 +57,10 @@ def can_edit(self):
57 57
         return True
58 58
     
59 59
     @property
  60
+    def speaker_email(self):
  61
+        return self.speaker.email
  62
+
  63
+    @property
60 64
     def number(self):
61 65
         return str(self.pk).zfill(3)
62 66
     

0 notes on commit 0b33ce9

Please sign in to comment.
Something went wrong with that request. Please try again.