Skip to content

Commit

Permalink
Fix a bug where files would not appear in the search index
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra committed Jan 11, 2025
1 parent c73e650 commit 74a70a3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<a href="{{ entity | localized_url }}" class="nav-secondary-action search-result-target">
<div class="search-result-description">
{{ entity.description | localize }}
{{ entity.label | localize }}
<div class="search-result-file-type">{% trans %}Media{% endtrans %}</div>
</div>
{% if entity.media_type and entity.media_type.type== 'image' %}
Expand Down
18 changes: 11 additions & 7 deletions betty/project/extension/cotton_candy/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _static_translations_to_text(


class _EntityTypeIndexer(Generic[_EntityT], ABC):
def text(self, entity: _EntityT) -> set[str]:
def text(self, localizer: Localizer, entity: _EntityT) -> set[str]:
text = set()

# Each note is owner by a single other entity, so index it as part of that entity.
Expand All @@ -65,8 +65,8 @@ def text(self, entity: _EntityT) -> set[str]:

class _PersonIndexer(_EntityTypeIndexer[Person]):
@override
def text(self, entity: Person) -> set[str]:
text = super().text(entity)
def text(self, localizer: Localizer, entity: Person) -> set[str]:
text = super().text(localizer, entity)
for name in entity.names:
if name.individual is not None:
text.update(set(name.individual.lower().split()))
Expand All @@ -77,15 +77,19 @@ def text(self, entity: Person) -> set[str]:

class _PlaceIndexer(_EntityTypeIndexer[Place]):
@override
def text(self, entity: Place) -> set[str]:
text = super().text(entity)
def text(self, localizer: Localizer, entity: Place) -> set[str]:
text = super().text(localizer, entity)
for name in entity.names:
text.update(_static_translations_to_text(name.name))
return text


class _FileIndexer(_EntityTypeIndexer[File]):
pass
@override
def text(self, localizer: Localizer, entity: File) -> set[str]:
text = super().text(localizer, entity)
text.update(entity.label.localize(localizer).strip().lower().split())
return text


class _SourceIndexer(_EntityTypeIndexer[Source]):
Expand Down Expand Up @@ -148,7 +152,7 @@ async def _build_entity(
) -> _Entry | None:
if is_private(entity):
return None
text = indexer.text(entity)
text = indexer.text(self._localizer, entity)
if not text:
return None
return _Entry(text, await self._render_entity(entity))
Expand Down
95 changes: 54 additions & 41 deletions betty/tests/project/extension/cotton_candy/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,48 +284,68 @@ async def test_build_private_place(self, new_temporary_app: App) -> None:

assert actual == []

async def test_build_file_without_description(self, new_temporary_app: App) -> None:
file_id = "F1"
file = File(
id=file_id,
path=Path(__file__),
)

async with Project.new_temporary(new_temporary_app) as project:
project.configuration.extensions.enable(CottonCandy)
project.configuration.locales["en-US"].alias = "en"
project.configuration.locales.append(
LocaleConfiguration(
"nl-NL",
alias="nl",
)
)
project.ancestry.add(file)
async with project:
actual = await Index(
project.ancestry,
await project.jinja2_environment,
Context(),
DEFAULT_LOCALIZER,
).build()

assert actual == []

@pytest.mark.parametrize(
("expected", "locale"),
("expected_text", "expected_result", "description", "locale"),
[
("/nl/file/F1/index.html", "nl-NL"),
("/en/file/F1/index.html", "en-US"),
(
{
'"file"',
"is",
"dutch",
"for",
'"traffic',
'jam"',
},
"/nl/file/F1/index.html",
'"file" is Dutch for "traffic jam"',
"nl-NL",
),
(
{
'"file"',
"is",
"dutch",
"for",
'"traffic',
'jam"',
},
"/en/file/F1/index.html",
'"file" is Dutch for "traffic jam"',
"en-US",
),
(
{
"bestand",
"f1",
},
"/nl/file/F1/index.html",
None,
"nl-NL",
),
(
{
"file",
"f1",
},
"/en/file/F1/index.html",
None,
"en-US",
),
],
)
async def test_build_file(
self, expected: str, locale: str, new_temporary_app: App
self,
expected_text: set[str],
expected_result: str,
description: str | None,
locale: str,
new_temporary_app: App,
) -> None:
file_id = "F1"
file = File(
id=file_id,
path=Path(__file__),
description='"file" is Dutch for "traffic jam"',
description=description,
)

async with Project.new_temporary(new_temporary_app) as project:
Expand All @@ -347,15 +367,8 @@ async def test_build_file(
await localizers.get(locale),
).build()

assert actual[0].text == {
'"file"',
"is",
"dutch",
"for",
'"traffic',
'jam"',
}
assert expected in actual[0].result
assert actual[0].text == expected_text
assert expected_result in actual[0].result

async def test_build_private_file(self, new_temporary_app: App) -> None:
file_id = "F1"
Expand Down

0 comments on commit 74a70a3

Please sign in to comment.