From 9be07d310b20af2acb1f7dcaf0dc17bd6c928b26 Mon Sep 17 00:00:00 2001 From: Hampus Toft Date: Wed, 1 Nov 2023 23:14:45 +0100 Subject: [PATCH 1/3] Sort section list by its abbreviation --- src/members/forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/members/forms.py b/src/members/forms.py index cfcbc185..c646232e 100644 --- a/src/members/forms.py +++ b/src/members/forms.py @@ -348,7 +348,7 @@ class CustomUserEditForm(UserEditForm): ) section = forms.ModelChoiceField( required=False, - queryset=Section.objects, + queryset=Section.objects.order_by('abbreviation'), label=_('Section'), ) status = forms.ChoiceField( @@ -412,7 +412,7 @@ class CustomUserCreationForm(UserCreationForm): ) section = forms.ModelChoiceField( required=False, - queryset=Section.objects, + queryset=Section.objects.order_by('abbreviation'), label=_("Section"), ) From 1a9427cd3ba14a342b51658feebbd91ae52f2fba Mon Sep 17 00:00:00 2001 From: Hampus Toft Date: Wed, 1 Nov 2023 23:19:59 +0100 Subject: [PATCH 2/3] Changed RegistrationForm to CustomUserCreationForm This is the same form that admin page uses when creating users. RegistrationForm is not affected by change in forms.py in commit: 9be07d310b20af2acb1f7dcaf0dc17bd6c928b26 and thereby did not get a sorted section list from previous change. I do not see the diffrence in accont creation between the two forms. --- src/members/urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/members/urls.py b/src/members/urls.py index 3f266a08..2f9402d7 100644 --- a/src/members/urls.py +++ b/src/members/urls.py @@ -3,7 +3,7 @@ from django.views.generic import CreateView from members import views -from members.forms import RegistrationForm +from members.forms import CustomUserCreationForm urlpatterns = [ re_path(r'^profile/$', views.ProfileView.as_view(), name='profile'), @@ -14,7 +14,7 @@ ), re_path('^register/', CreateView.as_view( template_name='members/register.html', - form_class=RegistrationForm, + form_class=CustomUserCreationForm, success_url=reverse_lazy('login'), ), name='register'), From e1dad4eeb7089bf5b570f486c9438aef21e56b04 Mon Sep 17 00:00:00 2001 From: Hampus Toft Date: Wed, 8 Nov 2023 17:49:21 +0100 Subject: [PATCH 3/3] Changed forms.py Changed so that section sorting occurs on all ModelForm's. This allows us to yet again use a custom ModelForm for admin page and another for the registration page. --- src/members/forms.py | 22 ++++++++++++++++++++++ src/members/urls.py | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/members/forms.py b/src/members/forms.py index c646232e..c6419f0d 100644 --- a/src/members/forms.py +++ b/src/members/forms.py @@ -24,6 +24,11 @@ class MemberForm(forms.ModelForm): + section = forms.ModelChoiceField( + required=False, + queryset=Section.objects.order_by('abbreviation'), + label=_('Section'), + ) person_number = PersonNumberField( label=_('Person number'), help_text=_('Person number using the YYYYMMDD-XXXX format.'), @@ -81,6 +86,12 @@ def save(self, commit=True): class RegistrationForm(MemberForm, auth.UserCreationForm): + section = forms.ModelChoiceField( + required=False, + queryset=Section.objects.order_by('abbreviation'), + label=_('Section'), + ) + class Meta: model = Member fields = ['username', 'email', 'phone_number', 'section'] @@ -216,6 +227,12 @@ def password_enabled(self): widget=forms.PasswordInput, help_text=_("Enter the same password as above, for verification.")) + section = forms.ModelChoiceField( + required=False, + queryset=Section.objects.order_by('abbreviation'), + label=_('Section'), + ) + is_superuser = forms.BooleanField( label=_("Administrator"), required=False, help_text=_('Administrators have full access to manage any object ' @@ -304,6 +321,11 @@ def save(self, commit=True): class UserEditForm(UserForm): password_required = False + section = forms.ModelChoiceField( + required=False, + queryset=Section.objects.order_by('abbreviation'), + label=_('Section'), + ) def __init__(self, *args, **kwargs): kwargs.pop('editing_self', False) diff --git a/src/members/urls.py b/src/members/urls.py index 2f9402d7..3f266a08 100644 --- a/src/members/urls.py +++ b/src/members/urls.py @@ -3,7 +3,7 @@ from django.views.generic import CreateView from members import views -from members.forms import CustomUserCreationForm +from members.forms import RegistrationForm urlpatterns = [ re_path(r'^profile/$', views.ProfileView.as_view(), name='profile'), @@ -14,7 +14,7 @@ ), re_path('^register/', CreateView.as_view( template_name='members/register.html', - form_class=CustomUserCreationForm, + form_class=RegistrationForm, success_url=reverse_lazy('login'), ), name='register'),