Skip to content

Commit

Permalink
ReferenceNumber autocomplete backend support.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwakulszowa committed Jul 27, 2021
1 parent 2cffe82 commit 832d6c0
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 10 deletions.
8 changes: 7 additions & 1 deletion backend-project/small_eod/autocomplete/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ..events.models import Event
from ..features.models import Feature, FeatureOption
from ..institutions.models import Institution
from ..letters.models import DocumentType
from ..letters.models import DocumentType, ReferenceNumber
from ..tags.models import Tag
from ..users.models import User

Expand Down Expand Up @@ -35,6 +35,12 @@ class Meta:
fields = ["id", "name"]


class ReferenceNumberAutocompleteSerializer(serializers.ModelSerializer):
class Meta:
model = ReferenceNumber
fields = ["id", "name"]


class EventAutocompleteSerializer(serializers.ModelSerializer):
class Meta:
model = Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
from ...generic.mixins import AuthRequiredMixin
from ...generic.tests.test_serializers import ResourceSerializerMixin
from ...institutions.factories import InstitutionFactory
from ...letters.factories import DocumentTypeFactory
from ...letters.factories import DocumentTypeFactory, ReferenceNumberFactory
from ...tags.factories import TagFactory
from ...users.factories import UserFactory
from ..serializers import (
AdministrativeUnitAutocompleteSerializer,
CaseAutocompleteSerializer,
ChannelAutocompleteSerializer,
DocumentTypeAutocompleteSerializer,
ReferenceNumberAutocompleteSerializer,
EventAutocompleteSerializer,
FeatureAutocompleteSerializer,
FeatureOptionAutocompleteSerializer,
Expand Down Expand Up @@ -53,6 +54,13 @@ class DocumentTypeAutocompleteSerializerTestCase(
factory_class = DocumentTypeFactory


class ReferenceNumberAutocompleteSerializerTestCase(
ResourceSerializerMixin, AuthRequiredMixin, TestCase
):
serializer_class = ReferenceNumberAutocompleteSerializer
factory_class = ReferenceNumberFactory


class EventAutocompleteSerializerTestCase(
ResourceSerializerMixin, AuthRequiredMixin, TestCase
):
Expand Down
60 changes: 57 additions & 3 deletions backend-project/small_eod/autocomplete/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from ...features.factories import FeatureFactory, FeatureOptionFactory
from ...generic.tests.test_views import ReadOnlyViewSetMixin
from ...institutions.factories import InstitutionFactory
from ...letters.factories import DocumentTypeFactory
from ...letters.factories import (
DocumentTypeFactory,
ReferenceNumberFactory,
LetterFactory,
)
from ...search.tests.mixins import SearchQueryMixin
from ...tags.factories import TagFactory
from ...users.factories import UserFactory
Expand Down Expand Up @@ -55,6 +59,56 @@ def validate_item(self, item):
self.assertEqual(item["name"], self.obj.name)


class ReferenceNumberAutocompleteViewSetTestCase(
ReadOnlyViewSetMixin, SearchQueryMixin, TestCase
):
basename = "autocomplete_reference_number"
factory_class = ReferenceNumberFactory

def validate_item(self, item):
self.assertEqual(item["id"], self.obj.id)
self.assertEqual(item["name"], self.obj.name)

def test_suggests_related(self):
case = CaseFactory()

# `ReadOnlyViewSetMixin` creates a single instance in its `setUp` method.
# Resuse the object not to have dangling items.
reference_number_a = self.obj
reference_number_b = ReferenceNumberFactory(name="ref_b")
reference_number_c = ReferenceNumberFactory(name="ref_c")
LetterFactory(reference_number=reference_number_a, case=None)
LetterFactory(reference_number=reference_number_b, case=case)
LetterFactory(reference_number=reference_number_c, case=case)

# Match all - the related items should appear at the top.
# There's no guarantee on specific ordering of items. We only have guarantees
# that:
# 1. Both related and unrelated items appear in the result.
# 2. Related items appear before unrelated ones
resp = self.get_response_for_query("ref", case=case.id)
result_ids = [datum["id"] for datum in resp.data["results"]]

# Ad. 1
self.assertEqual(
set(result_ids),
{reference_number_a.id, reference_number_b.id, reference_number_c.id},
)

# Ad. 2.
self.assertEqual(
set(result_ids[:2]), {reference_number_b.id, reference_number_c.id}
)
self.assertEqual(result_ids[2], reference_number_a.id)

# Match a specific, non-related item.
# Related items should not be present.
resp = self.get_response_for_query(reference_number_a.name, case=case.id)
self.assertEqual(
[r["id"] for r in resp.data["results"]], [reference_number_a.id]
)


class EventAutocompleteViewSetTestCase(
ReadOnlyViewSetMixin, SearchQueryMixin, TestCase
):
Expand Down Expand Up @@ -109,12 +163,12 @@ def test_suggests_related(self):

# 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)
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])
self.assertEqual([r["id"] for r in resp.data["results"]], [institution_a.id])


