Login

More information about users and groups in user admin

Author:
ramusus
Posted:
July 25, 2009
Language:
Python
Version:
1.0
Score:
5 (after 5 ratings)

Based on this snippet. More clean, with links to the related admin forms.

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 utils/admin_auth.py

 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
# utils/admin_auth.py
# -*- coding: utf-8 -*-
from django.utils.safestring import mark_safe
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
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([u"<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 = u'Groups'

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'

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'

from django.core.urlresolvers import reverse
def persons(self):
    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')])
persons.allow_tags = True

class UserAdmin(UserAdmin):
    list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', staff, adm, roles, last]
    list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active']

class GroupAdmin(GroupAdmin):
    list_display = ['name', persons]
    list_display_links = ['name']

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.register(User, UserAdmin)
admin.site.register(Group, GroupAdmin)

More like this

  1. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 2 weeks, 3 days ago
  2. Python Django CRUD Example Tutorial by tuts_station 1 month ago
  3. Browser-native date input field by kytta 2 months, 2 weeks ago
  4. Generate and render HTML Table by LLyaudet 2 months, 3 weeks ago
  5. My firs Snippets by GutemaG 3 months ago

Comments

adehnert (on August 6, 2011):

Is there a reason for the adm and staff functions? Just adding 'is_superuser' and 'is_staff' (with the quotes) to list_display works fine for me.

#

Please login first before commenting.