8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 20:03:11 +00:00

Fix user adherent/club creation form

This commit is contained in:
Gabriel Detraz 2020-05-28 22:08:16 +02:00
parent cf4423d237
commit 93e6c96493
2 changed files with 72 additions and 72 deletions

View file

@ -50,8 +50,7 @@ from .models import (
LdapUserGroup, LdapUserGroup,
) )
from .forms import ( from .forms import (
UserChangeForm, UserAdminForm,
UserCreationForm,
ServiceUserChangeForm, ServiceUserChangeForm,
ServiceUserCreationForm, ServiceUserCreationForm,
) )
@ -130,36 +129,29 @@ class WhitelistAdmin(VersionAdmin):
pass pass
class UserAdmin(VersionAdmin, BaseUserAdmin): class AdherentAdmin(VersionAdmin, BaseUserAdmin):
"""Gestion d'un user : modification des champs perso, mot de passe, etc"""
# The forms to add and change user instances # The forms to add and change user instances
form = UserChangeForm
add_form = UserCreationForm
# The fields to be used in displaying the User model. add_form = UserAdminForm
# These override the definitions on the base UserAdmin form = UserAdminForm
# that reference specific fields on auth.User.
list_display = ( list_display = (
"pseudo", "pseudo",
"name",
"surname", "surname",
"email", "email",
"local_email_redirect", "local_email_redirect",
"local_email_enabled", "local_email_enabled",
"school", "school",
"is_admin",
"shell", "shell",
) )
# Need to reset the settings from BaseUserAdmin
# They are using fields we don't use like 'is_staff'
list_filter = () list_filter = ()
fieldsets = ( fieldsets = (
(None, {"fields": ("pseudo", "password")}), (None, {"fields": ("pseudo",)}),
( (
"Personal info", "Personal info",
{"fields": ("surname", "email", "school", "shell", "uid_number", "profile_image")}, {"fields": ("surname", "name", "email", "school", "shell", "uid_number", "profile_image", "password1", "password2")},
), ),
("Permissions", {"fields": ("is_admin",)}),
) )
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user. # overrides get_fieldsets to use this attribute when creating a user.
@ -171,12 +163,59 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
"fields": ( "fields": (
"pseudo", "pseudo",
"surname", "surname",
"name",
"email", "email",
"school", "school",
"is_admin",
"password1", "password1",
"password2", "password2",
"profile_image", "profile_image",
"is_superuser",
),
},
),
)
search_fields = ("pseudo", "surname", "name")
ordering = ("pseudo",)
filter_horizontal = ()
class ClubAdmin(VersionAdmin, BaseUserAdmin):
# The forms to add and change user instances
add_form = UserAdminForm
form = UserAdminForm
list_display = (
"pseudo",
"surname",
"email",
"local_email_redirect",
"local_email_enabled",
"school",
"shell",
)
list_filter = ()
fieldsets = (
(None, {"fields": ("pseudo",)}),
(
"Personal info",
{"fields": ("surname", "email", "school", "shell", "uid_number", "profile_image", "password1", "password2")},
),
)
add_fieldsets = (
(
None,
{
"classes": ("wide",),
"fields": (
"pseudo",
"surname",
"email",
"school",
"password1",
"password2",
"profile_image",
"is_superuser",
), ),
}, },
), ),
@ -210,9 +249,8 @@ class ServiceUserAdmin(VersionAdmin, BaseUserAdmin):
filter_horizontal = () filter_horizontal = ()
admin.site.register(User, UserAdmin) admin.site.register(Adherent, AdherentAdmin)
admin.site.register(Adherent, UserAdmin) admin.site.register(Club, ClubAdmin)
admin.site.register(Club, UserAdmin)
admin.site.register(ServiceUser, ServiceUserAdmin) admin.site.register(ServiceUser, ServiceUserAdmin)
admin.site.register(LdapUser, LdapUserAdmin) admin.site.register(LdapUser, LdapUserAdmin)
admin.site.register(LdapUserGroup, LdapUserGroupAdmin) admin.site.register(LdapUserGroup, LdapUserGroupAdmin)
@ -226,9 +264,7 @@ admin.site.register(EMailAddress, EMailAddressAdmin)
admin.site.register(Whitelist, WhitelistAdmin) admin.site.register(Whitelist, WhitelistAdmin)
admin.site.register(Request, RequestAdmin) admin.site.register(Request, RequestAdmin)
# Now register the new UserAdmin... # Now register the new UserAdmin...
admin.site.unregister(User)
admin.site.unregister(ServiceUser) admin.site.unregister(ServiceUser)
admin.site.register(User, UserAdmin)
admin.site.register(ServiceUser, ServiceUserAdmin) admin.site.register(ServiceUser, ServiceUserAdmin)
# ... and, since we're not using Django's built-in permissions, # ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin. # unregister the Group model from admin.

