Permalink
Browse files

[fix bug 820747] [fix bug 820398] Enhance 'Users' and 'Groups' Admin …

…panel.
  • Loading branch information...
1 parent 10f1939 commit a7c9c06f8546663f7e06ca1b287f011ff24f56e1 @glogiotatidis glogiotatidis committed Dec 11, 2012
Showing with 117 additions and 2 deletions.
  1. +43 −0 apps/common/admin.py
  2. +25 −1 apps/groups/admin.py
  3. +49 −1 apps/users/admin.py
View
@@ -0,0 +1,43 @@
+import csv
+
+from django.http import HttpResponse
+
+
+def export_as_csv_action(description=None, 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
+
+ Based on snippet http://djangosnippets.org/snippets/2020/
+ """
+
+ def export_as_csv(modeladmin, request, queryset):
+ """
+ Generic csv export admin action.
+ based on http://djangosnippets.org/snippets/1697/
+ """
+ opts = modeladmin.model._meta
+ field_names = set([field.name for field in opts.fields])
+ if fields:
+ fieldset = set(fields)
+ field_names = 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, delimiter=';')
+ if header:
+ writer.writerow(list(field_names))
+ for obj in queryset:
+ writer.writerow([unicode(getattr(obj, field)).encode('utf-8')
+ for field in field_names])
+ return response
+
+ export_as_csv.short_description = (description or 'Export to CSV file')
+ return export_as_csv
View
@@ -1,11 +1,35 @@
from django.contrib import admin
+from django.contrib.admin import SimpleListFilter
from models import Group
+class CurratedGroupFilter(SimpleListFilter):
+ """Admin filter for curated groups."""
+ title = 'curated'
+ parameter_name = 'curated'
+
+ def lookups(self, request, model_admin):
+ return (('False', 'Curated'),
+ ('True', 'Not curated'))
+
+ def queryset(self, request, queryset):
+ if self.value() is None:
+ return queryset
+ value = self.value() == 'True'
+ return queryset.filter(steward__isnull=value)
+
+
class GroupAdmin(admin.ModelAdmin):
- list_display = ['name', 'steward', 'wiki', 'website', 'irc_channel']
+ """Group Admin."""
+ list_display = ['name', 'steward', 'wiki', 'website', 'irc_channel',
+ 'no_members']
search_fields = ['name']
raw_id_fields = ['steward']
+ list_filter = [CurratedGroupFilter]
+
+ def no_members(self, obj):
+ """Return number of members in group."""
+ return obj.userprofile_set.count()
admin.site.register(Group, GroupAdmin)
View
@@ -1,19 +1,65 @@
+from datetime import datetime, timedelta
+
from functools import update_wrapper
from django.conf.urls.defaults import patterns, url
from django.contrib import admin
from django.contrib import messages
+from django.contrib.admin import SimpleListFilter
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import Group, User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
+from apps.common.admin import export_as_csv_action
+
from .cron import index_all_profiles
from .models import UserProfile, UsernameBlacklist
admin.site.unregister(User)
admin.site.unregister(Group)
+class DateJoinedFilter(SimpleListFilter):
+ """Admin filter for date joined."""
+ title = 'date joined'
+ parameter_name = 'date_joined'
+
+ def lookups(self, request, model_admin):
+ return map(lambda x: (str(x.year), x.year),
+ User.objects.dates('date_joined', 'year'))
+
+ def queryset(self, request, queryset):
+ if self.value() is None:
+ return queryset
+ else:
+ return queryset.filter(date_joined__year=self.value())
+ return queryset
+
+
+class LastLoginFilter(SimpleListFilter):
+ """Admin filter for last login."""
+ title = 'last login'
+ parameter_name = 'last_login'
+
+ def lookups(self, request, model_admin):
+ return (('<6', 'Less than 6 months'),
+ ('>6', 'Between 6 and 12 months'),
+ ('>12', 'More than a year'))
+
+ def queryset(self, request, queryset):
+ half_year = datetime.today() - timedelta(days=180)
+ full_year = datetime.today() - timedelta(days=360)
+
+ if self.value() == '<6':
+ return queryset.filter(last_login__gte=half_year)
+ elif self.value() == '>6':
+ return queryset.filter(last_login__lt=half_year,
+ last_login__gt=full_year)
+ elif self.value() == '>12':
+ return queryset.filter(last_login__lt=full_year)
+ return queryset
+
+
class UserProfileInline(admin.StackedInline):
"""UserProfile Inline model for UserAdmin."""
model = UserProfile
@@ -25,11 +71,13 @@ class UserAdmin(UserAdmin):
inlines = [UserProfileInline]
search_fields = ['userprofile__full_name', 'email', 'username',
'userprofile__ircname']
- list_filter = ['userprofile__is_vouched']
+ list_filter = ['userprofile__is_vouched', DateJoinedFilter,
+ LastLoginFilter]
save_on_top = True
list_display = ['full_name', 'email', 'username', 'country', 'is_vouched',
'vouched_by']
list_display_links = ['full_name', 'email', 'username']
+ actions = [export_as_csv_action(fields=('username', 'email'), header=True)]
def country(self, obj):
return obj.userprofile.country

0 comments on commit a7c9c06

Please sign in to comment.