#save as useradmin.py
from django.utils.safestring import mark_safe
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.contrib import admin
def roles(self):
#short_name = unicode # function to get group name
short_name = lambda x:unicode(x)[:1].upper() # first letter of a group
p = sorted(["%s" % (x, short_name(x)) for x in self.groups.all()])
if self.user_permissions.count(): p += ['+']
value = ', '.join(p)
return mark_safe("%s" % value)
roles.allow_tags = True
roles.short_description = 'roles'
def last(self):
fmt = "%b %d, %H:%M"
#fmt = "%Y %b %d, %H:%M:%S"
value = self.last_login.strftime(fmt)
return mark_safe("%s" % value)
last.allow_tags = True
last.admin_order_field = 'last_login'
last.short_description = 'last login'
def adm(self):
return self.is_superuser
adm.boolean = True
adm.admin_order_field = 'is_superuser'
def staff(self):
return self.is_staff
staff.boolean = True
staff.admin_order_field = 'is_staff'
User.roles = roles
User.last = last
User.adm = adm
User.staff = staff
UserAdmin = admin.site._registry[User]
UserAdmin.list_display = ['username', 'email', 'first_name', 'last_name'] \
+ ['is_active', 'staff', 'adm', 'roles', 'last']
#use last few lines only when you have not very much users in each group
def persons(self):
return ", ".join([x.username for x in self.user_set.all()])
Group.persons = persons
GroupAdmin = admin.site._registry[Group]
GroupAdmin.list_display = ['name', 'persons']