Change user creation to not ask for a password (and clarify the readme).

This commit is contained in:
Tom Hacohen
2020-11-07 18:58:29 +02:00
parent 801826b8b6
commit 47103df48a
3 changed files with 45 additions and 5 deletions

View File

@@ -1,5 +1,16 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from .models import User
from .forms import AdminUserCreationForm
class UserAdmin(DjangoUserAdmin):
add_form = AdminUserCreationForm
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', ),
}),
)
admin.site.register(User, UserAdmin)

30
myauth/forms.py Normal file
View File

@@ -0,0 +1,30 @@
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UsernameField
User = get_user_model()
class AdminUserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
class Meta:
model = User
fields = ("username",)
field_classes = {'username': UsernameField}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self._meta.model.USERNAME_FIELD in self.fields:
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs['autofocus'] = True
def save(self, commit=True):
user = super().save(commit=False)
user.set_unusable_password()
if commit:
user.save()
return user