mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 11:56:18 -05:00
Creating function which appends tag names sub-select.
This commit is contained in:
parent
8916600a14
commit
59b23b3134
4 changed files with 27 additions and 29 deletions
|
|
@ -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 = ''',
|
||||
|
|
|
|||
|
|
@ -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 = ''',
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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]:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue