mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 17:36:18 -05:00
The Any Tag filter works on the Videos, Archives and Docs searches (#958)
The filter modal on these pages offered "Any Tag", but only the Files search sent it; the module searches had no `any_tag` field. Docs also matched any of the listed tag_names, while every other search requires all of them. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a0f30a0a60
commit
2800112f3e
16 changed files with 189 additions and 30 deletions
|
|
@ -66,6 +66,7 @@ async def search_docs(request: Request, body: schema.DocSearchRequest):
|
|||
order_by=body.order_by,
|
||||
tag_names=body.tag_names,
|
||||
deep=body.deep,
|
||||
any_tag=body.any_tag,
|
||||
)
|
||||
ret = dict(file_groups=file_groups, totals=dict(file_groups=total))
|
||||
return json_response(ret)
|
||||
|
|
|
|||
|
|
@ -13,8 +13,9 @@ from wrolpi.collections.types import collection_type_registry
|
|||
from wrolpi.errors import ValidationError
|
||||
from wrolpi.files.lib import split_path_stem_and_suffix, get_mimetype, cached_search_total, \
|
||||
search_filter_cache_key
|
||||
from wrolpi.tags import TagFile, tag_names_to_file_group_sub_select
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger =logging.getLogger(__name__)
|
||||
|
||||
# Register author and subject collection types.
|
||||
collection_type_registry.register(
|
||||
|
|
@ -371,7 +372,7 @@ def _doc_browse_sql(order_by: str) -> Optional[str]:
|
|||
|
||||
|
||||
def _search_docs(session: Session, search_str=None, author=None, subject=None, language=None, mimetype=None,
|
||||
limit=20, offset=0, order_by='published_datetime', tag_names=None, deep=False):
|
||||
limit=20, offset=0, order_by='published_datetime', tag_names=None, deep=False, any_tag=False):
|
||||
from .models import Doc
|
||||
from wrolpi.files.models import FileGroup
|
||||
|
||||
|
|
@ -402,14 +403,18 @@ def _search_docs(session: Session, search_str=None, author=None, subject=None, l
|
|||
if mimetype:
|
||||
query = query.filter(FileGroup.mimetype.startswith(mimetype))
|
||||
|
||||
if any_tag and tag_names:
|
||||
raise ValidationError('Cannot use both tag_names and any_tag.')
|
||||
if tag_names:
|
||||
from wrolpi.tags import Tag, TagFile
|
||||
tagged_fg_ids = session.query(TagFile.file_group_id) \
|
||||
.join(Tag, Tag.id == TagFile.tag_id) \
|
||||
.filter(Tag.name.in_(tag_names))
|
||||
# A Doc must have every Tag, like the other searches.
|
||||
tags_sub_select, tags_params = tag_names_to_file_group_sub_select(tag_names, dict())
|
||||
tagged_fg_ids = text(tags_sub_select).bindparams(**tags_params) \
|
||||
.columns(column('file_group_id', Integer))
|
||||
query = query.filter(FileGroup.id.in_(tagged_fg_ids))
|
||||
elif any_tag:
|
||||
query = query.filter(FileGroup.id.in_(session.query(TagFile.file_group_id)))
|
||||
|
||||
unfiltered = not (search_str or author or subject or language or mimetype or tag_names)
|
||||
unfiltered = not (search_str or author or subject or language or mimetype or tag_names or any_tag)
|
||||
if unfiltered:
|
||||
total = cached_search_total(
|
||||
search_filter_cache_key('docs'),
|
||||
|
|
@ -418,7 +423,7 @@ def _search_docs(session: Session, search_str=None, author=None, subject=None, l
|
|||
else:
|
||||
cache_key = search_filter_cache_key(
|
||||
'docs', search_str=search_str, author=author, subject=subject,
|
||||
language=language, mimetype=mimetype, tag_names=tag_names, deep=deep,
|
||||
language=language, mimetype=mimetype, tag_names=tag_names, deep=deep, any_tag=any_tag,
|
||||
)
|
||||
# Count before ORDER BY / LIMIT; cache by filters not page.
|
||||
total = cached_search_total(cache_key, lambda q=query: q.count())
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import dataclasses
|
||||
from typing import Optional
|
||||
|
||||
from wrolpi.errors import ValidationError
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class DocSearchRequest:
|
||||
|
|
@ -14,6 +16,11 @@ class DocSearchRequest:
|
|||
limit: int = 20
|
||||
offset: int = 0
|
||||
deep: bool = False # When True, search all text including document contents (d_text)
|
||||
any_tag: bool = False # When True, only Docs with at least one Tag.
|
||||
|
||||
def __post_init__(self):
|
||||
if self.any_tag and self.tag_names:
|
||||
raise ValidationError('Cannot use both tag_names and any_tag.')
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
|
|
|
|||
|
|
@ -98,6 +98,36 @@ async def test_docs_search_by_tag(async_client, test_session, doc_factory, tag_f
|
|||
assert response.json['totals']['file_groups'] == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_docs_search_tag_names_and_any_tag(async_client, test_session, doc_factory, tag_factory):
|
||||
"""Docs tag filtering matches the other searches: `tag_names` requires every tag, `any_tag` requires some tag."""
|
||||
tag1, tag2 = await tag_factory(), await tag_factory()
|
||||
one_tag, both_tags, untagged = doc_factory(), doc_factory(), doc_factory()
|
||||
one_tag.file_group.add_tag(test_session, tag1.id)
|
||||
both_tags.file_group.add_tag(test_session, tag1.id)
|
||||
both_tags.file_group.add_tag(test_session, tag2.id)
|
||||
test_session.commit()
|
||||
|
||||
async def search_ids(**body):
|
||||
request, response = await async_client.post('/api/docs/search', content=json.dumps(body))
|
||||
assert response.status_code == HTTPStatus.OK, response.json
|
||||
assert response.json['totals']['file_groups'] == len(response.json['file_groups'])
|
||||
return {i['id'] for i in response.json['file_groups']}
|
||||
|
||||
assert await search_ids(tag_names=[tag1.name]) == {one_tag.file_group_id, both_tags.file_group_id}
|
||||
# Only one Doc has both Tags.
|
||||
assert await search_ids(tag_names=[tag1.name, tag2.name]) == {both_tags.file_group_id}
|
||||
# Any Tag excludes only the untagged Doc.
|
||||
assert await search_ids(any_tag=True) == {one_tag.file_group_id, both_tags.file_group_id}
|
||||
assert await search_ids(any_tag=False) == {one_tag.file_group_id, both_tags.file_group_id,
|
||||
untagged.file_group_id}
|
||||
|
||||
# Cannot search for any_tag, and tag names.
|
||||
content = dict(tag_names=[tag1.name], any_tag=True)
|
||||
request, response = await async_client.post('/api/docs/search', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_doc(async_client, test_session, test_directory, example_epub, refresh_files):
|
||||
"""Docs can be deleted, including their on-disk files and FileGroup row."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue