diff --git a/ynr/apps/search/tests/test_search.py b/ynr/apps/search/tests/test_search.py index 149c42252..7af377f80 100644 --- a/ynr/apps/search/tests/test_search.py +++ b/ynr/apps/search/tests/test_search.py @@ -145,3 +145,11 @@ def test_backtick_regression(self): "` ' £$$^* ($ £% Henry " "Jekyll \t" ).exists() ) + + def test_search_person_by_name_with_special_characters(self): + name = "Z$^o@&ë%" + person = PersonFactory(name=name) + person.save() + qs = search_person_by_name(name=person.name) + self.assertEqual(qs.count(), 1) + self.assertEqual(qs.first().name, person.name) diff --git a/ynr/apps/search/utils.py b/ynr/apps/search/utils.py index d67ff89f7..ecf2dcadd 100644 --- a/ynr/apps/search/utils.py +++ b/ynr/apps/search/utils.py @@ -8,6 +8,8 @@ from people.models import Person from popolo.models import Membership +import unicodedata + def search_person_by_name(name: str, synonym: bool = False) -> PersonQuerySet: """ @@ -22,6 +24,11 @@ def search_person_by_name(name: str, synonym: bool = False) -> PersonQuerySet: see the comment in line about a workaround for a performance bug. """ + name = ( + unicodedata.normalize("NFKD", name) + .encode("ascii", "ignore") + .decode("ascii") + ) name = name.lower() name = re.sub(r"[^a-z ]", " ", name) name = " ".join(name.strip().split())