Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Add export CSV function to admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieu Nguyen committed Oct 20, 2013
1 parent 6c241c6 commit 4bbc3db
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions api/pyconfi2013/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from datetime import date, timedelta
import csv

from django.contrib import admin
from django.http import HttpResponse
Expand Down Expand Up @@ -124,8 +125,13 @@ class RegistrationAdmin(admin.ModelAdmin):
'ticket_type', 'country', 'dinner',
'accommodation', 'preconf')
ordering = ['-registered_timestamp']
actions = ['send_bill', 'send_payment_notification', 'show_email_addresses',
'send_late_bird_bill']
actions = [
'send_bill',
'send_payment_notification',
'show_email_addresses',
'send_late_bird_bill',
'export_as_csv',
]

def bill_overdue(self, obj):
return (obj.billed and not obj.paid and
Expand Down Expand Up @@ -230,4 +236,18 @@ def generate_emails():
show_email_addresses.short_description = ('Show email addresses of the '
'selected registrants')

def export_as_csv(self, request, queryset):
opts = self.model._meta
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_')
writer = csv.writer(response)
field_names = [field.name for field in opts.fields]
# Write a first row with header information
writer.writerow(field_names)
# Write data rows
for obj in queryset:
writer.writerow([getattr(obj, field) for field in field_names])
return response
export_as_csv.short_description = 'Export registrations as CSV file'

admin.site.register(Registration, RegistrationAdmin)

0 comments on commit 4bbc3db

Please sign in to comment.