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
This commit is contained in:
Ole Laursen 2016-08-03 14:20:13 +00:00
parent d56560ed89
commit afccaa0b4d
2 changed files with 31 additions and 1 deletions

View file

@ -9,6 +9,8 @@ from django.contrib.auth.models import User
from django.utils.html import mark_safe
from django.core.urlresolvers import reverse as urlreverse
from unidecode import unidecode
import debug # pyflakes:ignore
from ietf.person.models import Person, Email
@ -45,7 +47,7 @@ class PasswordForm(forms.Form):
def ascii_cleaner(supposedly_ascii):
outside_printable_ascii_pattern = r'[^\x20-\x7F]'
if re.search(outside_printable_ascii_pattern, supposedly_ascii):
raise forms.ValidationError("Please only enter ASCII characters.")
raise forms.ValidationError("Only unaccented Latin characters are allowed.")
return supposedly_ascii
def prevent_at_symbol(name):
@ -69,12 +71,31 @@ def get_person_form(*args, **kwargs):
def __init__(self, *args, **kwargs):
super(ModelForm, self).__init__(*args, **kwargs)
# blank ascii if it's the same as name
self.fields["ascii"].required = self.fields["ascii"].widget.is_required = False
self.fields["ascii"].help_text += " " + "Leave blank to use auto-reconstructed Latin version of name."
if self.initial.get("ascii") == self.initial.get("name"):
self.initial["ascii"] = ""
self.unidecoded_ascii = False
if self.data and not self.data.get("ascii", "").strip():
self.data = self.data.copy()
name = self.data["name"]
reconstructed_name = unidecode(name)
self.data["ascii"] = reconstructed_name
self.unidecoded_ascii = name != reconstructed_name
def clean_name(self):
name = self.cleaned_data.get("name") or u""
prevent_at_symbol(name)
return name
def clean_ascii(self):
if self.unidecoded_ascii:
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.")
name = self.cleaned_data.get("ascii") or u""
prevent_at_symbol(name)
return ascii_cleaner(name)

View file

@ -213,6 +213,15 @@ class IetfAuthTests(TestCase):
q = PyQuery(r.content)
self.assertTrue(len(q("form .has-error")) > 0)
# edit details - blank ASCII
blank_ascii = base_data.copy()
blank_ascii["ascii"] = u""
r = self.client.post(url, blank_ascii)
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
self.assertTrue(len(q("form .has-error")) > 0) # we get a warning about reconstructed name
self.assertEqual(q("input[name=ascii]").val(), base_data["ascii"])
# edit details
r = self.client.post(url, base_data)
self.assertEqual(r.status_code, 200)