Package madrona :: Package common :: Module admin
[hide private]

Source Code for Module madrona.common.admin

 1  # utils/admin_auth.py 
 2  # -*- coding: utf-8 -*- 
 3  # derived from http://www.djangosnippets.org/snippets/1650/ 
 4  from django.utils.safestring import mark_safe 
 5  from django.contrib.auth.models import User, Group 
 6  from django.contrib.auth.admin import UserAdmin, GroupAdmin 
 7  from django.contrib import admin 
 8   
9 -def roles(self):
10 #short_name = unicode # function to get group name 11 p = sorted([u"<a title='%s'>%s</a>" % (x, x) for x in self.groups.all()]) 12 if self.user_permissions.count(): 13 p += ['+'] 14 value = ' | '.join(p) 15 return mark_safe("%s" % value)
16 roles.allow_tags = True 17 roles.short_description = u'Groups' 18
19 -def last(self):
20 fmt = "%b %d, %H:%M" 21 #fmt = "%Y %b %d, %H:%M:%S" 22 value = self.last_login.strftime(fmt) 23 return mark_safe("<nobr>%s</nobr>" % value)
24 last.allow_tags = True 25 last.admin_order_field = 'last_login' 26
27 -def adm(self):
28 return self.is_superuser
29 adm.boolean = True 30 adm.admin_order_field = 'is_superuser' 31
32 -def staff(self):
33 return self.is_staff
34 staff.boolean = True 35 staff.admin_order_field = 'is_staff' 36 37 from django.core.urlresolvers import reverse
38 -def persons(self):
39 return ', '.join(['<a href="%s">%s</a>' % (reverse('admin:auth_user_change', args=(x.id,)), x.username) for x in self.user_set.all().order_by('username')])
40 persons.allow_tags = True 41
42 -class GroupMembersInline(admin.TabularInline):
43 model = User.groups.through 44 verbose_name = 'Group member' 45 verbose_name_plural = 'Group members' 46 extra = 3
47
48 -def allow_share_to_staff(modeladmin, request, queryset):
49 from django.conf import settings 50 sg = settings.SHARING_TO_STAFF_GROUPS 51 if hasattr(sg,'__iter__') and len(sg) > 0: 52 for g in sg: 53 group_obj = Group.objects.get(name=g) 54 for user in queryset: 55 user.groups.add(group_obj) 56 user.save()
57 allow_share_to_staff.short_description = "Allow selected users to share to staff for review" 58
59 -def remove_users_from_all_groups(modeladmin, request, queryset):
60 for user in queryset: 61 user.groups.clear() 62 user.save()
63 remove_users_from_all_groups.short_description = "Remove all group associations from selected users" 64
65 -class UserAdmin(UserAdmin):
66 list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', staff, adm, roles, last] 67 list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active'] 68 actions = [allow_share_to_staff, remove_users_from_all_groups]
69
70 -class GroupAdmin(GroupAdmin):
71 list_display = ['name', persons] 72 list_display_links = ['name'] 73 inlines = (GroupMembersInline,)
74 75 admin.site.unregister(User) 76 admin.site.unregister(Group) 77 admin.site.register(User, UserAdmin) 78 admin.site.register(Group, GroupAdmin) 79