admin: customize admin forms for user models
This commit is contained in:
parent
d946bf7ebc
commit
1ab1149927
|
@ -1,13 +1,47 @@
|
|||
from copy import deepcopy
|
||||
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from django.conf import settings
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .nonce.models import Nonce
|
||||
|
||||
class NonceModelAdmin(admin.ModelAdmin):
|
||||
list_display = ("value", "context", "not_on_or_after")
|
||||
if settings.DEBUG:
|
||||
class NonceModelAdmin(admin.ModelAdmin):
|
||||
list_display = ("value", "context", "not_on_or_after")
|
||||
admin.site.register(Nonce, NonceModelAdmin)
|
||||
|
||||
admin.site.register(Nonce, NonceModelAdmin)
|
||||
if settings.AUTH_USER_MODEL == 'authentic2.User':
|
||||
from models import User
|
||||
admin.site.register(User, UserAdmin)
|
||||
import models
|
||||
import admin_forms
|
||||
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
class AuthenticUserAdmin(UserAdmin):
|
||||
fieldsets = (
|
||||
(None, {'fields': ('username', 'password')}),
|
||||
(_('Personal info'), {'fields': ('first_name', 'last_name',
|
||||
'email', 'nickname', 'url', 'company', 'phone',
|
||||
'postal_address')}),
|
||||
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
|
||||
'groups')}),
|
||||
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
|
||||
)
|
||||
form = admin_forms.UserChangeForm
|
||||
add_form = admin_forms.UserCreationForm
|
||||
add_fieldsets = (
|
||||
(None, {
|
||||
'classes': ('wide',),
|
||||
'fields': ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')}
|
||||
),
|
||||
)
|
||||
|
||||
def get_fieldsets(self, request, obj=None):
|
||||
fieldsets = deepcopy(super(AuthenticUserAdmin, self).get_fieldsets(request, obj))
|
||||
if obj:
|
||||
if not request.user.is_superuser:
|
||||
fieldsets[2][1]['fields'] = filter(lambda x: x !=
|
||||
'is_superuser', fieldsets[2][1]['fields'])
|
||||
return fieldsets
|
||||
|
||||
|
||||
admin.site.register(models.User, AuthenticUserAdmin)
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
from django import forms
|
||||
|
||||
from django.contrib.auth.forms import UserChangeForm as AuthUserChangeForm, UserCreationForm as AuthUserCreationForm
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
class UserChangeForm(AuthUserChangeForm):
|
||||
class Meta(AuthUserChangeForm.Meta):
|
||||
model = get_user_model()
|
||||
|
||||
class UserCreationForm(AuthUserCreationForm):
|
||||
class Meta(AuthUserCreationForm.Meta):
|
||||
model = get_user_model()
|
||||
|
||||
|
||||
def clean_username(self):
|
||||
# Since User.username is unique, this check is redundant,
|
||||
# but it sets a nicer error message than the ORM. See #13147.
|
||||
username = self.cleaned_data["username"]
|
||||
User = get_user_model()
|
||||
try:
|
||||
User._default_manager.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
return username
|
||||
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
Loading…
Reference in New Issue