Skip to content

Commit

Permalink
Merge pull request #184 from Teknologforeningen/feature/enhance-admin…
Browse files Browse the repository at this point in the history
…-search

Feature/enhance admin search
  • Loading branch information
filiptypjeu authored Dec 5, 2023
2 parents b8fa259 + 2e0cfb6 commit dd6b36a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
17 changes: 0 additions & 17 deletions teknologr/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,9 @@
from rest_framework.renderers import BrowsableAPIRenderer
from rest_framework.pagination import LimitOffsetPagination
from members.models import Member
from registration.models import Applicant
from datetime import datetime
from rest_framework.response import Response

def findMembers(query, count=50):
members = Member.objects.search_by_name(query.split(), True)[:count]
Member.order_by(members, 'name')
return members

def findApplicants(query, count=50):
args = []

for word in query.split():
args.append(Q(given_names__icontains=word) | Q(surname__icontains=word))

if not args:
return []

return Applicant.objects.filter(*args).order_by('surname', 'given_names')[:count]


def create_dump_response(content, name, filetype):
dumpname = f'filename="{name}_{datetime.today().strftime("%Y-%m-%d_%H-%M-%S")}.{filetype}'
Expand Down
15 changes: 12 additions & 3 deletions teknologr/members/lookups.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ajax_select import register, LookupChannel
from members.models import Member
from registration.models import Applicant
from api.utils import findMembers, findApplicants
from django.db.models import Q


@register('member')
Expand All @@ -11,7 +11,10 @@ class MemberLookup(LookupChannel):
def get_query(self, q, request):
if q == '__ALL__':
return Member.objects.order_by('-modified')[:50]
return findMembers(q)

members = Member.objects.search_by_name(q.split(), True)
Member.order_by(members, 'name')
return members[:50]

def get_result(self, obj):
""" result is the simple text that is the completion of what the person typed """
Expand All @@ -31,7 +34,13 @@ class ApplicantLookup(LookupChannel):
model = Applicant

def get_query(self, q, request):
return findApplicants(q)
if not q:
return []

queries = [q.lower() for q in queries]
filters = [(Q(given_names__icontains=q) | Q(surname__icontains=q)) for q in queries]

return Applicant.objects.filter(*filters).order_by('surname', 'given_names')[:50]

def get_result(self, obj):
return obj.full_name
Expand Down
28 changes: 20 additions & 8 deletions teknologr/members/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from django.shortcuts import get_object_or_404
from django.utils.html import format_html
from locale import strxfrm
from operator import attrgetter, and_
from functools import reduce
from operator import attrgetter
from datetime import date
from katalogen.utils import *
from members.utils import *
Expand Down Expand Up @@ -46,19 +45,32 @@ def all_with_related(self):
def get_prefetched_or_404(self, member_id):
return get_object_or_404(self.all_with_related(), id=member_id)

def search_by_name(self, queries, include_hidden=False):
def search_by_name(self, queries, staff_search=False):
if not queries:
return []

# XXX: Would be nice to be able to filter on preferred_name on hidden users, but that is not always set in the database
queries = [q.lower() for q in queries]
filters = [(Q(given_names__icontains=q) | Q(surname__icontains=q)) for q in queries]
members = self.filter(reduce(and_, filters))
if staff_search:
# Search within comments too since that can include old names or nicknames
filters = [(
Q(given_names__icontains=q) |
Q(preferred_name__icontains=q) |
Q(surname__icontains=q) |
Q(comment__icontains=q) |
Q(email__icontains=q)
) for q in queries]
else:
filters = [(
Q(given_names__icontains=q) |
Q(preferred_name__icontains=q) |
Q(surname__icontains=q)
) for q in queries]
members = self.filter(*filters)
members = list(members)

# Need to remove hidden Members that were matched on a non-preferred given name
if not include_hidden:
members = [m for m in members if m.allow_publish_info or any([q in m.surname.lower() or q in m.get_preferred_name().lower() for q in queries])]
if not staff_search:
members = [m for m in members if m.allow_publish_info or all([q in m.surname.lower() or q in m.get_preferred_name().lower() for q in queries])]

return members

Expand Down
8 changes: 8 additions & 0 deletions teknologr/members/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def test_str(self):
self.assertEqual('Foo Bar Baz Tester', str(self.member2))
self.assertEqual('F-B Biz-Baz von der Tester', str(self.member3))

def test_search_by_name(self):
Member.objects.create(given_names='Bar Foo', surname='von Test', allow_publish_info=False)
Member.objects.create(given_names='Test', surname='Test', allow_publish_info=False, comment="foo")
self.assertEqual(3, len(Member.objects.search_by_name(["foo"], False)))
self.assertEqual(5, len(Member.objects.search_by_name(["foo"], True)))
self.assertEqual(1, len(Member.objects.search_by_name(["foo", "von"], False)))
self.assertEqual(2, len(Member.objects.search_by_name(["foo", "von"], True)))

def test_member_type(self):
member = Member(given_names='Svatta', surname='Teknolog')
member.save()
Expand Down

0 comments on commit dd6b36a

Please sign in to comment.