From 32aa0572f189c409bb6972c3581c0f6287ba927e Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Thu, 28 May 2020 22:24:07 +0200 Subject: [PATCH] Refactor serviceuser admin --- users/admin.py | 12 ++++-------- users/forms.py | 38 ++++++++------------------------------ 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/users/admin.py b/users/admin.py index 765bd6bf..400cb1cd 100644 --- a/users/admin.py +++ b/users/admin.py @@ -51,8 +51,7 @@ from .models import ( ) from .forms import ( UserAdminForm, - ServiceUserChangeForm, - ServiceUserCreationForm, + ServiceUserAdminForm, ) @@ -230,15 +229,15 @@ class ServiceUserAdmin(VersionAdmin, BaseUserAdmin): mot de passe; etc""" # The forms to add and change user instances - form = ServiceUserChangeForm - add_form = ServiceUserCreationForm + form = ServiceUserAdminForm + add_form = ServiceUserAdminForm # The fields to be used in displaying the User model. # These override the definitions on the base UserAdmin # that reference specific fields on auth.User. list_display = ("pseudo", "access_group") list_filter = () - fieldsets = ((None, {"fields": ("pseudo", "password", "access_group")}),) + fieldsets = ((None, {"fields": ("pseudo", "access_group", "comment", "password1", "password2")}),) # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin # overrides get_fieldsets to use this attribute when creating a user. add_fieldsets = ( @@ -263,9 +262,6 @@ admin.site.register(Ban, BanAdmin) admin.site.register(EMailAddress, EMailAddressAdmin) admin.site.register(Whitelist, WhitelistAdmin) admin.site.register(Request, RequestAdmin) -# Now register the new UserAdmin... -admin.site.unregister(ServiceUser) -admin.site.register(ServiceUser, ServiceUserAdmin) # ... and, since we're not using Django's built-in permissions, # unregister the Group model from admin. admin.site.unregister(Group) diff --git a/users/forms.py b/users/forms.py index 26975ec5..f057373d 100644 --- a/users/forms.py +++ b/users/forms.py @@ -122,7 +122,7 @@ class PassForm(FormRevMixin, FieldPermissionFormMixin, forms.ModelForm): class UserAdminForm(FormRevMixin, forms.ModelForm): - """A form for creating new users. Includes all the required + """A form for creating new and editing users. Includes all the required fields, plus a repeated password. Formulaire pour la création d'un user. N'est utilisé que pour @@ -171,7 +171,7 @@ class UserAdminForm(FormRevMixin, forms.ModelForm): return user -class ServiceUserCreationForm(FormRevMixin, forms.ModelForm): +class ServiceUserAdminForm(FormRevMixin, forms.ModelForm): """A form for creating new users. Includes all the required fields, plus a repeated password. @@ -179,25 +179,26 @@ class ServiceUserCreationForm(FormRevMixin, forms.ModelForm): Requiert seulement un mot de passe; et un pseudo""" password1 = forms.CharField( - label=_("Password"), widget=forms.PasswordInput, min_length=8, max_length=255 + label=_("Password"), + widget=forms.PasswordInput, + max_length=255, ) password2 = forms.CharField( label=_("Password confirmation"), widget=forms.PasswordInput, - min_length=8, max_length=255, ) def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) - super(ServiceUserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) + super(ServiceUserAdminForm, self).__init__(*args, prefix=prefix, **kwargs) class Meta: model = ServiceUser fields = ("pseudo",) def clean_password2(self): - """Verifie que password1 et 2 sont indentiques""" + """Verifie que password1 et 2 sont identiques""" # Check that the two password entries match password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") @@ -207,35 +208,12 @@ class ServiceUserCreationForm(FormRevMixin, forms.ModelForm): def save(self, commit=True): # Save the provided password in hashed format - user = super(ServiceUserCreationForm, self).save(commit=False) + user = super(ServiceUserAdminForm, self).save(commit=False) user.set_password(self.cleaned_data["password1"]) user.save() return user -class ServiceUserChangeForm(FormRevMixin, forms.ModelForm): - """A form for updating users. Includes all the fields on - the user, but replaces the password field with admin's - password hash display field. - - Formulaire pour l'edition des service users coté admin - """ - - password = ReadOnlyPasswordHashField() - - def __init__(self, *args, **kwargs): - prefix = kwargs.pop("prefix", self.Meta.model.__name__) - super(ServiceUserChangeForm, self).__init__(*args, prefix=prefix, **kwargs) - - class Meta: - model = ServiceUser - fields = ("pseudo",) - - def clean_password(self): - """Dummy fun""" - return self.initial["password"] - - class ResetPasswordForm(forms.Form): """Formulaire de demande de reinitialisation de mot de passe, mdp oublié"""