From 7c08d4ccd21f462043361b60384a423520a02a58 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 24 Feb 2021 16:46:43 +0000 Subject: [PATCH] Combine name and email settings into one panel --- .../admin/tests/test_account_management.py | 24 +++++++++---------- wagtail/admin/views/account.py | 23 +++++------------- wagtail/users/forms.py | 20 ++++++++-------- 3 files changed, 28 insertions(+), 39 deletions(-) diff --git a/wagtail/admin/tests/test_account_management.py b/wagtail/admin/tests/test_account_management.py index 15720cca87..34a5fa41b5 100644 --- a/wagtail/admin/tests/test_account_management.py +++ b/wagtail/admin/tests/test_account_management.py @@ -196,9 +196,9 @@ class TestAccountSectionUtilsMixin: def post_form(self, extra_post_data): post_data = { - 'name-first_name': 'Test', - 'name-last_name': 'User', - 'email-email': self.user.email, + 'name_email-first_name': 'Test', + 'name_email-last_name': 'User', + 'name_email-email': self.user.email, 'notifications-submitted_notifications': 'false', 'notifications-approved_notifications': 'false', 'notifications-rejected_notifications': 'true', @@ -228,12 +228,12 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailadmin/account/account.html') - self.assertPanelActive(response, 'name') - self.assertPanelActive(response, 'email') + self.assertPanelActive(response, 'name_email') self.assertPanelActive(response, 'notifications') self.assertPanelActive(response, 'locale') - # These two fields may hide themselves + # These fields may hide themselves + self.assertContains(response, "Email:") self.assertContains(response, "Preferred language:") self.assertContains(response, "Current time zone:") @@ -242,8 +242,8 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi def test_change_name_post(self): response = self.post_form({ - 'name-first_name': 'Fox', - 'name-last_name': 'Mulder', + 'name_email-first_name': 'Fox', + 'name_email-last_name': 'Mulder', }) # Check that the user was redirected to the account page @@ -256,7 +256,7 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi def test_change_email_post(self): response = self.post_form({ - 'email-email': 'test@email.com', + 'name_email-email': 'test@email.com', }) # Check that the user was redirected to the account page @@ -268,14 +268,14 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi def test_change_email_not_valid(self): response = self.post_form({ - 'email-email': 'test@email', + 'name_email-email': 'test@email', }) # Check that the user wasn't redirected self.assertEqual(response.status_code, 200) # Check that a validation error was raised - self.assertTrue('email' in response.context['panels_by_tab'][profile_tab][1].get_form().errors.keys()) + self.assertTrue('email' in response.context['panels_by_tab'][profile_tab][0].get_form().errors.keys()) # Check that the email was not changed self.user.refresh_from_db() @@ -288,7 +288,7 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailadmin/account/account.html') - self.assertPanelNotActive(response, 'email') + self.assertNotContains(response, "Email:") @override_settings(WAGTAIL_PASSWORD_MANAGEMENT_ENABLED=False) def test_account_view_with_password_management_disabled(self): diff --git a/wagtail/admin/views/account.py b/wagtail/admin/views/account.py index 3defd5c10a..1bc7e29f16 100644 --- a/wagtail/admin/views/account.py +++ b/wagtail/admin/views/account.py @@ -20,7 +20,7 @@ from wagtail.admin.localization import get_available_admin_languages, get_availa from wagtail.core import hooks from wagtail.core.models import UserPagePermissionsProxy from wagtail.users.forms import ( - AvatarPreferencesForm, EmailForm, LocalePreferencesForm, NameForm, NotificationPreferencesForm) + AvatarPreferencesForm, LocalePreferencesForm, NameEmailForm, NotificationPreferencesForm) from wagtail.users.models import UserProfile from wagtail.utils.loading import get_custom_form @@ -114,21 +114,11 @@ class BaseSettingsPanel: return render_to_string(self.template_name, self.get_context_data(), request=self.request) -class NameSettingsPanel(BaseSettingsPanel): - name = 'name' - title = gettext_lazy('Name') +class NameEmailSettingsPanel(BaseSettingsPanel): + name = 'name_email' + title = gettext_lazy('Name and Email') order = 100 - form_class = NameForm - - -class EmailSettingsPanel(BaseSettingsPanel): - name = 'email' - title = gettext_lazy('Email') - order = 200 - form_class = EmailForm - - def is_active(self): - return email_management_enabled() + form_class = NameEmailForm class AvatarSettingsPanel(BaseSettingsPanel): @@ -179,8 +169,7 @@ def account(request): # Panels panels = [ - NameSettingsPanel(request, user, profile), - EmailSettingsPanel(request, user, profile), + NameEmailSettingsPanel(request, user, profile), AvatarSettingsPanel(request, user, profile), NotificationsSettingsPanel(request, user, profile), LocaleSettingsPanel(request, user, profile), diff --git a/wagtail/users/forms.py b/wagtail/users/forms.py index 0c1b70c521..eeb1ddfaaf 100644 --- a/wagtail/users/forms.py +++ b/wagtail/users/forms.py @@ -432,21 +432,21 @@ class LocalePreferencesForm(forms.ModelForm): fields = ['preferred_language', 'current_time_zone'] -class EmailForm(forms.ModelForm): - email = forms.EmailField(required=True, label=_('Email')) - - class Meta: - model = User - fields = ("email",) - - -class NameForm(forms.ModelForm): +class NameEmailForm(forms.ModelForm): first_name = forms.CharField(required=True, label=_('First Name')) last_name = forms.CharField(required=True, label=_('Last Name')) + email = forms.EmailField(required=True, label=_('Email')) + + def __init__(self, *args, **kwargs): + from wagtail.admin.views.account import email_management_enabled + super().__init__(*args, **kwargs) + + if not email_management_enabled(): + del self.fields['email'] class Meta: model = User - fields = ("first_name", "last_name",) + fields = ['first_name', 'last_name', 'email'] class AvatarPreferencesForm(forms.ModelForm):