Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search names with special characters #1944

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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