Add-on for auth app of newforms-admin branch, adding more information about users and their groups at user and group list in admin interface. Also, nice example on customization of contributed django admin apps.
It adds the following to the user list interface: fields for is_superuser and is_staff, last login time; by default, short names of groups user is in (mousehover to see full names) It adds the following to the to group list interface: list of users in your groups.
To enable, just put it somewhere and import it from your main urls.py: import useradmin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #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(["<a title='%s'>%s</a>" % (x, short_name(x)) for x in self.groups.all()])
if self.user_permissions.count(): p += ['+']
value = ', '.join(p)
return mark_safe("<nobr>%s</nobr>" % 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("<nobr>%s</nobr>" % 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']
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
The
import useradmin
must come afteradmin.autodiscover()
is called in your main urls.py in r8010.#
on django 1.4 I need to move this snippet from root project to the first app that have an admin.py.
I found that import this snippet on urls.py (on root project) gives an error because User and Group models are not yet registered by admin object. It's what I assume or seems.
That's why I try to move it for my first app that has an admin.py and works perfectly again.
see ay
#
Please login first before commenting.