Skip to content

Instantly share code, notes, and snippets.

@airtonix
Last active December 16, 2015 19:38
Show Gist options
  • Save airtonix/5486055 to your computer and use it in GitHub Desktop.
Save airtonix/5486055 to your computer and use it in GitHub Desktop.
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
from tastypie.models import ApiAccess, ApiKey
from models import UserProfile
try:
admin.site.unregister(User)
admin.site.unregister(ApiKey)
except Exception as error:
pass
class ApiKeyInline(admin.StackedInline):
model = ApiKey
extra = 0
class UserProfileInline(admin.StackedInline):
model = UserProfile
max_num = 1
extra = 0
can_delete = False
fk_name = 'user'
class UserAdmin(AuthUserAdmin):
inlines = [UserProfileInline, ApiKeyInline]
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 = "{} people were".format(rows_updated)
self.message_user(request, "{} successfully made active.".format(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 = "{} people were".format(rows_updated)
self.message_user(request, "{} successfully made inactive.".format(message_bit))
admin.site.register(User, UserAdmin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment