Merge ~newell-jensen/maas:lp1727419 into maas:master

Proposed by Newell Jensen
Status: Rejected
Rejected by: Blake Rouse
Proposed branch: ~newell-jensen/maas:lp1727419
Merge into: maas:master
Diff against target: 136 lines (+53/-10)
4 files modified
src/maasserver/api/zones.py (+1/-1)
src/maasserver/forms/__init__.py (+37/-3)
src/maasserver/forms/tests/test_user.py (+3/-5)
src/maasserver/forms/tests/test_zone.py (+12/-1)
Reviewer Review Type Date Requested Status
MAAS Lander Approve
Lee Trager (community) Needs Information
Andres Rodriguez (community) Needs Information
Review via email: mp+367595@code.launchpad.net

Commit message

LP: #1727419 -- Uncapitalize letters in middle of error message sentences.

To post a comment you must log in.
Revision history for this message
Andres Rodriguez (andreserl) wrote :

To what messages does it apply? Only E-mail?

review: Needs Information
~newell-jensen/maas:lp1727419 updated
05a8a62... by Newell Jensen

Add unit test showing that when trying to add zone with the same name as an already existing one, that the error message has lower case words in rest of the sentence.

Revision history for this message
Newell Jensen (newell-jensen) wrote :

> To what messages does it apply? Only E-mail?

It applies to all forms that inherit from MAASModelForm. I added a unit test showing that when trying to add a zone with the same name as an already existing one, that the error message has lower case words in rest of the sentence.

Revision history for this message
Lee Trager (ltrager) wrote :

Couple of questions below

review: Needs Information
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp1727419 lp:~newell-jensen/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 05a8a62a8b07a3bf70280d94d1d66251a9393e27

review: Approve

Unmerged commits

05a8a62... by Newell Jensen

Add unit test showing that when trying to add zone with the same name as an already existing one, that the error message has lower case words in rest of the sentence.

4071ceb... by Newell Jensen

Update unit test.

971e135... by Newell Jensen

