etesync-server/myauth/forms.py
2020-12-29 13:32:19 +02:00

30 lines
843 B
Python

from django import forms
from django.contrib.auth.forms import UsernameField
from myauth.models import get_typed_user_model
User = get_typed_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