Skip to content

Commit

Permalink
Allow incidents to be searched by content in updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chigby committed Apr 24, 2023
1 parent 95fa7df commit c5c7f60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions incident/models/incident_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,10 @@ class IncidentPage(MetadataPageMixin, Page):
index.SearchField('attribution'),
]),
index.SearchField('image_caption'),
index.RelatedFields('updates', [
index.SearchField('title'),
index.SearchField('body'),
]),
]

def get_context(self, request, *args, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions incident/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ class Meta:
body = None


class IncidentUpdateWithBodyFactory(IncidentUpdateFactory):
body = wagtail_factories.StreamFieldFactory({
'rich_text': factory.SubFactory(RichTextTemplateBlockFactory),
'image': factory.SubFactory(
wagtail_factories.blocks.ImageChooserBlockFactory
),
'raw_html': factory.SubFactory(RawHTMLBlockFactory),
'tweet': factory.SubFactory(TweetEmbedBlockFactory),
'blockquote': factory.SubFactory(RichTextBlockQuoteBlockFactory),
'video': factory.SubFactory(AlignedCaptionedEmbedBlockFactory),
})


class IncidentLinkFactory(factory.django.DjangoModelFactory):
class Meta:
model = IncidentPageLinks
Expand Down
18 changes: 17 additions & 1 deletion incident/tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
EquipmentBrokenFactory,
IncidentPageFactory,
IncidentPageWithBodyFactory,
IncidentUpdateWithBodyFactory,
IncidentIndexPageFactory,
IncidentUpdateFactory,
InexactDateIncidentPageFactory,
Expand Down Expand Up @@ -507,7 +508,14 @@ def setUpTestData(cls):
),
body__6__video__alignment=ALIGNMENT_CHOICES[1][0],
)

IncidentUpdateWithBodyFactory(
page=cls.incident1,
title='Coconut',
body__0__rich_text=RichText('Strawberry.'),
)
# Save required here to force update of index after
# IncidentUpdate created.
cls.incident1.save()

cls.incident2 = IncidentPageWithBodyFactory(
parent=cls.index,
Expand Down Expand Up @@ -609,6 +617,14 @@ def test_body_video_embed_alignments_are_not_searched(self):
incidents = IncidentFilter({'search': 'right'}).get_queryset()
self.assertQuerysetEqual(incidents, [])

def test_update_titles_are_searched(self):
incidents = IncidentFilter({'search': 'coconut'}).get_queryset()
self.assertQuerysetEqual(incidents, [self.incident1])

def test_update_bodies_are_searched(self):
incidents = IncidentFilter({'search': 'strawberry'}).get_queryset()
self.assertQuerysetEqual(incidents, [self.incident1])


class TestAllFiltersAtOnce:
"""Base class for testing all filters at once."""
Expand Down

0 comments on commit c5c7f60

Please sign in to comment.