Skip to content

Commit b70a668

Browse files
authored
Merge pull request #249 from abaisero/feature/improved_search
fix: generalizes player search query format
2 parents 87fceca + 78d404e commit b70a668

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

agagd/agagd_core/views/search.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import functools
2+
import operator
3+
14
from agagd_core.models import Member
25
from agagd_core.tables.search import SearchResultsTable
36
from django.db.models import F, Q
@@ -13,7 +16,7 @@ class SearchView(DetailView):
1316
search_results_template_name = "search_results.html"
1417

1518
def get(self, request):
16-
query = request.GET.get("q", "")
19+
query = request.GET.get("q", "").strip()
1720

1821
if not query:
1922
return TemplateResponse(request, self.template_name)
@@ -22,9 +25,14 @@ def get(self, request):
2225
member_id = [int(query)]
2326
return HttpResponseRedirect(reverse("players_profile", args=member_id))
2427

28+
# constructing intersection queryset from query tokens
29+
tokens = query.split()
30+
querysets = (Q(full_name__icontains=token) for token in tokens)
31+
queryset = functools.reduce(operator.and_, querysets)
32+
2533
member_table_data = (
2634
Member.objects.filter(Q(member_id=F("players__pin_player")))
27-
.filter(full_name__icontains=query)
35+
.filter(queryset)
2836
.values(
2937
"member_id",
3038
"chapter_id",

0 commit comments

Comments
 (0)