Skip to content

Commit e97876a

Browse files
committed
Merged in ^/branch/iola/account-ascii-fixes-r11748 from olau@iola.dk:
- Added a check for a reserved name in account registration - 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). - Throw a validation error if a profile name contains an @ sign upon edit so that people do not forget to enter their name. - Legacy-Id: 11791
2 parents 1dcf7b1 + c4e2f68 commit e97876a

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

ietf/ietfauth/forms.py

Lines changed: 45 additions & 3 deletions
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,9 +47,18 @@ 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

53+
def prevent_at_symbol(name):
54+
if "@" in name:
55+
raise forms.ValidationError("Please fill in name - this looks like an email address (@ is not allowed in names).")
56+
57+
def prevent_system_name(name):
58+
name_without_spaces = name.replace(" ", "").replace("\t", "")
59+
if "(system)" in name_without_spaces.lower():
60+
raise forms.ValidationError("Please pick another name - this name is reserved.")
61+
5162
def get_person_form(*args, **kwargs):
5263

5364
exclude_list = ['time', 'user', 'photo_thumb', 'photo', ]
@@ -65,11 +76,42 @@ class Meta:
6576
def __init__(self, *args, **kwargs):
6677
super(ModelForm, self).__init__(*args, **kwargs)
6778

79+
# blank ascii if it's the same as name
80+
self.fields["ascii"].required = self.fields["ascii"].widget.is_required = False
81+
self.fields["ascii"].help_text += " " + "Leave blank to use auto-reconstructed Latin version of name."
82+
83+
if self.initial.get("ascii") == self.initial.get("name"):
84+
self.initial["ascii"] = ""
85+
86+
self.unidecoded_ascii = False
87+
88+
if self.data and not self.data.get("ascii", "").strip():
89+
self.data = self.data.copy()
90+
name = self.data["name"]
91+
reconstructed_name = unidecode(name)
92+
self.data["ascii"] = reconstructed_name
93+
self.unidecoded_ascii = name != reconstructed_name
94+
95+
def clean_name(self):
96+
name = self.cleaned_data.get("name") or u""
97+
prevent_at_symbol(name)
98+
prevent_system_name(name)
99+
return name
100+
68101
def clean_ascii(self):
69-
return ascii_cleaner(self.cleaned_data.get("ascii") or u"")
102+
if self.unidecoded_ascii:
103+
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.")
104+
105+
name = self.cleaned_data.get("ascii") or u""
106+
prevent_at_symbol(name)
107+
prevent_system_name(name)
108+
return ascii_cleaner(name)
70109

71110
def clean_ascii_short(self):
72-
return ascii_cleaner(self.cleaned_data.get("ascii_short") or u"")
111+
name = self.cleaned_data.get("ascii_short") or u""
112+
prevent_at_symbol(name)
113+
prevent_system_name(name)
114+
return ascii_cleaner(name)
73115

74116
return PersonForm(*args, **kwargs)
75117

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)