LP: #1727419 -- Uncapitalize letters in middle of error message sentences.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/api/zones.py b/src/maasserver/api/zones.py
2index 6d56264..c233d01 100644
3--- a/src/maasserver/api/zones.py
4+++ b/src/maasserver/api/zones.py
5@@ -159,7 +159,7 @@ class ZonesHandler(ModelCollectionOperationsHandler):
6 @error (http-status-code) "400" 400
7 @error (content) "alreadyexists" The zone already exists
8 @error-example "alreadyexists"
9- {"name": ["Physical zone with this Name already exists."]}
10+ {"name": ["Physical zone with this name already exists."]}
11 """
12 return super().create(request)
13
14diff --git a/src/maasserver/forms/__init__.py b/src/maasserver/forms/__init__.py
15index 2341511..659655e 100644
16--- a/src/maasserver/forms/__init__.py
17+++ b/src/maasserver/forms/__init__.py
18@@ -445,10 +445,23 @@ class MAASModelForm(
19
20 def _update_errors(self, errors):
21 """Provide Django 1.11-like behaviour in 1.8 as well."""
22+ # Sometimes Django capitalizes the field name while it is in
23+ # the middle of the error messages. As these errors are
24+ # surfaced to the UI, we want well formed sentences.
25 if hasattr(errors, 'error_dict'):
26- error_dict = errors
27+ error_dict = errors.error_dict
28+ for field, validation_errors in error_dict.items():
29+ new_messages = []
30+ for messages in validation_errors:
31+ for message in messages:
32+ if field.capitalize() in message:
33+ new_messages.append(
34+ message.replace(field.capitalize(), field))
35+ else:
36+ new_messages.append(message)
37+ error_dict[field] = new_messages
38 else:
39- error_dict = ValidationError({NON_FIELD_ERRORS: errors})
40+ error_dict = {NON_FIELD_ERRORS: errors}
41 super(MAASModelForm, self)._update_errors(error_dict)
42
43 def use_perms(self):
44@@ -1462,7 +1475,7 @@ class NewUserCreationForm(UserCreationForm):
45 email = self.cleaned_data['email']
46 if User.objects.filter(email__iexact=email).exists():
47 raise forms.ValidationError(
48- "User with this E-mail address already exists.")
49+ "User with this email address already exists.")
50 return email
51
52
53@@ -1486,6 +1499,27 @@ class EditUserForm(UserChangeForm):
54 if 'password' in self.fields:
55 del self.fields['password']
56
57+ def _update_errors(self, errors):
58+ """Provide Django 1.11-like behaviour in 1.8 as well."""
59+ # Sometimes Django capitalizes the field name while it is in
60+ # the middle of the error messages. As these errors are
61+ # surfaced to the UI, we want well formed sentences.
62+ if hasattr(errors, 'error_dict'):
63+ error_dict = errors.error_dict
64+ for field, validation_errors in error_dict.items():
65+ new_messages = []
66+ for messages in validation_errors:
67+ for message in messages:
68+ if field.capitalize() in message:
69+ new_messages.append(
70+ message.replace(field.capitalize(), field))
71+ else:
72+ new_messages.append(message)
73+ error_dict[field] = new_messages
74+ else:
75+ error_dict = {NON_FIELD_ERRORS: errors}
76+ super(EditUserForm, self)._update_errors(error_dict)
77+
78
79 class DeleteUserForm(Form):
80 """A form to remove a user."""
81diff --git a/src/maasserver/forms/tests/test_user.py b/src/maasserver/forms/tests/test_user.py
82index 3e08f7a..721deeb 100644
83--- a/src/maasserver/forms/tests/test_user.py
84+++ b/src/maasserver/forms/tests/test_user.py
85@@ -1,4 +1,4 @@
86-# Copyright 2014-2015 Canonical Ltd. This software is licensed under the
87+# Copyright 2014-2019 Canonical Ltd. This software is licensed under the
88 # GNU Affero General Public License version 3 (see the file LICENSE).
89
90 """Tests for user-creation forms."""
91@@ -14,7 +14,6 @@ from maasserver.forms import (
92 from maasserver.models import Config
93 from maasserver.testing.factory import factory
94 from maasserver.testing.testcase import MAASServerTestCase
95-from testtools.matchers import MatchesRegex
96
97
98 class TestUniqueEmailForms(MAASServerTestCase):
99@@ -24,10 +23,9 @@ class TestUniqueEmailForms(MAASServerTestCase):
100 self.assertIn('email', form._errors)
101 self.assertEqual(1, len(form._errors['email']))
102 # Cope with 'Email' and 'E-mail' in error message.
103- self.assertThat(
104+ self.assertEqual(
105 form._errors['email'][0],
106- MatchesRegex(
107- r'User with this E-{0,1}mail address already exists.'))
108+ 'User with this email address already exists.')
109
110 def test_ProfileForm_fails_validation_if_email_taken(self):
111 another_email = '%s@example.com' % factory.make_string()
112diff --git a/src/maasserver/forms/tests/test_zone.py b/src/maasserver/forms/tests/test_zone.py
113index 9e93239..efd3464 100644
114--- a/src/maasserver/forms/tests/test_zone.py
115+++ b/src/maasserver/forms/tests/test_zone.py
116@@ -1,4 +1,4 @@
117-# Copyright 2014-2016 Canonical Ltd. This software is licensed under the
118+# Copyright 2014-2019 Canonical Ltd. This software is licensed under the
119 # GNU Affero General Public License version 3 (see the file LICENSE).
120
121 """Tests for `ZoneForm`."""
122@@ -56,3 +56,14 @@ class TestZoneForm(MAASServerTestCase):
123 data={'name': factory.make_name('zone')},
124 instance=zone)
125 self.assertTrue(form.is_valid())
126+
127+ def test_zone_create_errors_with_lowercase_words_in_middle(self):
128+ name = factory.make_name('zone')
129+ form = ZoneForm(data={'name': name})
130+ form.save()
131+ self.assertTrue(form.is_valid(), form._errors)
132+ new_form = ZoneForm(data={'name': name})
133+ new_form.is_valid()
134+ self.assertEquals(
135+ new_form._errors['name'][0],
136+ 'Physical zone with this name already exists.')

Subscribers

People subscribed via source and target branches