Export user info as a CSV, fixes #368
authorCharles Connell <charles@connells.org>
Fri, 16 May 2014 19:05:07 +0000 (15:05 -0400)
committerCharles Connell <charles@connells.org>
Fri, 16 May 2014 19:05:07 +0000 (15:05 -0400)
karmaworld/apps/users/admin.py

index 2e29b1668863200b863360f2acf063e1726bf1be..0b3df963015ec88bc5c21b456a464e53fbb47e7f 100644 (file)
@@ -2,10 +2,27 @@
 # -*- coding:utf8 -*-
 # Copyright (C) 2013  FinalsClub Foundation
 from django.contrib import admin
+import csv
+import StringIO
+from django.contrib.admin import ModelAdmin
+from django.http import HttpResponse
 from karmaworld.apps.users.models import UserProfile, NoteKarmaEvent, CourseKarmaEvent, GenericKarmaEvent
 
+def user_export(modeladmin, request, queryset):
+    f = StringIO.StringIO()
+    writer = csv.writer(f, dialect='excel')
+    writer.writerow(['First Name', 'Last Name', 'Email'])
+    for user_profile in queryset:
+        user = user_profile.user
+        writer.writerow([user.first_name, user.last_name, user.email])
+    return HttpResponse(f.getvalue())
+user_export.short_description = "Download a CSV file with users' information"
 
-admin.site.register(UserProfile)
+
+class UserProfileAdmin(ModelAdmin):
+    actions = (user_export,)
+
+admin.site.register(UserProfile, UserProfileAdmin)
 admin.site.register(GenericKarmaEvent)
 admin.site.register(NoteKarmaEvent)
 admin.site.register(CourseKarmaEvent)