0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Convert time zone preferences into a settings panel

This commit is contained in:
Karl Hobley 2021-01-16 21:45:56 +00:00 committed by Matt Westcott
parent 64f0543df6
commit 662bb6e2c3
5 changed files with 27 additions and 85 deletions

View File

@ -1,20 +0,0 @@
{% extends "wagtailadmin/base.html" %}
{% load i18n %}
{% block titletag %}{% trans "Set Time Zone" %}{% endblock %}
{% block content %}
{% trans "Set Time Zone" as prefs_str %}
{% include "wagtailadmin/shared/header.html" with title=prefs_str %}
<div class="nice-padding">
<form action="{% url 'wagtailadmin_account_current_time_zone' %}" method="POST" novalidate>
{% csrf_token %}
<ul class="fields">
{% for field in form %}
{% include "wagtailadmin/shared/field_as_li.html" with field=field %}
{% endfor %}
<li class="submit"><input type="submit" value="{% trans 'Update' %}" class="button" /></li>
</ul>
</form>
</div>
{% endblock %}

View File

@ -198,6 +198,7 @@ class TestAccountSectionUtilsMixin:
'notifications-rejected_notifications': 'true', 'notifications-rejected_notifications': 'true',
'notifications-updated_comments_notifications': 'true', 'notifications-updated_comments_notifications': 'true',
'language-preferred_language': 'es', 'language-preferred_language': 'es',
'time-zone-current_time_zone': 'Europe/London',
} }
post_data.update(extra_post_data) post_data.update(extra_post_data)
return self.client.post(reverse('wagtailadmin_account'), post_data) return self.client.post(reverse('wagtailadmin_account'), post_data)
@ -225,6 +226,7 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi
self.assertPanelActive(response, 'email') self.assertPanelActive(response, 'email')
self.assertPanelActive(response, 'notifications') self.assertPanelActive(response, 'notifications')
self.assertPanelActive(response, 'language') self.assertPanelActive(response, 'language')
self.assertPanelActive(response, 'time-zone')
# Page should contain a 'Change password' option # Page should contain a 'Change password' option
self.assertContains(response, "Change password") self.assertContains(response, "Change password")
@ -424,50 +426,30 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi
response = self.client.get(reverse('wagtailadmin_account')) response = self.client.get(reverse('wagtailadmin_account'))
self.assertPanelNotActive(response, 'language') self.assertPanelNotActive(response, 'language')
def test_current_time_zone_view(self): def test_change_current_time_zone(self):
""" response = self.post_form({
This tests that the current time zone view responds with an index page 'time-zone-current_time_zone': 'Pacific/Fiji',
""" })
# Get account page
response = self.client.get(reverse('wagtailadmin_account_current_time_zone'))
# Check that the user received an account page
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailadmin/account/current_time_zone.html')
# Page should contain a 'Set Time Zone' title
self.assertContains(response, "Set Time Zone")
def test_current_time_zone_view_post(self):
"""
This posts to the current time zone view and checks that the
user profile is updated
"""
# Post new values to the current time zone page
post_data = {
'current_time_zone': 'Pacific/Fiji'
}
response = self.client.post(reverse('wagtailadmin_account_current_time_zone'), post_data)
# Check that the user was redirected to the account page # Check that the user was redirected to the account page
self.assertRedirects(response, reverse('wagtailadmin_account')) self.assertRedirects(response, reverse('wagtailadmin_account'))
profile = UserProfile.get_for_user(get_user_model().objects.get(pk=self.user.pk)) profile = UserProfile.get_for_user(self.user)
profile.refresh_from_db()
# Check that the current time zone is stored # Check that the current time zone is stored
self.assertEqual(profile.current_time_zone, 'Pacific/Fiji') self.assertEqual(profile.current_time_zone, 'Pacific/Fiji')
def test_unset_current_time_zone(self): def test_unset_current_time_zone(self):
# Post new values to the current time zone page response = self.post_form({
post_data = { 'time-zone-current_time_zone': '',
'current_time_zone': '' })
}
response = self.client.post(reverse('wagtailadmin_account_current_time_zone'), post_data)
# Check that the user was redirected to the account page # Check that the user was redirected to the account page
self.assertRedirects(response, reverse('wagtailadmin_account')) self.assertRedirects(response, reverse('wagtailadmin_account'))
profile = UserProfile.get_for_user(get_user_model().objects.get(pk=self.user.pk)) profile = UserProfile.get_for_user(self.user)
profile.refresh_from_db()
# Check that the current time zone are stored # Check that the current time zone are stored
self.assertEqual(profile.current_time_zone, '') self.assertEqual(profile.current_time_zone, '')
@ -483,7 +465,7 @@ class TestAccountSection(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixi
@override_settings(WAGTAIL_USER_TIME_ZONES=['Europe/London']) @override_settings(WAGTAIL_USER_TIME_ZONES=['Europe/London'])
def test_not_show_options_if_only_one_time_zone_is_permitted(self): def test_not_show_options_if_only_one_time_zone_is_permitted(self):
response = self.client.get(reverse('wagtailadmin_account')) response = self.client.get(reverse('wagtailadmin_account'))
self.assertNotContains(response, 'Set Time Zone') self.assertPanelNotActive(response, 'time-zone')
class TestAccountUploadAvatar(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixin): class TestAccountUploadAvatar(TestCase, WagtailTestUtils, TestAccountSectionUtilsMixin):

View File

@ -54,11 +54,6 @@ urlpatterns = [
path('account/', account.account, name='wagtailadmin_account'), path('account/', account.account, name='wagtailadmin_account'),
path('account/change_password/', account.change_password, name='wagtailadmin_account_change_password'), path('account/change_password/', account.change_password, name='wagtailadmin_account_change_password'),
path(
'account/current_time_zone/',
account.current_time_zone,
name='wagtailadmin_account_current_time_zone'
),
path('logout/', account.LogoutView.as_view(), name='wagtailadmin_logout'), path('logout/', account.LogoutView.as_view(), name='wagtailadmin_logout'),
] ]

View File

@ -14,7 +14,7 @@ from django.utils.translation import gettext_lazy, override
from django.views.decorators.debug import sensitive_post_parameters from django.views.decorators.debug import sensitive_post_parameters
from wagtail.admin.forms.auth import LoginForm, PasswordResetForm from wagtail.admin.forms.auth import LoginForm, PasswordResetForm
from wagtail.admin.localization import get_available_admin_languages from wagtail.admin.localization import get_available_admin_languages, get_available_admin_time_zones
from wagtail.core import hooks from wagtail.core import hooks
from wagtail.core.models import UserPagePermissionsProxy from wagtail.core.models import UserPagePermissionsProxy
from wagtail.users.forms import ( from wagtail.users.forms import (
@ -152,6 +152,17 @@ class LanguageSettingsPanel(BaseSettingsPanel):
return len(get_available_admin_languages()) > 1 return len(get_available_admin_languages()) > 1
class TimeZoneSettingsPanel(BaseSettingsPanel):
name = 'time-zone'
title = gettext_lazy('Time Zone')
order = 500
form_class = CurrentTimeZoneForm
form_object = 'profile'
def is_active(self):
return len(get_available_admin_time_zones()) > 1
# Views # Views
def account(request): def account(request):
@ -167,6 +178,7 @@ def account(request):
AvatarSettingsPanel(request, user, profile), AvatarSettingsPanel(request, user, profile),
NotificationsSettingsPanel(request, user, profile), NotificationsSettingsPanel(request, user, profile),
LanguageSettingsPanel(request, user, profile), LanguageSettingsPanel(request, user, profile),
TimeZoneSettingsPanel(request, user, profile),
] ]
for fn in hooks.get_hooks('register_account_settings_panel'): for fn in hooks.get_hooks('register_account_settings_panel'):
panel = fn(request, user, profile) panel = fn(request, user, profile)
@ -267,22 +279,6 @@ class PasswordResetCompleteView(PasswordResetEnabledViewMixin, auth_views.Passwo
template_name = 'wagtailadmin/account/password_reset/complete.html' template_name = 'wagtailadmin/account/password_reset/complete.html'
def current_time_zone(request):
if request.method == 'POST':
form = CurrentTimeZoneForm(request.POST, instance=UserProfile.get_for_user(request.user))
if form.is_valid():
form.save()
messages.success(request, _("Your preferences have been updated."))
return redirect('wagtailadmin_account')
else:
form = CurrentTimeZoneForm(instance=UserProfile.get_for_user(request.user))
return TemplateResponse(request, 'wagtailadmin/account/current_time_zone.html', {
'form': form,
})
class LoginView(auth_views.LoginView): class LoginView(auth_views.LoginView):
template_name = 'wagtailadmin/login.html' template_name = 'wagtailadmin/login.html'

View File

@ -8,7 +8,6 @@ from draftjs_exporter.dom import DOM
import wagtail.admin.rich_text.editors.draftail.features as draftail_features import wagtail.admin.rich_text.editors.draftail.features as draftail_features
from wagtail.admin.auth import user_has_any_page_permission from wagtail.admin.auth import user_has_any_page_permission
from wagtail.admin.localization import get_available_admin_time_zones
from wagtail.admin.menu import MenuItem, SubmenuMenuItem, reports_menu, settings_menu from wagtail.admin.menu import MenuItem, SubmenuMenuItem, reports_menu, settings_menu
from wagtail.admin.navigation import get_explorable_root_page from wagtail.admin.navigation import get_explorable_root_page
from wagtail.admin.rich_text import ( from wagtail.admin.rich_text import (
@ -258,16 +257,6 @@ def register_account_change_password(request):
} }
@hooks.register('register_account_menu_item')
def register_account_current_time_zone(request):
if len(get_available_admin_time_zones()) > 1:
return {
'url': reverse('wagtailadmin_account_current_time_zone'),
'label': _('Current Time Zone'),
'help_text': _('Choose your current time zone.'),
}
@hooks.register('register_rich_text_features') @hooks.register('register_rich_text_features')
def register_core_features(features): def register_core_features(features):
# Hallo.js # Hallo.js