2020-11-07 16:58:29 +00:00
|
|
|
from django import forms
|
|
|
|
from django.contrib.auth.forms import UsernameField
|
2020-12-29 11:22:36 +00:00
|
|
|
from myauth.models import get_typed_user_model
|
2020-11-07 16:58:29 +00:00
|
|
|
|
2020-12-29 11:22:36 +00:00
|
|
|
User = get_typed_user_model()
|
2020-11-07 16:58:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
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",)
|
2020-11-14 15:04:41 +00:00
|
|
|
field_classes = {"username": UsernameField}
|
2020-11-07 16:58:29 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if self._meta.model.USERNAME_FIELD in self.fields:
|
2020-11-14 15:04:41 +00:00
|
|
|
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs["autofocus"] = True
|
2020-11-07 16:58:29 +00:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
user = super().save(commit=False)
|
|
|
|
user.set_unusable_password()
|
|
|
|
if commit:
|
|
|
|
user.save()
|
|
|
|
return user
|