View file

@ -121,7 +121,7 @@ class PassForm(FormRevMixin, FieldPermissionFormMixin, forms.ModelForm):
user.save() user.save()
class UserCreationForm(FormRevMixin, forms.ModelForm): class UserAdminForm(FormRevMixin, forms.ModelForm):
"""A form for creating new users. Includes all the required """A form for creating new users. Includes all the required
fields, plus a repeated password. fields, plus a repeated password.
@ -133,40 +133,41 @@ class UserCreationForm(FormRevMixin, forms.ModelForm):
label=_("Password"), label=_("Password"),
widget=forms.PasswordInput, widget=forms.PasswordInput,
max_length=255, max_length=255,
help_text=password_validators_help_text_html() help_text=password_validators_help_text_html(),
required=False,
) )
password2 = forms.CharField( password2 = forms.CharField(
label=_("Password confirmation"), label=_("Password confirmation"),
widget=forms.PasswordInput, widget=forms.PasswordInput,
max_length=255, max_length=255,
required=False,
) )
is_admin = forms.BooleanField(label=_("Is admin"))
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__) prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) super(UserAdminForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields["email"].required = True self.fields["email"].required = True
class Meta: class Meta:
model = Adherent fields = ("pseudo", "surname", "name", "email", "is_superuser")
fields = ("pseudo", "surname", "email")
def clean_password2(self): def clean_password2(self):
"""Verifie que password1 et 2 sont identiques""" """Verifie que password1 et 2 sont identiques"""
# Check that the two password entries match # Check that the two password entries match
password1 = self.cleaned_data.get("password1") password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2") password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2: if password1 and password2:
raise forms.ValidationError(_("The passwords don't match.")) if password1 and password2 and password1 != password2:
validate_password(password1) raise forms.ValidationError(_("The passwords don't match."))
validate_password(password1)
return password2 return password2
def save(self, commit=True): def save(self, commit=True):
# Save the provided password in hashed format # Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False) user = super(UserAdminForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"]) if self.cleaned_data["password1"]:
user.save() user.set_password(self.cleaned_data["password1"])
user.is_admin = self.cleaned_data.get("is_admin") user.save()
return user return user
@ -212,43 +213,6 @@ class ServiceUserCreationForm(FormRevMixin, forms.ModelForm):
return user return user
class UserChangeForm(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 la modification d'un user coté admin
"""
password = ReadOnlyPasswordHashField()
is_admin = forms.BooleanField(label=_("Is admin"), required=False)
class Meta:
model = Adherent
fields = ("pseudo", "password", "surname", "email")
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(UserChangeForm, self).__init__(*args, prefix=prefix, **kwargs)
print(_("User is admin: %s") % kwargs["instance"].is_admin)
self.initial["is_admin"] = kwargs["instance"].is_admin
def clean_password(self):
"""Dummy fun"""
# 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 save(self, commit=True):
# Save the provided password in hashed format
user = super(UserChangeForm, self).save(commit=False)
user.is_admin = self.cleaned_data.get("is_admin")
if commit:
user.save()
return user
class ServiceUserChangeForm(FormRevMixin, forms.ModelForm): class ServiceUserChangeForm(FormRevMixin, forms.ModelForm):
"""A form for updating users. Includes all the fields on """A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's the user, but replaces the password field with admin's