class TagAutocompleteViewSetTestCase(ReadOnlyViewSetMixin, SearchQueryMixin, TestCase):
Expand Down
6 changes: 6 additions & 0 deletions backend-project/small_eod/autocomplete/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CaseAutocompleteViewSet,
ChannelAutocompleteViewSet,
DocumentTypeAutocompleteViewSet,
ReferenceNumberAutocompleteViewSet,
EventAutocompleteViewSet,
FeatureAutocompleteViewSet,
FeatureOptionAutocompleteViewSet,
Expand All @@ -25,6 +26,11 @@
router.register(
"document_types", DocumentTypeAutocompleteViewSet, "autocomplete_document_type"
)
router.register(
"reference_numbers",
ReferenceNumberAutocompleteViewSet,
"autocomplete_reference_number",
)
router.register("events", EventAutocompleteViewSet, "autocomplete_event")
router.register("features", FeatureAutocompleteViewSet, "autocomplete_feature")
router.register(
Expand Down
39 changes: 34 additions & 5 deletions backend-project/small_eod/autocomplete/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from ..features.models import Feature, FeatureOption
from ..institutions.filterset import InstitutionFilterSet
from ..institutions.models import Institution
from ..letters.filterset import DocumentTypeFilterSet
from ..letters.models import DocumentType
from ..letters.filterset import DocumentTypeFilterSet, ReferenceNumberFilterSet
from ..letters.models import DocumentType, ReferenceNumber, Letter
from ..tags.filterset import TagFilterSet
from ..tags.models import Tag
from ..users.filterset import UserFilterSet
Expand All @@ -25,6 +25,7 @@
CaseAutocompleteSerializer,
ChannelAutocompleteSerializer,
DocumentTypeAutocompleteSerializer,
ReferenceNumberAutocompleteSerializer,
EventAutocompleteSerializer,
FeatureAutocompleteSerializer,
FeatureOptionAutocompleteSerializer,
Expand All @@ -33,6 +34,7 @@
UserAutocompleteSerializer,
)


class AdministrativeUnitAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):
queryset = AdministrativeUnit.objects.only("id", "name").all()
serializer_class = AdministrativeUnitAutocompleteSerializer
Expand Down Expand Up @@ -61,6 +63,33 @@ class DocumentTypeAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):
filterset_class = DocumentTypeFilterSet


class ReferenceNumberAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = ReferenceNumberAutocompleteSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = ReferenceNumberFilterSet

def get_queryset(self):
req = self.request
related_case_id = req.GET.get("case", None)
if related_case_id is None:
qs = ReferenceNumber.objects.all()
else:
# Put related objects at the beginning of the list.
reference_numbers_under_case = (
Letter.objects.filter(case=related_case_id)
.values_list("reference_number", flat=True)
.distinct()
)
qs = ReferenceNumber.objects.annotate(
related=models.Case(
models.When(id__in=reference_numbers_under_case, then=True),
default=False,
output_field=models.BooleanField(),
)
).order_by("-related")
return qs.only("id", "name")


class EventAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Event.objects.only("id", "name").all()
serializer_class = EventAutocompleteSerializer
Expand Down Expand Up @@ -89,7 +118,7 @@ class InstitutionAutocompleteViewSet(viewsets.ReadOnlyModelViewSet):

def get_queryset(self):
req = self.request
related_case_id = req.GET.get('case', None)
related_case_id = req.GET.get("case", None)
if related_case_id is None:
qs = Institution.objects.all()
else:
Expand All @@ -98,9 +127,9 @@ def get_queryset(self):
related=models.Case(
models.When(case=related_case_id, then=True),
default=False,
output_field=models.BooleanField()
output_field=models.BooleanField(),
)
).order_by('-related')
).order_by("-related")
return qs.only("id", "name")


Expand Down

0 comments on commit 832d6c0

Please sign in to comment.