Login

Improved User Admin

Author:
gregb
Posted:
May 2, 2010
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.