From 59b23b3134d581331f5c0c9652fb57e52d107a87 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 10 Feb 2024 21:11:19 -0700 Subject: [PATCH] Creating function which appends tag names sub-select. --- modules/archive/lib.py | 8 ++------ modules/videos/video/lib.py | 9 +++------ wrolpi/files/lib.py | 19 ++++++------------- wrolpi/tags.py | 20 ++++++++++++++++---- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/modules/archive/lib.py b/modules/archive/lib.py index ea35a5a8..2c381f3b 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -19,7 +19,7 @@ from wrolpi.common import get_media_directory, logger, extract_domain, escape_fi from wrolpi.dates import now, Seconds from wrolpi.db import get_db_session, get_db_curs, optional_session from wrolpi.errors import UnknownArchive, InvalidOrderBy, InvalidDatetime -from wrolpi.tags import tag_names_to_file_group_sub_select +from wrolpi.tags import tag_append_sub_select_where from wrolpi.vars import PYTEST logger = logger.getChild(__name__) @@ -492,11 +492,7 @@ def search_archives(search_str: str, domain: str, limit: int, offset: int, order if order in NO_NULL_ORDERS: wheres.append(NO_NULL_ORDERS[order]) - if tag_names: - # Filter all FileGroups by those that have been tagged with the provided tag names. - tags_stmt, params_ = tag_names_to_file_group_sub_select(tag_names) - params.update(params_) - wheres.append(f'fg.id = ANY({tags_stmt})') + wheres, params = tag_append_sub_select_where(tag_names, wheres, params) if search_str and headline: headline = ''', diff --git a/modules/videos/video/lib.py b/modules/videos/video/lib.py index 1009eb55..2796d30f 100644 --- a/modules/videos/video/lib.py +++ b/modules/videos/video/lib.py @@ -5,7 +5,8 @@ from sqlalchemy.orm import Session from modules.videos.models import Video from wrolpi.common import run_after, logger from wrolpi.db import get_db_session, optional_session -from wrolpi.files.lib import handle_file_group_search_results, tag_names_to_file_group_sub_select +from wrolpi.files.lib import handle_file_group_search_results +from wrolpi.tags import tag_append_sub_select_where from ..errors import UnknownVideo from ..lib import save_channels_config @@ -89,11 +90,7 @@ def search_videos( # No search_str provided. Get id and total only. select_columns = 'fg.id, COUNT(*) OVER() AS total' - if tag_names: - # Filter all FileGroups by those that have been tagged with the provided tag names. - tags_stmt, params_ = tag_names_to_file_group_sub_select(tag_names) - params.update(params_) - wheres.append(f'fg.id = ANY({tags_stmt})') + wheres, params = tag_append_sub_select_where(tag_names, wheres, params) if search_str and headline: headline = ''', diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index 0d6d5f67..cdca00be 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -31,7 +31,7 @@ from wrolpi.errors import InvalidFile, UnknownDirectory, UnknownFile, UnknownTag from wrolpi.events import Events from wrolpi.files.models import FileGroup, Directory from wrolpi.lang import ISO_639_CODES -from wrolpi.tags import TagFile, Tag, tag_names_to_file_group_sub_select +from wrolpi.tags import TagFile, Tag, tag_append_sub_select_where from wrolpi.vars import PYTEST try: @@ -673,7 +673,7 @@ async def search_directories_by_name(name: str, excluded: List[str] = None, limi def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] = None, model: str = None, - tags: List[str] = None, headline: bool = False) -> \ + tag_names: List[str] = None, headline: bool = False) -> \ Tuple[List[dict], int]: """Search the FileGroup table. @@ -686,7 +686,7 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] offset: Offset the query. mimetypes: Only return files that match these mimetypes. model: Only return files that match this model. - tags: A list of tag names. + tag_names: A list of tag names. headline: Includes Postgresql headline if True. """ params = dict(offset=offset, limit=limit) @@ -709,11 +709,7 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] params['model'] = model wheres.append('model = %(model)s') - if tags: - # Filter all FileGroups by those that have been tagged with all the provided tag names. - tags_stmt, params_ = tag_names_to_file_group_sub_select(tags) - params.update(params_) - wheres.append(f'fg.id = ANY({tags_stmt})') + wheres, params = tag_append_sub_select_where(tag_names, wheres, params) if search_str and headline: headline = ''', @@ -1079,11 +1075,8 @@ async def search_file_suggestion_count(search_str: str, tag_names: List[str], mi if search_str: wheres.append('fg.textsearch @@ websearch_to_tsquery(%(search_str)s)') - if tag_names: - # Only estimate files that have been tagged with all tag names. - tags_stmt, params_ = tag_names_to_file_group_sub_select(tag_names) - params.update(params_) - wheres.append(f'fg.id = ANY({tags_stmt})') + + wheres, params = tag_append_sub_select_where(tag_names, wheres, params) mimetype_wheres, params = mimetypes_to_sql_wheres(mimetypes, params) if mimetype_wheres: diff --git a/wrolpi/tags.py b/wrolpi/tags.py index 8465448d..4a0a1a83 100644 --- a/wrolpi/tags.py +++ b/wrolpi/tags.py @@ -412,12 +412,13 @@ def import_tags_config(session: Session = None): raise -def tag_names_to_file_group_sub_select(tag_names: List[str]) -> Tuple[str, dict]: +def tag_names_to_file_group_sub_select(tag_names: List[str], params: dict) -> Tuple[str, dict]: """Create the SQL necessary to filter FileGroup by the provided Tag names.""" if not tag_names: - return '', dict() + return '', params - stmt = ''' + # This select gets an array of FileGroup.id's which are tagged with the provided tag names. + sub_select = ''' SELECT tf.file_group_id FROM @@ -427,7 +428,18 @@ def tag_names_to_file_group_sub_select(tag_names: List[str]) -> Tuple[str, dict] -- Match only FileGroups that have at least all the Tag names. HAVING array_agg(t.name)::TEXT[] @> %(tag_names)s::TEXT[] ''' - return stmt, dict(tag_names=tag_names) + params['tag_names'] = tag_names + return sub_select, params + + +def tag_append_sub_select_where(tag_names: list[str], wheres: list[str], params: dict) -> Tuple[list[str], dict]: + """Modify provided `wheres` and `params` to filter by `tag_names`, if any.""" + if not tag_names: + return wheres, params + + tags_sub_select, params = tag_names_to_file_group_sub_select(tag_names, params) + wheres.append(f'fg.id = ANY({tags_sub_select})') + return wheres, params def tag_names_to_zim_sub_select(tag_names: List[str], zim_id: int = None) -> Tuple[str, dict]: