From 5fd202fabe6e27c88f0db692a6408009d06b55b1 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 7 Dec 2025 08:51:42 -0700 Subject: [PATCH] Fixing issue where not all commonly shared tags were found during bulk tagging. --- app/src/components/BulkTagModal.js | 2 +- wrolpi/files/lib.py | 16 ++++++++++--- wrolpi/files/test/test_lib.py | 37 +++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/app/src/components/BulkTagModal.js b/app/src/components/BulkTagModal.js index 4dcda236..08ad8632 100644 --- a/app/src/components/BulkTagModal.js +++ b/app/src/components/BulkTagModal.js @@ -185,7 +185,7 @@ export function BulkTagModal({open, onClose, paths, onComplete}) { indicating color="blue" > - {progress.completed} / {progress.total} files + {progress.completed} / {progress.total} files {progress.queued_jobs > 0 && (

{progress.queued_jobs} job{progress.queued_jobs !== 1 ? 's' : ''} queued

diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index 1ac20284..c1078839 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -1706,9 +1706,13 @@ def get_bulk_tag_preview(paths: List[str]) -> BulkTagPreview: # Find tags shared by ALL files # First, get all FileGroups and their tags + # We query by stem prefix because unique_files may contain non-primary files + # (e.g., .readability.json) but FileGroups are stored with primary_path (e.g., .html) with get_db_session() as session: + from sqlalchemy import or_ + stems = [split_path_stem_and_suffix(f, full=True)[0] for f in unique_files] file_groups = session.query(FileGroup).filter( - FileGroup.primary_path.in_([str(f) for f in unique_files]) + or_(*[FileGroup.primary_path.like(f'{stem}%') for stem in stems]) ).all() if not file_groups: @@ -1782,10 +1786,16 @@ def queue_bulk_tag_job(paths: List[str], add_tag_names: List[str], remove_tag_na ) api_app.shared_ctx.bulk_tag_queue.put(job) - # Update queued jobs count + # Reset progress state and update queued jobs count try: current_queued = api_app.shared_ctx.bulk_tag.get('queued_jobs', 0) - api_app.shared_ctx.bulk_tag['queued_jobs'] = current_queued + 1 + api_app.shared_ctx.bulk_tag.update(dict( + status='idle', + total=0, + completed=0, + error=None, + queued_jobs=current_queued + 1, + )) except Exception: pass diff --git a/wrolpi/files/test/test_lib.py b/wrolpi/files/test/test_lib.py index 2171214c..453fae25 100644 --- a/wrolpi/files/test/test_lib.py +++ b/wrolpi/files/test/test_lib.py @@ -1206,7 +1206,8 @@ async def test_get_bulk_tag_preview_files(async_client, test_session, make_files assert 'only_foo' not in preview.shared_tag_names # Not shared by all -def test_get_bulk_tag_preview_directory(test_session, make_files_structure, test_directory): +@pytest.mark.asyncio +async def test_get_bulk_tag_preview_directory(test_session, make_files_structure, test_directory, tag_factory): """get_bulk_tag_preview recursively finds files in directories.""" make_files_structure({ 'mydir/foo.txt': 'foo', @@ -1219,6 +1220,40 @@ def test_get_bulk_tag_preview_directory(test_session, make_files_structure, test assert preview.file_count == 3 +@pytest.mark.asyncio +async def test_get_bulk_tag_preview_multi_file_filegroup(async_client, test_session, make_files_structure, + test_directory, tag_factory): + """get_bulk_tag_preview finds shared tags for multi-file FileGroups. + + This tests a bug where `get_unique_files_by_stem` may return a non-primary file (e.g. .readability.json) + but the FileGroup query looks for exact primary_path match (e.g. .html), causing no FileGroup to be found. + """ + # Create a multi-file FileGroup like an Archive (primary is .html, but has .readability.json, etc.) + # SingleFile archives are named with the pattern: %Y-%m-%d-%H-%M-%S_title.html + html_file, json_file, txt_file = make_files_structure({ + 'archive/2025-01-01-12-00-00_Page Title.html': 'content', + 'archive/2025-01-01-12-00-00_Page Title.readability.json': '{"title": "Page"}', + 'archive/2025-01-01-12-00-00_Page Title.readability.txt': 'Page content', + }) + + # Create the FileGroup with .html as the primary path + fg = FileGroup.from_paths(test_session, html_file, json_file, txt_file) + test_session.commit() + + # Verify the primary_path is the .html file + assert fg.primary_path.suffix == '.html', f'Expected primary_path to be .html, got {fg.primary_path}' + + # Add a tag to this FileGroup + tag = await tag_factory('my_tag') + fg.add_tag(tag.id) + test_session.commit() + + # Preview for the directory should find the FileGroup and its tag + preview = lib.get_bulk_tag_preview(['archive/']) + assert preview.file_count == 1, f'Expected 1 FileGroup, got {preview.file_count}' + assert 'my_tag' in preview.shared_tag_names, f'Expected my_tag in shared tags, got {preview.shared_tag_names}' + + def test_get_bulk_tag_preview_empty(test_session, test_directory): """get_bulk_tag_preview returns 0 for empty or non-existent paths.""" preview = lib.get_bulk_tag_preview([])