Skip to content

Commit

Permalink
Merge pull request #2774 from carpentries/feature/2758-bring-back-tok…
Browse files Browse the repository at this point in the history
…enized-search

feat(#2758): Bring back the old-old search
  • Loading branch information
pbanaszkiewicz authored Feb 28, 2025
2 parents bd43d70 + 16a4b9b commit 8216fec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
9 changes: 4 additions & 5 deletions amy/dashboard/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ def tokenize(term: str) -> list[str]:
return [v for v in re.split(r"\s+", term) if v]


def tokenized_multiple_Q_icontains(tokens: list[str], *args: str) -> Q:
q = Q()
for term in tokens:
for arg in args:
q |= Q(**{f"{arg}__icontains": term})
def cross_multiple_Q_icontains(token1: str, token2: str, field1: str, field2: str) -> Q:
q = (Q(**{f"{field1}__icontains": token1}) & Q(**{f"{field2}__icontains": token2})) | (
Q(**{f"{field1}__icontains": token2}) & Q(**{f"{field2}__icontains": token1})
)
return q


Expand Down
14 changes: 3 additions & 11 deletions amy/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
SignupForRecruitmentForm,
)
from dashboard.utils import (
cross_multiple_Q_icontains,
get_passed_or_last_progress,
multiple_Q_icontains,
tokenize,
tokenized_multiple_Q_icontains,
)
from emails.signals import instructor_signs_up_for_workshop_signal
from extrequests.base_views import AMYCreateAndFetchObjectView
Expand Down Expand Up @@ -603,11 +603,7 @@ def search(request: HttpRequest) -> HttpResponse:

persons = Person.objects.filter(
multiple_Q_icontains(term, "personal", "middle", "family", "email", "secondary_email", "github")
| (
tokenized_multiple_Q_icontains(tokens[0:3], "personal", "middle", "family")
if 1 < len(tokens) <= 3
else Q()
)
| (cross_multiple_Q_icontains(tokens[0], tokens[1], "personal", "family") if len(tokens) == 2 else Q())
).order_by("family")
results_combined += list(persons)

Expand All @@ -628,11 +624,7 @@ def search(request: HttpRequest) -> HttpResponse:
"location",
"user_notes",
)
| (
tokenized_multiple_Q_icontains(tokens[0:3], "personal", "middle", "family")
if 1 < len(tokens) <= 3
else Q()
)
| (cross_multiple_Q_icontains(tokens[0], tokens[1], "personal", "family") if len(tokens) == 2 else Q())
).order_by("family")
results_combined += list(training_requests)

Expand Down

0 comments on commit 8216fec

Please sign in to comment.