Skip to content

Commit

Permalink
Merge pull request #1944 from DemocracyClub/hotfix/special-character-…
Browse files Browse the repository at this point in the history
…search

Search names with special characters
  • Loading branch information
VirginiaDooley authored Feb 16, 2023
2 parents 55e2a0d + f55b139 commit f2482a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ynr/apps/search/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions ynr/apps/search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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())
Expand Down

0 comments on commit f2482a5

Please sign in to comment.