From 224ef5b2f034a14f6c4064eb147b5c97860383a2 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 29 Jul 2020 19:37:40 +0200 Subject: [PATCH] Allow users to have complicated username --- apps/member/forms.py | 13 +++++++++++++ apps/member/views.py | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/member/forms.py b/apps/member/forms.py index 9dc818b4..49537838 100644 --- a/apps/member/forms.py +++ b/apps/member/forms.py @@ -20,6 +20,19 @@ class CustomAuthenticationForm(AuthenticationForm): ) +class UserForm(forms.ModelForm): + def _get_validation_exclusions(self): + # Django usernames can only contain letters, numbers, @, ., +, - and _. + # We want to allow users to have uncommon and unpractical usernames: + # That is their problem, and we have normalized aliases for us. + return super()._get_validation_exclusions() + ["username"] + + class Meta: + model = User + fields = ('first_name', 'last_name', 'username', 'email',) + + + class ProfileForm(forms.ModelForm): """ A form for the extras field provided by the :model:`member.Profile` model. diff --git a/apps/member/views.py b/apps/member/views.py index 9d6b34e5..02cf69db 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -26,7 +26,7 @@ from permission.models import Role from permission.views import ProtectQuerysetMixin from wei.models import WEIClub -from .forms import ProfileForm, ClubForm, MembershipForm, CustomAuthenticationForm +from .forms import ProfileForm, ClubForm, MembershipForm, CustomAuthenticationForm, UserForm from .models import Club, Membership from .tables import ClubTable, UserTable, MembershipTable @@ -47,9 +47,10 @@ class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): Update the user information. """ model = User - fields = ['first_name', 'last_name', 'username', 'email'] + form_class = UserForm template_name = 'member/profile_update.html' context_object_name = 'user_object' + profile_form = ProfileForm def get_context_data(self, **kwargs):