Login

Improved User Admin

Author:
gregb
Posted:
May 2, 2010
Language:
Python
Version:
1.1
Tags:
admin user userprofile
Score:
1 (after 1 ratings)

Helper function which adds some niceties to the auth/user admin views. Needs django 1.2 to work.

Usage

Define a UserProfile class and set AUTH_PROFILE_MODULE as per the django docs

In your admin.py file (or anywhere else) add the following:

from models import UserProfile
from [path to snippet] import upgrade_user_admin

upgrade_user_admin(UserProfile)
 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
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.forms.models import inlineformset_factory



def upgrade_user_admin(UserProfile):

    class UserProfileFormSet(inlineformset_factory(User, UserProfile)):
        def __init__(self, *args, **kwargs):
            super(UserProfileFormSet, self).__init__(*args, **kwargs)
            self.can_delete = False
    
    # Allow user profiles to be edited inline with User
    class UserProfileInline(admin.StackedInline):
        model = UserProfile
        fk_name = 'user'
        max_num = 1
        extra = 0
        formset = UserProfileFormSet
    
    class MyUserAdmin(UserAdmin):
        inlines = [UserProfileInline, ]
        actions = ['make_active', 'make_inactive',]
        list_filter = ['is_active', 'is_staff', 'is_superuser', 'date_joined', 'last_login',]
        list_display = ['first_name', 'last_name', 'email', 'username', 'date_joined',]
        list_display_links = ['first_name', 'last_name', 'email', 'username']
        
        def make_active(self, request, queryset):
            rows_updated = queryset.update(is_active=True)
            if rows_updated == 1:
                message_bit = "1 person was"
            else:
                message_bit = "%s people were" % rows_updated
            self.message_user(request, "%s successfully made active." % message_bit)
    
        def make_inactive(self, request, queryset):
            rows_updated = queryset.update(is_active=False)
            if rows_updated == 1:
                message_bit = "1 person was"
            else:
                message_bit = "%s people were" % rows_updated
            self.message_user(request, "%s successfully made inactive." % message_bit)


    # Re-register UserAdmin
    admin.site.unregister(User)
    admin.site.register(User, MyUserAdmin)

More like this

  1. Multiple User subclasses custom Auth backend by ungenio41 3 years, 10 months ago
  2. convenience parent class for UserProfile model by willhardy 6 years, 3 months ago
  3. More information about users and groups in user admin by buriy 7 years ago
  4. Alternative to User.get_profile() by jpwatts 7 years, 4 months ago
  5. group_required decorator by msanders 5 years, 11 months ago

Comments

Please login first before commenting.