From 07788872faf549f874af997ab4e0651e4c807f57 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Fri, 16 May 2014 15:05:07 -0400 Subject: [PATCH] Export user info as a CSV, fixes #368 --- karmaworld/apps/users/admin.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/karmaworld/apps/users/admin.py b/karmaworld/apps/users/admin.py index 2e29b16..0b3df96 100644 --- a/karmaworld/apps/users/admin.py +++ b/karmaworld/apps/users/admin.py @@ -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) -- 2.25.1