Skip to content

Commit

Permalink
Prioritize related institutions in autocomplete.
Browse files Browse the repository at this point in the history
The autocomplete endpoint accepts an optional `case` parameter.
The backend will put institutions related to `case` on top of the
suggestions list.
  • Loading branch information
rwakulszowa committed Jun 17, 2021
1 parent e0f380e commit 37927f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
18 changes: 18 additions & 0 deletions backend-project/small_eod/autocomplete/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ def validate_item(self, item):
self.assertEqual(item["id"], self.obj.id)
self.assertEqual(item["name"], self.obj.name)

def test_suggests_related(self):
institution_a = InstitutionFactory(name="institution_a")
institution_b = InstitutionFactory(name="institution_b")
InstitutionFactory(name="institution_c")

# Make the case audit the 2nd institution - it's unlikely to appear
# at the top of the results just by coincidence.
case = CaseFactory(audited_institutions=[institution_b])

# Match all - the related institution should appear at the top.
resp = self.get_response_for_query("institution", case=case.id)
self.assertEqual(resp.data['results'][0]['id'], institution_b.id)

# Match a specific, non-related item.
# The related institution should not be present.
resp = self.get_response_for_query("institution_a", case=case.id)
self.assertEqual([r['id'] for r in resp.data['results']], [institution_a.id])


class TagAutocompleteViewSetTestCase(ReadOnlyViewSetMixin, SearchQueryMixin, TestCase):
basename = "autocomplete_tag"
Expand Down
19 changes: 17 additions & 2 deletions backend-project/small_eod/autocomplete/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django_filters.rest_framework import DjangoFilterBackend
from django.db import models
from rest_framework import viewsets

from ..administrative_units.filterset import AdministrativeUnitFilterSet
Expand Down Expand Up @@ -32,7 +33,6 @@
UserAutocompleteSerializer,
)


class AdministrativeUnitAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):
queryset = AdministrativeUnit.objects.only("id", "name").all()
serializer_class = AdministrativeUnitAutocompleteSerializer
Expand Down Expand Up @@ -83,11 +83,26 @@ class FeatureOptionAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):


class InstitutionAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Institution.objects.only("id", "name").all()
serializer_class = InstitutionAutocompleteSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = InstitutionFilterSet

def get_queryset(self):
req = self.request
related_case_id = req.GET.get('case', None)
if related_case_id is None:
qs = Institution.objects.all()
else:
# Put related institutions at the beginning of the list.
qs = Institution.objects.annotate(
related=models.Case(
models.When(case=related_case_id, then=True),
default=False,
output_field=models.BooleanField()
)
).order_by('-related')
return qs.only("id", "name")


class TagAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Tag.objects.only("id", "name").all()
Expand Down
4 changes: 2 additions & 2 deletions backend-project/small_eod/search/tests/mixins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class SearchQueryMixin:
def get_response_for_query(self, query):
def get_response_for_query(self, query, **data):
self.login_required()
return self.client.get(
self.get_url(name="list", **self.get_extra_kwargs()),
data={"query": query},
data={"query": query, **data},
)

def assertResultEqual(self, response, items):
Expand Down

0 comments on commit 37927f9

Please sign in to comment.