|
|
|
|
@ -5,6 +5,8 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm
|
|
|
|
|
from django.contrib.auth.forms import ReadOnlyPasswordHashField
|
|
|
|
|
# Register your models here.
|
|
|
|
|
from .models import BlogUser
|
|
|
|
|
from django.utils.translation import gettext, gettext_lazy as _
|
|
|
|
|
from django.contrib.auth.forms import UsernameField
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlogUserCreationForm(forms.ModelForm):
|
|
|
|
|
@ -28,23 +30,29 @@ class BlogUserCreationForm(forms.ModelForm):
|
|
|
|
|
user = super().save(commit=False)
|
|
|
|
|
user.set_password(self.cleaned_data["password1"])
|
|
|
|
|
if commit:
|
|
|
|
|
user.source = 'adminsite'
|
|
|
|
|
user.save()
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlogUserChangeForm(forms.ModelForm):
|
|
|
|
|
password = ReadOnlyPasswordHashField
|
|
|
|
|
class BlogUserChangeForm(UserChangeForm):
|
|
|
|
|
password = ReadOnlyPasswordHashField(
|
|
|
|
|
label=_("Password"),
|
|
|
|
|
help_text=_(
|
|
|
|
|
"Raw passwords are not stored, so there is no way to see this "
|
|
|
|
|
"user's password, but you can change the password using "
|
|
|
|
|
"<a href=\"{}\">this form</a>."
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
email = forms.EmailField(label="Email", widget=forms.EmailInput)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = BlogUser
|
|
|
|
|
fields = ('email', 'password', 'is_active')
|
|
|
|
|
fields = '__all__'
|
|
|
|
|
field_classes = {'username': UsernameField}
|
|
|
|
|
|
|
|
|
|
def clean_password(self):
|
|
|
|
|
# Regardless of what the user provides, return the initial value.
|
|
|
|
|
# This is done here, rather than on the field, because the
|
|
|
|
|
# field does not have access to the initial value
|
|
|
|
|
return self.initial["password"]
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlogUserAdmin(UserAdmin):
|
|
|
|
|
|