Skip to content

Commit afccaa0

Browse files
committed
Handle Person.ascii in edit profile better to try to ensure that
people fill it in correctly. Blank it out if it's unchanged from name and use unidecode to set it automatically (with a warning if it actually converts something). Branch ready for merge. - Legacy-Id: 11752
1 parent d56560e commit afccaa0

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

ietf/ietfauth/forms.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from django.utils.html import mark_safe
1010
from django.core.urlresolvers import reverse as urlreverse
1111

12+
from unidecode import unidecode
13+
1214
import debug # pyflakes:ignore
1315

1416
from ietf.person.models import Person, Email
@@ -45,7 +47,7 @@ def clean_password_confirmation(self):
4547
def ascii_cleaner(supposedly_ascii):
4648
outside_printable_ascii_pattern = r'[^\x20-\x7F]'
4749
if re.search(outside_printable_ascii_pattern, supposedly_ascii):
48-
raise forms.ValidationError("Please only enter ASCII characters.")
50+
raise forms.ValidationError("Only unaccented Latin characters are allowed.")
4951
return supposedly_ascii
5052

5153
def prevent_at_symbol(name):
@@ -69,12 +71,31 @@ class Meta:
6971
def __init__(self, *args, **kwargs):
7072
super(ModelForm, self).__init__(*args, **kwargs)
7173

74+
# blank ascii if it's the same as name
75+
self.fields["ascii"].required = self.fields["ascii"].widget.is_required = False
76+
self.fields["ascii"].help_text += " " + "Leave blank to use auto-reconstructed Latin version of name."
77+
78+
if self.initial.get("ascii") == self.initial.get("name"):
79+
self.initial["ascii"] = ""
80+
81+
self.unidecoded_ascii = False
82+
83+
if self.data and not self.data.get("ascii", "").strip():
84+
self.data = self.data.copy()
85+
name = self.data["name"]
86+
reconstructed_name = unidecode(name)
87+
self.data["ascii"] = reconstructed_name
88+
self.unidecoded_ascii = name != reconstructed_name
89+
7290
def clean_name(self):
7391
name = self.cleaned_data.get("name") or u""
7492
prevent_at_symbol(name)
7593
return name
7694

7795
def clean_ascii(self):
96+
if self.unidecoded_ascii:
97+
raise forms.ValidationError("Name contained non-ASCII characters, and was automatically reconstructed using only Latin characters. Check the result - if you are happy, just hit Submit again.")
98+
7899
name = self.cleaned_data.get("ascii") or u""
79100
prevent_at_symbol(name)
80101
return ascii_cleaner(name)

ietf/ietfauth/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ def test_profile(self):
213213
q = PyQuery(r.content)
214214
self.assertTrue(len(q("form .has-error")) > 0)
215215

216+
# edit details - blank ASCII
217+
blank_ascii = base_data.copy()
218+
blank_ascii["ascii"] = u""
219+
r = self.client.post(url, blank_ascii)
220+
self.assertEqual(r.status_code, 200)
221+
q = PyQuery(r.content)
222+
self.assertTrue(len(q("form .has-error")) > 0) # we get a warning about reconstructed name
223+
self.assertEqual(q("input[name=ascii]").val(), base_data["ascii"])
224+
216225
# edit details
217226
r = self.client.post(url, base_data)
218227
self.assertEqual(r.status_code, 200)

0 commit comments

Comments
 (0)