diff --git a/wrolpi/files/api.py b/wrolpi/files/api.py index 5ad08ee6..1dc8b3ab 100644 --- a/wrolpi/files/api.py +++ b/wrolpi/files/api.py @@ -127,7 +127,7 @@ async def post_search_files(_: Request, body: schema.FilesSearchRequest): with timer('Searching all files', 'info', logger__=logger): file_groups, total = lib.search_files(body.search_str, body.limit, body.offset, body.mimetypes, body.model, body.tag_names, body.headline, body.months, body.from_year, body.to_year, - body.any_tag, body.order) + body.any_tag, body.order, body.url) return json_response(dict(file_groups=file_groups, totals=dict(file_groups=total))) diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index 96da186a..553292de 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -889,7 +889,8 @@ async def search_directories_by_name(session: Session, name: str, excluded: List def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] = None, model: str = None, tag_names: List[str] = None, headline: bool = False, months: List[int] = None, - from_year: int = None, to_year: int = None, any_tag: bool = False, order: str = None) -> \ + from_year: int = None, to_year: int = None, any_tag: bool = False, order: str = None, + url: str = None) -> \ Tuple[List[dict], int]: """Search the FileGroup table. @@ -908,6 +909,7 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] @param headline: Includes Postgresql headline if True. @param months: A list of integers representing the index of the month of the year, starting at 1. @param order: Used to change results from most relevant to recently viewed. + @param url: Filter by URL using case-insensitive partial match (ILIKE). """ params = dict(offset=offset, limit=limit, search_str=search_str, url_search_str=f'%{search_str}%') wheres = [] @@ -930,6 +932,10 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] wheres, params = months_selector_to_where(wheres, params, months) wheres, params = date_range_to_where(wheres, params, from_year, to_year) + if url: + params['url_filter'] = f'%{url}%' + wheres.append('fg.url ILIKE %(url_filter)s') + if search_str and headline: headline = ''', ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline", diff --git a/wrolpi/files/schema.py b/wrolpi/files/schema.py index 459397a0..c9da0b17 100644 --- a/wrolpi/files/schema.py +++ b/wrolpi/files/schema.py @@ -33,6 +33,7 @@ class FilesSearchRequest: to_year: Optional[int] = None any_tag: bool = False order: Optional[str] = None + url: Optional[str] = None # Filter by URL (partial match) def __post_init__(self): if self.any_tag and self.tag_names: diff --git a/wrolpi/files/test/test_lib.py b/wrolpi/files/test/test_lib.py index 1016c15c..ed01ffb5 100644 --- a/wrolpi/files/test/test_lib.py +++ b/wrolpi/files/test/test_lib.py @@ -1383,6 +1383,60 @@ async def test_rename_non_primary_file_renames_filegroup(async_client, test_sess +@pytest.mark.asyncio +async def test_file_search_url(test_session, make_files_structure, refresh_files): + """Test searching FileGroups by their URL.""" + foo, bar, baz = make_files_structure({ + 'foo.txt': 'content', + 'bar.txt': 'content', + 'baz.txt': 'content', + }) + await refresh_files() + + foo_fg, bar_fg, baz_fg = test_session.query(FileGroup).order_by(FileGroup.primary_path).all() + foo_fg.url = 'https://example.com/foo' + bar_fg.url = 'https://example.org/bar' + baz_fg.url = 'https://example.com/baz/page' + test_session.commit() + + # Search by full URL + files, total = lib.search_files('', 10, 0, url='https://example.com/foo') + assert total == 1 + assert files[0]['id'] == foo_fg.id + + # Search by domain (partial match) + files, total = lib.search_files('', 10, 0, url='example.com') + assert total == 2 + assert {f['id'] for f in files} == {foo_fg.id, baz_fg.id} + + # Case insensitive search + files, total = lib.search_files('', 10, 0, url='EXAMPLE.COM') + assert total == 2 + + # No results for non-matching URL + files, total = lib.search_files('', 10, 0, url='nonexistent.com') + assert total == 0 + + +@pytest.mark.asyncio +async def test_file_search_url_with_null(test_session, make_files_structure, refresh_files): + """Test that files without URLs are excluded when URL filter is applied.""" + foo, bar = make_files_structure({ + 'foo.txt': 'content', + 'bar.txt': 'content', + }) + await refresh_files() + + foo_fg, bar_fg = test_session.query(FileGroup).order_by(FileGroup.primary_path).all() + foo_fg.url = 'https://example.com/foo' + # bar_fg.url remains None + test_session.commit() + + files, total = lib.search_files('', 10, 0, url='example.com') + assert total == 1 + assert files[0]['id'] == foo_fg.id + + def test_bulk_update_file_groups_reorganize_handles_datetime(test_session, test_directory): """_bulk_update_file_groups_reorganize should handle datetime objects in files JSON.