Added change email functionality from the account settings. Fix #4325 (#4375)

pull/4315/merge
alejandrogarza 2018-04-02 14:42:46 +02:00 zatwierdzone przez Thibaud Colas
rodzic 3939397850
commit 5a5e6d5d2b
9 zmienionych plików z 101 dodań i 1 usunięć

Wyświetl plik

@ -16,6 +16,7 @@ Changelog
* Added error notification when running the `wagtail` command on Python <3.4 (Matt Westcott)
* `update_index` management command now accepts a `--chunk_size` option to determine the number of items to load at once (Dave Bell)
* Added hook `register_account_menu_item` to add new account preference items (Michael van Tellingen)
* Added change email functionality from the account settings (Alejandro Garza, Alexs Mathilda)
* Fix: Status button on 'edit page' now links to the correct URL when live and draft slug differ (LB (Ben Johnston))
* Fix: Image title text in the gallery and in the chooser now wraps for long filenames (LB (Ben Johnston), Luiz Boaretto)
* Fix: Move image editor action buttons to the bottom of the form on mobile (Julian Gallo)

Wyświetl plik

@ -288,6 +288,7 @@ Contributors
* Tony Yates
* Mike Kamermans
* Arthur Holzner
* Alejandro Garza
Translators
===========

Wyświetl plik

@ -30,6 +30,7 @@ Other features
* Added error notification when running the ``wagtail`` command on Python <3.4 (Matt Westcott)
* ``update_index`` management command now accepts a ``--chunk_size`` option to determine the number of items to load at once (Dave Bell)
* Added hook `register_account_menu_item` to add new account preference items (Michael van Tellingen)
* Added change email functionality from the account settings (Alejandro Garza, Alexs Mathilda)
Bug fixes
~~~~~~~~~

Wyświetl plik

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

Wyświetl plik

@ -190,6 +190,49 @@ class TestAccountSection(TestCase, WagtailTestUtils):
# Page should contain a 'Change password' option
self.assertContains(response, "Change password")
def test_change_email_view(self):
"""
This tests that the change email view responds with a change email page
"""
# Get change email page
response = self.client.get(reverse('wagtailadmin_account_change_email'))
# Check that the user received a change email page
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailadmin/account/change_email.html')
def test_change_email_post(self):
post_data = {
'email': 'test@email.com'
}
response = self.client.post(reverse('wagtailadmin_account_change_email'), post_data)
# Check that the user was redirected to the account page
self.assertRedirects(response, reverse('wagtailadmin_account'))
# Check that the email was changed
self.assertEqual(get_user_model().objects.get(pk=self.user.pk).email, post_data['email'])
def test_change_email_not_valid(self):
post_data = {
'email': 'test@email'
}
response = self.client.post(reverse('wagtailadmin_account_change_email'), post_data)
# 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['form'].errors.keys())
# Check that the password was not changed
self.assertNotEqual(get_user_model().objects.get(pk=self.user.pk).email, post_data['email'])
@override_settings(WAGTAIL_PASSWORD_MANAGEMENT_ENABLED=False)
def test_account_view_with_password_management_disabled(self):
# Get account page

Wyświetl plik

@ -44,6 +44,7 @@ urlpatterns = [
url(r'^account/$', account.account, name='wagtailadmin_account'),
url(r'^account/change_password/$', account.change_password, name='wagtailadmin_account_change_password'),
url(r'^account/change_email/$', account.change_email, name='wagtailadmin_account_change_email'),
url(
r'^account/notification_preferences/$',
account.notification_preferences,

Wyświetl plik

@ -14,7 +14,7 @@ from django.views.decorators.debug import sensitive_post_parameters
from wagtail.admin import forms
from wagtail.core import hooks
from wagtail.users.forms import NotificationPreferencesForm, PreferredLanguageForm
from wagtail.users.forms import EmailForm, NotificationPreferencesForm, PreferredLanguageForm
from wagtail.users.models import UserProfile
from wagtail.utils.loading import get_custom_form
@ -81,6 +81,22 @@ def change_password(request):
})
def change_email(request):
if request.method == 'POST':
form = EmailForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
messages.success(request, _("Your email has been changed successfully!"))
return redirect('wagtailadmin_account')
else:
form = EmailForm(instance=request.user)
return render(request, 'wagtailadmin/account/change_email.html', {
'form': form,
})
def _wrap_password_reset_view(view_func):
@wraps(view_func)
def wrapper(*args, **kwargs):

Wyświetl plik

@ -204,6 +204,15 @@ def register_account_set_gravatar(request):
}
@hooks.register('register_account_menu_item')
def register_account_change_email(request):
return {
'url': reverse('wagtailadmin_account_change_email'),
'label': _('Change email'),
'help_text': _('Change the email address linked to your account.'),
}
@hooks.register('register_account_menu_item')
def register_account_change_password(request):
if password_management_enabled() and request.user.has_usable_password():

Wyświetl plik

@ -391,3 +391,11 @@ class PreferredLanguageForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ("preferred_language",)
class EmailForm(forms.ModelForm):
email = forms.EmailField(required=True, label=_('Email'))
class Meta:
model = User
fields = ("email", )