mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-20 23:16:17 -05:00
Adding bulk tag button to the file browser. User can add tags to many files, but can only remove tags if all files share the tags to be removed.
This commit is contained in:
parent
c1e318cafc
commit
c6df730dcc
10 changed files with 1023 additions and 1 deletions
|
|
@ -1684,3 +1684,37 @@ export async function triggerUpgrade() {
|
|||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function getBulkTagPreview(paths) {
|
||||
const body = {paths};
|
||||
const response = await apiPost(`${API_URI}/files/bulk_tag/preview`, body);
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
} else {
|
||||
const message = await getErrorMessage(response, 'Failed to get bulk tag preview.');
|
||||
toast({type: 'error', title: 'Bulk Tag Error', description: message, time: 5000});
|
||||
}
|
||||
}
|
||||
|
||||
export async function applyBulkTags(paths, addTagNames, removeTagNames) {
|
||||
const body = {
|
||||
paths,
|
||||
add_tag_names: addTagNames || [],
|
||||
remove_tag_names: removeTagNames || [],
|
||||
};
|
||||
const response = await apiPost(`${API_URI}/files/bulk_tag/apply`, body);
|
||||
if (!response.ok) {
|
||||
const message = await getErrorMessage(response, 'Failed to apply bulk tags.');
|
||||
toast({type: 'error', title: 'Bulk Tag Error', description: message, time: 5000});
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function getBulkTagProgress() {
|
||||
const response = await apiGet(`${API_URI}/files/bulk_tag/progress`);
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
}
|
||||
// Not toasting because this happens often during polling.
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
243
app/src/components/BulkTagModal.js
Normal file
243
app/src/components/BulkTagModal.js
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
import React from 'react';
|
||||
import {Button, Divider, Header, Loader, Modal, ModalActions, ModalContent, ModalHeader, Progress} from "./Theme";
|
||||
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
|
||||
import {applyBulkTags, getBulkTagPreview, getBulkTagProgress} from "../api";
|
||||
import {TagsContext} from "../Tags";
|
||||
|
||||
const BULK_TAG_WARNING_THRESHOLD = 50;
|
||||
const POLL_INTERVAL = 500; // Poll progress every 500ms
|
||||
|
||||
export function BulkTagModal({open, onClose, paths, onComplete}) {
|
||||
// State machine: 'loading' -> 'preview' -> 'applying' -> 'complete'
|
||||
const [state, setState] = React.useState('loading');
|
||||
const [fileCount, setFileCount] = React.useState(0);
|
||||
const [sharedTagNames, setSharedTagNames] = React.useState([]);
|
||||
const [localTags, setLocalTags] = React.useState([]);
|
||||
const [progress, setProgress] = React.useState({total: 0, completed: 0, queued_jobs: 0});
|
||||
const [error, setError] = React.useState(null);
|
||||
|
||||
const {tagNames, TagsGroup} = React.useContext(TagsContext);
|
||||
|
||||
// Fetch preview when modal opens
|
||||
React.useEffect(() => {
|
||||
if (open && paths && paths.length > 0) {
|
||||
setState('loading');
|
||||
setLocalTags([]);
|
||||
setError(null);
|
||||
|
||||
getBulkTagPreview(paths).then(preview => {
|
||||
if (preview) {
|
||||
setFileCount(preview.file_count);
|
||||
setSharedTagNames(preview.shared_tag_names || []);
|
||||
setLocalTags(preview.shared_tag_names || []);
|
||||
setState('preview');
|
||||
} else {
|
||||
setError('Failed to get preview');
|
||||
setState('preview');
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('Failed to get bulk tag preview', err);
|
||||
setError(err.message || 'Failed to get preview');
|
||||
setState('preview');
|
||||
});
|
||||
}
|
||||
}, [open, paths]);
|
||||
|
||||
// Poll progress while applying
|
||||
React.useEffect(() => {
|
||||
if (state !== 'applying') {
|
||||
return;
|
||||
}
|
||||
|
||||
const pollProgress = async () => {
|
||||
const progressData = await getBulkTagProgress();
|
||||
if (progressData) {
|
||||
setProgress(progressData);
|
||||
if (progressData.status === 'idle' && progressData.queued_jobs === 0) {
|
||||
// Done applying
|
||||
setState('complete');
|
||||
}
|
||||
if (progressData.error) {
|
||||
setError(progressData.error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const interval = setInterval(pollProgress, POLL_INTERVAL);
|
||||
pollProgress(); // Initial poll
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [state]);
|
||||
|
||||
const addTag = (name) => {
|
||||
if (!localTags.includes(name)) {
|
||||
setLocalTags([...localTags, name]);
|
||||
}
|
||||
};
|
||||
|
||||
const removeTag = (name) => {
|
||||
setLocalTags(localTags.filter(t => t !== name));
|
||||
};
|
||||
|
||||
const handleApply = async () => {
|
||||
// Compute what changed
|
||||
const tagsToAdd = localTags.filter(t => !sharedTagNames.includes(t));
|
||||
const tagsToRemove = sharedTagNames.filter(t => !localTags.includes(t));
|
||||
|
||||
if (tagsToAdd.length === 0 && tagsToRemove.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState('applying');
|
||||
setProgress({total: 0, completed: 0, queued_jobs: 1});
|
||||
|
||||
try {
|
||||
await applyBulkTags(paths, tagsToAdd, tagsToRemove);
|
||||
} catch (err) {
|
||||
console.error('Failed to apply bulk tags', err);
|
||||
setError(err.message || 'Failed to apply tags');
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
if (state === 'complete' && onComplete) {
|
||||
onComplete();
|
||||
}
|
||||
setState('loading');
|
||||
setFileCount(0);
|
||||
setSharedTagNames([]);
|
||||
setLocalTags([]);
|
||||
setProgress({total: 0, completed: 0, queued_jobs: 0});
|
||||
setError(null);
|
||||
onClose();
|
||||
};
|
||||
|
||||
// Tags not currently applied (available to add)
|
||||
const unusedTags = tagNames ? tagNames.filter(name => !localTags.includes(name)) : [];
|
||||
|
||||
// Check if there are any changes from original
|
||||
const hasChanges =
|
||||
localTags.some(t => !sharedTagNames.includes(t)) || // Added tags
|
||||
sharedTagNames.some(t => !localTags.includes(t)); // Removed tags
|
||||
const showWarning = fileCount >= BULK_TAG_WARNING_THRESHOLD;
|
||||
|
||||
const progressPercent = progress.total > 0
|
||||
? Math.round((progress.completed / progress.total) * 100)
|
||||
: 0;
|
||||
|
||||
return (
|
||||
<Modal closeIcon open={open} onClose={handleClose} size="small">
|
||||
<ModalHeader>Bulk Tag Files</ModalHeader>
|
||||
<ModalContent>
|
||||
{state === 'loading' && (
|
||||
<div style={{textAlign: 'center', padding: '2em'}}>
|
||||
<Loader active inline="centered">Loading preview...</Loader>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{state === 'preview' && (
|
||||
<>
|
||||
{error && (
|
||||
<Message negative>
|
||||
<Message.Header>Error</Message.Header>
|
||||
<p>{error}</p>
|
||||
</Message>
|
||||
)}
|
||||
|
||||
<Header as="h4">
|
||||
{fileCount} file{fileCount !== 1 ? 's' : ''} will be affected
|
||||
</Header>
|
||||
|
||||
{showWarning && (
|
||||
<Message warning>
|
||||
<Message.Header>Warning</Message.Header>
|
||||
<p>Tagging over {BULK_TAG_WARNING_THRESHOLD} files may make it difficult to find what you are looking for. Are you sure?</p>
|
||||
</Message>
|
||||
)}
|
||||
|
||||
<Divider/>
|
||||
|
||||
<Header as="h4">Applied Tags</Header>
|
||||
{localTags.length > 0 ? (
|
||||
<TagsGroup tagNames={localTags} onClick={removeTag}/>
|
||||
) : (
|
||||
<p style={{color: 'grey'}}>Add one or more tags below</p>
|
||||
)}
|
||||
|
||||
<Divider/>
|
||||
|
||||
{unusedTags.length > 0 ? (
|
||||
<TagsGroup tagNames={unusedTags} onClick={addTag}/>
|
||||
) : (
|
||||
<p style={{color: 'grey'}}>You have no tags</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
{state === 'applying' && (
|
||||
<div style={{padding: '1em'}}>
|
||||
<Header as="h4">Applying tags...</Header>
|
||||
<Progress
|
||||
percent={progressPercent}
|
||||
progress
|
||||
indicating
|
||||
color="blue"
|
||||
>
|
||||
{progress.completed} / {progress.total} files
|
||||
</Progress>
|
||||
{progress.queued_jobs > 0 && (
|
||||
<p style={{color: 'grey'}}>{progress.queued_jobs} job{progress.queued_jobs !== 1 ? 's' : ''} queued</p>
|
||||
)}
|
||||
{error && (
|
||||
<Message negative>
|
||||
<Message.Header>Error</Message.Header>
|
||||
<p>{error}</p>
|
||||
</Message>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{state === 'complete' && (
|
||||
<div style={{padding: '1em'}}>
|
||||
<Message positive>
|
||||
<Message.Header>Complete</Message.Header>
|
||||
<p>Successfully tagged {progress.total} file{progress.total !== 1 ? 's' : ''}.</p>
|
||||
</Message>
|
||||
{error && (
|
||||
<Message warning>
|
||||
<Message.Header>Warning</Message.Header>
|
||||
<p>Some errors occurred: {error}</p>
|
||||
</Message>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</ModalContent>
|
||||
<ModalActions>
|
||||
{state === 'preview' && (
|
||||
<>
|
||||
<Button onClick={handleClose}>Cancel</Button>
|
||||
<Button
|
||||
color="violet"
|
||||
onClick={handleApply}
|
||||
disabled={!hasChanges || fileCount === 0}
|
||||
>
|
||||
Apply Tags
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
{state === 'applying' && (
|
||||
<Button onClick={handleClose} disabled>
|
||||
Please wait...
|
||||
</Button>
|
||||
)}
|
||||
{state === 'complete' && (
|
||||
<Button color="green" onClick={handleClose}>
|
||||
Done
|
||||
</Button>
|
||||
)}
|
||||
</ModalActions>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
export default BulkTagModal;
|
||||
198
app/src/components/BulkTagModal.test.js
Normal file
198
app/src/components/BulkTagModal.test.js
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
import React from 'react';
|
||||
import {act, render, screen, waitFor} from '@testing-library/react';
|
||||
import {BulkTagModal} from './BulkTagModal';
|
||||
import {TagsContext} from '../Tags';
|
||||
|
||||
// Mock the API functions
|
||||
jest.mock('../api', () => ({
|
||||
getBulkTagPreview: jest.fn(),
|
||||
applyBulkTags: jest.fn(),
|
||||
getBulkTagProgress: jest.fn(),
|
||||
}));
|
||||
|
||||
// Mock Theme components
|
||||
jest.mock('./Theme', () => ({
|
||||
...jest.requireActual('./Theme'),
|
||||
Modal: ({open, onClose, children, closeIcon}) => {
|
||||
if (!open) return null;
|
||||
return (
|
||||
<div data-testid="modal">
|
||||
{closeIcon && <button data-testid="close-icon" onClick={onClose}>x</button>}
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
},
|
||||
ModalHeader: ({children}) => <div data-testid="modal-header">{children}</div>,
|
||||
ModalContent: ({children}) => <div data-testid="modal-content">{children}</div>,
|
||||
ModalActions: ({children}) => <div data-testid="modal-actions">{children}</div>,
|
||||
Button: ({children, onClick, disabled, color, ...props}) => (
|
||||
<button onClick={onClick} disabled={disabled} data-testid={`button-${children}`} {...props}>{children}</button>
|
||||
),
|
||||
Header: ({children, as}) => <div data-testid="header">{children}</div>,
|
||||
Divider: () => <hr data-testid="divider"/>,
|
||||
Loader: ({children}) => <div data-testid="loader">{children}</div>,
|
||||
Message: ({children, warning, negative, positive}) => (
|
||||
<div data-testid={`message-${warning ? 'warning' : negative ? 'negative' : positive ? 'positive' : 'info'}`}>
|
||||
{children}
|
||||
</div>
|
||||
),
|
||||
Progress: ({percent, children}) => (
|
||||
<div data-testid="progress" data-percent={percent}>{children}</div>
|
||||
),
|
||||
}));
|
||||
|
||||
// Import mocked API functions
|
||||
import {getBulkTagPreview, applyBulkTags, getBulkTagProgress} from '../api';
|
||||
|
||||
// Mock TagsGroup component for testing
|
||||
const MockTagsGroup = ({tagNames, onClick}) => (
|
||||
<div data-testid="tags-group">
|
||||
{tagNames?.map(name => (
|
||||
<span key={name} onClick={() => onClick && onClick(name)} data-testid={`tag-${name}`}>
|
||||
{name}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
|
||||
// Create a wrapper with TagsContext
|
||||
const TagsContextWrapper = ({children}) => {
|
||||
const tagsValue = {
|
||||
tagNames: ['tag1', 'tag2', 'tag3'],
|
||||
TagsGroup: MockTagsGroup,
|
||||
};
|
||||
return (
|
||||
<TagsContext.Provider value={tagsValue}>
|
||||
{children}
|
||||
</TagsContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
// Custom render function with TagsContext
|
||||
const renderWithTags = (ui, options) =>
|
||||
render(ui, {wrapper: TagsContextWrapper, ...options});
|
||||
|
||||
describe('BulkTagModal', () => {
|
||||
const defaultProps = {
|
||||
open: true,
|
||||
onClose: jest.fn(),
|
||||
paths: ['file1.txt', 'file2.txt'],
|
||||
onComplete: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
getBulkTagPreview.mockResolvedValue({
|
||||
file_count: 2,
|
||||
shared_tag_names: ['shared'],
|
||||
});
|
||||
getBulkTagProgress.mockResolvedValue({
|
||||
status: 'idle',
|
||||
total: 0,
|
||||
completed: 0,
|
||||
queued_jobs: 0,
|
||||
});
|
||||
});
|
||||
|
||||
describe('Modal Rendering', () => {
|
||||
it('renders modal when open is true', async () => {
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
expect(screen.getByTestId('modal')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('does not render modal when open is false', () => {
|
||||
renderWithTags(<BulkTagModal {...defaultProps} open={false}/>);
|
||||
expect(screen.queryByTestId('modal')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows header', async () => {
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
expect(screen.getByTestId('modal-header')).toHaveTextContent('Bulk Tag Files');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Loading State', () => {
|
||||
it('shows loading state while fetching preview', async () => {
|
||||
getBulkTagPreview.mockImplementation(() => new Promise(() => {
|
||||
})); // Never resolves
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
expect(screen.getByTestId('loader')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Preview State', () => {
|
||||
it('shows file count after loading', async () => {
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/2 files will be affected/i)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('does not show warning for less than 50 files', async () => {
|
||||
getBulkTagPreview.mockResolvedValue({
|
||||
file_count: 10,
|
||||
shared_tag_names: [],
|
||||
});
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/10 files will be affected/i)).toBeInTheDocument();
|
||||
});
|
||||
expect(screen.queryByTestId('message-warning')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('Apply button is disabled when no changes', async () => {
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/2 files will be affected/i)).toBeInTheDocument();
|
||||
});
|
||||
const applyButton = screen.getByTestId('button-Apply Tags');
|
||||
expect(applyButton).toBeDisabled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Applying Tags', () => {
|
||||
it('calls applyBulkTags when Apply is clicked', async () => {
|
||||
applyBulkTags.mockResolvedValue({ok: true});
|
||||
getBulkTagProgress.mockResolvedValue({
|
||||
status: 'running',
|
||||
total: 2,
|
||||
completed: 1,
|
||||
queued_jobs: 0,
|
||||
});
|
||||
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
|
||||
// Wait for preview to load
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/2 files will be affected/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// The test would need to simulate adding a tag first
|
||||
// This is a simplified test - in real usage, user would click to add tags
|
||||
});
|
||||
});
|
||||
|
||||
describe('Close Modal', () => {
|
||||
it('calls onClose when Cancel is clicked', async () => {
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/2 files will be affected/i)).toBeInTheDocument();
|
||||
});
|
||||
const cancelButton = screen.getByTestId('button-Cancel');
|
||||
act(() => {
|
||||
cancelButton.click();
|
||||
});
|
||||
expect(defaultProps.onClose).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error Handling', () => {
|
||||
it('handles preview failure gracefully', async () => {
|
||||
// When preview returns undefined (API error), we should still see the modal
|
||||
getBulkTagPreview.mockResolvedValue(undefined);
|
||||
renderWithTags(<BulkTagModal {...defaultProps} />);
|
||||
// Modal should still be visible
|
||||
expect(screen.getByTestId('modal')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -21,6 +21,7 @@ import {useBrowseFiles, useMediaDirectory, useWROLMode} from "../hooks/customHoo
|
|||
import {FileRowTagIcon, FilesRefreshButton} from "./Files";
|
||||
import {FilePreviewContext} from "./FilePreview";
|
||||
import {SettingsContext} from "../contexts/contexts";
|
||||
import {BulkTagModal} from "./BulkTagModal";
|
||||
|
||||
function depthIndentation(path) {
|
||||
// Repeated spaces for every folder a path is in.
|
||||
|
|
@ -137,6 +138,7 @@ export function FileBrowser() {
|
|||
const [moveOpen, setMoveOpen] = React.useState(false);
|
||||
const [makeDirectoryOpen, setMakeDirectoryOpen] = React.useState(false);
|
||||
const [ignoreDirectoryOpen, setIgnoreDirectoryOpen] = React.useState(false);
|
||||
const [bulkTagOpen, setBulkTagOpen] = React.useState(false);
|
||||
const [pending, setPending] = React.useState(false);
|
||||
// Only true if only one path is selected, and it is a directory.
|
||||
const [singleDirectorySelected, setSingleDirectorySelected] = React.useState(false);
|
||||
|
|
@ -242,7 +244,7 @@ export function FileBrowser() {
|
|||
onSubmit={reset}
|
||||
/> : null}
|
||||
<Button icon='move'
|
||||
color='violet'
|
||||
color='teal'
|
||||
disabled={wrolModeEnabled || selectedPathsCount === 0}
|
||||
onClick={() => setMoveOpen(true)}
|
||||
/>
|
||||
|
|
@ -282,6 +284,18 @@ export function FileBrowser() {
|
|||
directory={selectedPaths.length === 1 ? selectedPaths[0] : null}
|
||||
ignored={isDirectoryIgnored}
|
||||
/>
|
||||
<Button
|
||||
color='violet'
|
||||
icon='tags'
|
||||
disabled={selectedPathsCount === 0}
|
||||
onClick={() => setBulkTagOpen(true)}
|
||||
/>
|
||||
<BulkTagModal
|
||||
open={bulkTagOpen}
|
||||
onClose={() => setBulkTagOpen(false)}
|
||||
paths={selectedPaths}
|
||||
onComplete={reset}
|
||||
/>
|
||||
</TableHeaderCell>
|
||||
</TableRow>
|
||||
</TableFooter>;
|
||||
|
|
|
|||
|
|
@ -59,6 +59,10 @@ def attach_shared_contexts(app: Sanic):
|
|||
app.shared_ctx.archive_singlefiles = multiprocessing.Queue()
|
||||
app.shared_ctx.archive_screenshots = multiprocessing.Queue()
|
||||
|
||||
# Bulk tagging
|
||||
app.shared_ctx.bulk_tag = manager.dict()
|
||||
app.shared_ctx.bulk_tag_queue = manager.Queue()
|
||||
|
||||
# Warnings
|
||||
app.shared_ctx.warn_once = manager.dict()
|
||||
|
||||
|
|
@ -134,6 +138,23 @@ def reset_shared_contexts(app: Sanic):
|
|||
except queue.Empty:
|
||||
break
|
||||
|
||||
# Bulk tagging
|
||||
app.shared_ctx.bulk_tag.clear()
|
||||
app.shared_ctx.bulk_tag.update(dict(
|
||||
status='idle', # 'idle', 'running'
|
||||
total=0,
|
||||
completed=0,
|
||||
add_tag_names=[],
|
||||
remove_tag_names=[],
|
||||
error=None,
|
||||
))
|
||||
while True:
|
||||
# Clear out any pending bulk tag jobs.
|
||||
try:
|
||||
app.shared_ctx.bulk_tag_queue.get_nowait()
|
||||
except queue.Empty:
|
||||
break
|
||||
|
||||
# Events.
|
||||
app.shared_ctx.single_tasks_started.clear()
|
||||
app.shared_ctx.flags_initialized.clear()
|
||||
|
|
|
|||
|
|
@ -491,3 +491,34 @@ async def post_ignore_directory(request: Request, body: schema.Directory):
|
|||
async def post_unignore_directory(request: Request, body: schema.Directory):
|
||||
lib.remove_ignored_directory(body.path)
|
||||
return response.empty(HTTPStatus.OK)
|
||||
|
||||
|
||||
@files_bp.post('/bulk_tag/preview')
|
||||
@openapi.definition(
|
||||
summary='Get preview information for bulk tagging operation.',
|
||||
body=schema.BulkTagPreviewRequest,
|
||||
)
|
||||
@validate(schema.BulkTagPreviewRequest)
|
||||
async def post_bulk_tag_preview(_: Request, body: schema.BulkTagPreviewRequest):
|
||||
preview = lib.get_bulk_tag_preview(body.paths)
|
||||
return json_response(preview.__json__())
|
||||
|
||||
|
||||
@files_bp.post('/bulk_tag/apply')
|
||||
@openapi.definition(
|
||||
summary='Queue a bulk tagging operation.',
|
||||
body=schema.BulkTagApplyRequest,
|
||||
)
|
||||
@validate(schema.BulkTagApplyRequest)
|
||||
async def post_bulk_tag_apply(_: Request, body: schema.BulkTagApplyRequest):
|
||||
lib.queue_bulk_tag_job(body.paths, body.add_tag_names, body.remove_tag_names)
|
||||
return response.empty(HTTPStatus.ACCEPTED)
|
||||
|
||||
|
||||
@files_bp.get('/bulk_tag/progress')
|
||||
@openapi.definition(
|
||||
summary='Get progress of bulk tagging operation.',
|
||||
)
|
||||
async def get_bulk_tag_progress(_: Request):
|
||||
progress = lib.get_bulk_tag_progress()
|
||||
return json_response(progress.__json__())
|
||||
|
|
|
|||
|
|
@ -1658,3 +1658,253 @@ def get_real_path_name(path: pathlib.Path) -> pathlib.Path:
|
|||
if path_.name.lower() == path.name.lower():
|
||||
return path_
|
||||
return path
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class BulkTagPreview:
|
||||
"""Preview information for a bulk tagging operation."""
|
||||
file_count: int = 0
|
||||
shared_tag_names: List[str] = dataclasses.field(default_factory=list)
|
||||
|
||||
def __json__(self) -> dict:
|
||||
return dict(
|
||||
file_count=self.file_count,
|
||||
shared_tag_names=self.shared_tag_names,
|
||||
)
|
||||
|
||||
|
||||
def get_bulk_tag_preview(paths: List[str]) -> BulkTagPreview:
|
||||
"""Get preview information for bulk tagging operation.
|
||||
|
||||
Returns the count of files that will be tagged and the tags that are shared by ALL files.
|
||||
"""
|
||||
media_directory = get_media_directory()
|
||||
absolute_paths = [media_directory / p for p in paths]
|
||||
|
||||
# Collect all file paths (expanding directories recursively)
|
||||
all_file_paths: List[pathlib.Path] = []
|
||||
for path in absolute_paths:
|
||||
if path.is_file():
|
||||
all_file_paths.append(path)
|
||||
elif path.is_dir():
|
||||
for file in walk(path):
|
||||
if file.is_file():
|
||||
all_file_paths.append(file)
|
||||
|
||||
# Remove files in ignored directories
|
||||
all_file_paths = remove_files_in_ignored_directories(all_file_paths)
|
||||
|
||||
if not all_file_paths:
|
||||
return BulkTagPreview(file_count=0, shared_tag_names=[])
|
||||
|
||||
# Get unique files by stem (each FileGroup counted once)
|
||||
unique_files = get_unique_files_by_stem(all_file_paths)
|
||||
file_count = len(unique_files)
|
||||
|
||||
if file_count == 0:
|
||||
return BulkTagPreview(file_count=0, shared_tag_names=[])
|
||||
|
||||
# Find tags shared by ALL files
|
||||
# First, get all FileGroups and their tags
|
||||
with get_db_session() as session:
|
||||
file_groups = session.query(FileGroup).filter(
|
||||
FileGroup.primary_path.in_([str(f) for f in unique_files])
|
||||
).all()
|
||||
|
||||
if not file_groups:
|
||||
# No FileGroups exist yet, no shared tags
|
||||
return BulkTagPreview(file_count=file_count, shared_tag_names=[])
|
||||
|
||||
# Get tag names for each FileGroup
|
||||
tag_sets = []
|
||||
for fg in file_groups:
|
||||
tag_sets.append(set(fg.tag_names))
|
||||
|
||||
# Find intersection of all tag sets (tags shared by ALL files)
|
||||
if tag_sets:
|
||||
shared_tags = tag_sets[0]
|
||||
for tag_set in tag_sets[1:]:
|
||||
shared_tags = shared_tags.intersection(tag_set)
|
||||
shared_tag_names = sorted(list(shared_tags))
|
||||
else:
|
||||
shared_tag_names = []
|
||||
|
||||
return BulkTagPreview(file_count=file_count, shared_tag_names=shared_tag_names)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class BulkTagProgress:
|
||||
"""Progress information for bulk tagging operation."""
|
||||
status: str = 'idle' # 'idle', 'running'
|
||||
total: int = 0
|
||||
completed: int = 0
|
||||
add_tag_names: List[str] = dataclasses.field(default_factory=list)
|
||||
remove_tag_names: List[str] = dataclasses.field(default_factory=list)
|
||||
error: str = None
|
||||
queued_jobs: int = 0
|
||||
|
||||
def __json__(self) -> dict:
|
||||
return dict(
|
||||
status=self.status,
|
||||
total=self.total,
|
||||
completed=self.completed,
|
||||
add_tag_names=self.add_tag_names,
|
||||
remove_tag_names=self.remove_tag_names,
|
||||
error=self.error,
|
||||
queued_jobs=self.queued_jobs,
|
||||
)
|
||||
|
||||
|
||||
def get_bulk_tag_progress() -> BulkTagProgress:
|
||||
"""Get the current progress of bulk tagging operations."""
|
||||
from wrolpi.api_utils import api_app
|
||||
|
||||
bulk_tag = api_app.shared_ctx.bulk_tag
|
||||
return BulkTagProgress(
|
||||
status=bulk_tag.get('status', 'idle'),
|
||||
total=bulk_tag.get('total', 0),
|
||||
completed=bulk_tag.get('completed', 0),
|
||||
add_tag_names=list(bulk_tag.get('add_tag_names', [])),
|
||||
remove_tag_names=list(bulk_tag.get('remove_tag_names', [])),
|
||||
error=bulk_tag.get('error'),
|
||||
queued_jobs=bulk_tag.get('queued_jobs', 0),
|
||||
)
|
||||
|
||||
|
||||
def queue_bulk_tag_job(paths: List[str], add_tag_names: List[str], remove_tag_names: List[str]):
|
||||
"""Add a bulk tagging job to the queue."""
|
||||
from wrolpi.api_utils import api_app
|
||||
|
||||
job = dict(
|
||||
paths=paths,
|
||||
add_tag_names=add_tag_names,
|
||||
remove_tag_names=remove_tag_names,
|
||||
)
|
||||
api_app.shared_ctx.bulk_tag_queue.put(job)
|
||||
|
||||
# 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
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
async def _process_bulk_tag_job(job: dict):
|
||||
"""Process a single bulk tagging job."""
|
||||
from wrolpi.api_utils import api_app
|
||||
|
||||
paths = job['paths']
|
||||
add_tag_names = job['add_tag_names']
|
||||
remove_tag_names = job['remove_tag_names']
|
||||
|
||||
media_directory = get_media_directory()
|
||||
absolute_paths = [media_directory / p for p in paths]
|
||||
|
||||
# Collect all file paths (expanding directories recursively)
|
||||
all_file_paths: List[pathlib.Path] = []
|
||||
for path in absolute_paths:
|
||||
if path.is_file():
|
||||
all_file_paths.append(path)
|
||||
elif path.is_dir():
|
||||
for file in walk(path):
|
||||
if file.is_file():
|
||||
all_file_paths.append(file)
|
||||
|
||||
# Remove files in ignored directories
|
||||
all_file_paths = remove_files_in_ignored_directories(all_file_paths)
|
||||
|
||||
if not all_file_paths:
|
||||
return
|
||||
|
||||
# Get unique files by stem
|
||||
unique_files = get_unique_files_by_stem(all_file_paths)
|
||||
|
||||
# Update progress with total count
|
||||
api_app.shared_ctx.bulk_tag.update(dict(
|
||||
status='running',
|
||||
total=len(unique_files),
|
||||
completed=0,
|
||||
add_tag_names=add_tag_names,
|
||||
remove_tag_names=remove_tag_names,
|
||||
error=None,
|
||||
))
|
||||
|
||||
completed = 0
|
||||
for file_path in unique_files:
|
||||
try:
|
||||
with get_db_session(commit=True) as session:
|
||||
# Get or create FileGroup
|
||||
file_group = FileGroup.get_by_path(file_path, session=session)
|
||||
if not file_group:
|
||||
# Create FileGroup for unindexed file
|
||||
paths_for_group = glob_shared_stem(file_path)
|
||||
paths_for_group = remove_files_in_ignored_directories(paths_for_group)
|
||||
if paths_for_group:
|
||||
try:
|
||||
file_group = FileGroup.from_paths(session, *paths_for_group)
|
||||
file_group.do_model(session)
|
||||
except (NoPrimaryFile, IntegrityError) as e:
|
||||
logger.warning(f'Could not create FileGroup for {file_path}: {e}')
|
||||
continue
|
||||
|
||||
if file_group:
|
||||
# Flush to ensure file_group.id is set before tagging
|
||||
session.flush([file_group])
|
||||
# Compute the final desired tags and apply them
|
||||
current_tags = set(file_group.tag_names)
|
||||
new_tags = (current_tags | set(add_tag_names)) - set(remove_tag_names)
|
||||
file_group.set_tags(new_tags, session=session)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f'Error tagging file {file_path}: {e}', exc_info=e)
|
||||
|
||||
completed += 1
|
||||
api_app.shared_ctx.bulk_tag['completed'] = completed
|
||||
|
||||
# Sleep to allow cancellation and reduce DB pressure
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
DISABLE_BULK_TAG_WORKER = bool(PYTEST)
|
||||
|
||||
|
||||
async def bulk_tag_worker():
|
||||
"""Background worker that processes bulk tagging jobs from the queue."""
|
||||
import queue
|
||||
from wrolpi.api_utils import api_app, perpetual_signal
|
||||
|
||||
if PYTEST and DISABLE_BULK_TAG_WORKER:
|
||||
return
|
||||
|
||||
bulk_tag_ctx = api_app.shared_ctx.bulk_tag
|
||||
bulk_tag_queue = api_app.shared_ctx.bulk_tag_queue
|
||||
|
||||
try:
|
||||
# Try to get a job from the queue (non-blocking)
|
||||
job = bulk_tag_queue.get_nowait()
|
||||
except queue.Empty:
|
||||
# No jobs in queue, stay idle
|
||||
bulk_tag_ctx['status'] = 'idle'
|
||||
return
|
||||
|
||||
# Decrement queued jobs count
|
||||
try:
|
||||
current_queued = bulk_tag_ctx.get('queued_jobs', 1)
|
||||
bulk_tag_ctx['queued_jobs'] = max(0, current_queued - 1)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
await _process_bulk_tag_job(job)
|
||||
except Exception as e:
|
||||
logger.error(f'Bulk tag worker error: {e}', exc_info=e)
|
||||
bulk_tag_ctx['error'] = str(e)
|
||||
finally:
|
||||
bulk_tag_ctx['status'] = 'idle'
|
||||
|
||||
|
||||
# Register the worker as a perpetual signal
|
||||
from wrolpi.api_utils import perpetual_signal
|
||||
|
||||
bulk_tag_worker = perpetual_signal(sleep=1)(bulk_tag_worker)
|
||||
|
|
|
|||
|
|
@ -82,3 +82,17 @@ class Move:
|
|||
class Rename:
|
||||
path: str
|
||||
new_name: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class BulkTagPreviewRequest:
|
||||
"""Request to preview bulk tagging operation."""
|
||||
paths: List[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class BulkTagApplyRequest:
|
||||
"""Request to apply bulk tagging operation."""
|
||||
paths: List[str] = field(default_factory=list)
|
||||
add_tag_names: List[str] = field(default_factory=list)
|
||||
remove_tag_names: List[str] = field(default_factory=list)
|
||||
|
|
|
|||
|
|
@ -964,3 +964,85 @@ async def test_ignore_directory(test_session, async_client, test_directory, make
|
|||
request, response = await async_client.post('/api/files/ignore_directory', json=content)
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert len(get_wrolpi_config().ignored_directories) == 0
|
||||
|
||||
|
||||
# --- Bulk Tagging API Tests ---
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_tag_preview_api(test_session, async_client, make_files_structure, tag_factory):
|
||||
"""The bulk tag preview endpoint returns file count and shared tags."""
|
||||
foo, bar, baz = make_files_structure({
|
||||
'foo.txt': 'foo',
|
||||
'bar.txt': 'bar',
|
||||
'baz.txt': 'baz',
|
||||
})
|
||||
|
||||
# Create FileGroups
|
||||
fg_foo = FileGroup.from_paths(test_session, foo)
|
||||
fg_bar = FileGroup.from_paths(test_session, bar)
|
||||
fg_baz = FileGroup.from_paths(test_session, baz)
|
||||
test_session.commit()
|
||||
|
||||
# Add shared and non-shared tags
|
||||
tag1 = await tag_factory('shared_tag')
|
||||
tag2 = await tag_factory('only_foo')
|
||||
fg_foo.add_tag(tag1.id)
|
||||
fg_foo.add_tag(tag2.id)
|
||||
fg_bar.add_tag(tag1.id)
|
||||
fg_baz.add_tag(tag1.id)
|
||||
test_session.commit()
|
||||
|
||||
content = {'paths': ['foo.txt', 'bar.txt', 'baz.txt']}
|
||||
request, response = await async_client.post('/api/files/bulk_tag/preview', json=content)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert response.json['file_count'] == 3
|
||||
assert 'shared_tag' in response.json['shared_tag_names']
|
||||
assert 'only_foo' not in response.json['shared_tag_names']
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_tag_preview_empty(test_session, async_client):
|
||||
"""The bulk tag preview endpoint handles empty paths."""
|
||||
content = {'paths': []}
|
||||
request, response = await async_client.post('/api/files/bulk_tag/preview', json=content)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert response.json['file_count'] == 0
|
||||
assert response.json['shared_tag_names'] == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_tag_apply_api(test_session, async_client, make_files_structure, tag_factory):
|
||||
"""The bulk tag apply endpoint queues a tagging job."""
|
||||
foo, bar = make_files_structure({
|
||||
'foo.txt': 'foo',
|
||||
'bar.txt': 'bar',
|
||||
})
|
||||
|
||||
# Create FileGroups
|
||||
FileGroup.from_paths(test_session, foo)
|
||||
FileGroup.from_paths(test_session, bar)
|
||||
test_session.commit()
|
||||
|
||||
# Create a tag
|
||||
tag = await tag_factory('api_tag')
|
||||
test_session.commit()
|
||||
|
||||
content = {
|
||||
'paths': ['foo.txt', 'bar.txt'],
|
||||
'add_tag_names': ['api_tag'],
|
||||
'remove_tag_names': [],
|
||||
}
|
||||
request, response = await async_client.post('/api/files/bulk_tag/apply', json=content)
|
||||
assert response.status_code == HTTPStatus.ACCEPTED
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_tag_progress_api(test_session, async_client):
|
||||
"""The bulk tag progress endpoint returns progress information."""
|
||||
request, response = await async_client.get('/api/files/bulk_tag/progress')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert 'status' in response.json
|
||||
assert 'total' in response.json
|
||||
assert 'completed' in response.json
|
||||
assert 'queued_jobs' in response.json
|
||||
|
|
|
|||
|
|
@ -1170,3 +1170,138 @@ def test_get_real_path_name(test_directory):
|
|||
|
||||
assert (test_directory / 'FOO.TXT').exists()
|
||||
assert lib.get_real_path_name(test_directory / 'FOO.txt') == test_directory / 'foo.txt'
|
||||
|
||||
|
||||
# --- Bulk Tagging Tests ---
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_bulk_tag_preview_files(async_client, test_session, make_files_structure, tag_factory):
|
||||
"""get_bulk_tag_preview returns correct file count and shared tags for files."""
|
||||
foo, bar, baz = make_files_structure({
|
||||
'foo.txt': 'foo',
|
||||
'bar.txt': 'bar',
|
||||
'baz.txt': 'baz',
|
||||
})
|
||||
|
||||
# Create FileGroups
|
||||
fg_foo = FileGroup.from_paths(test_session, foo)
|
||||
fg_bar = FileGroup.from_paths(test_session, bar)
|
||||
fg_baz = FileGroup.from_paths(test_session, baz)
|
||||
test_session.commit()
|
||||
|
||||
# Add shared and non-shared tags
|
||||
tag1 = await tag_factory('shared')
|
||||
tag2 = await tag_factory('only_foo')
|
||||
fg_foo.add_tag(tag1.id)
|
||||
fg_foo.add_tag(tag2.id)
|
||||
fg_bar.add_tag(tag1.id)
|
||||
fg_baz.add_tag(tag1.id)
|
||||
test_session.commit()
|
||||
|
||||
# Preview for all three files
|
||||
preview = lib.get_bulk_tag_preview(['foo.txt', 'bar.txt', 'baz.txt'])
|
||||
assert preview.file_count == 3
|
||||
assert 'shared' in preview.shared_tag_names
|
||||
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):
|
||||
"""get_bulk_tag_preview recursively finds files in directories."""
|
||||
make_files_structure({
|
||||
'mydir/foo.txt': 'foo',
|
||||
'mydir/bar.txt': 'bar',
|
||||
'mydir/subdir/baz.txt': 'baz',
|
||||
})
|
||||
|
||||
# Preview for the directory - should find all files recursively
|
||||
preview = lib.get_bulk_tag_preview(['mydir/'])
|
||||
assert preview.file_count == 3
|
||||
|
||||
|
||||
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([])
|
||||
assert preview.file_count == 0
|
||||
assert preview.shared_tag_names == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize('test_case,expected_fg_count', [
|
||||
('existing_filegroups', 2),
|
||||
('creates_filegroups', 2),
|
||||
('recursive_directory', 2),
|
||||
('multi_file_filegroup', 1),
|
||||
])
|
||||
async def test_process_bulk_tag_job_add_tags(test_case, expected_fg_count, async_client, test_session,
|
||||
make_files_structure, tag_factory, video_bytes, srt_text):
|
||||
"""_process_bulk_tag_job adds tags to files in various scenarios."""
|
||||
if test_case == 'existing_filegroups':
|
||||
foo, bar = make_files_structure({'foo.txt': 'foo', 'bar.txt': 'bar'})
|
||||
FileGroup.from_paths(test_session, foo)
|
||||
FileGroup.from_paths(test_session, bar)
|
||||
test_session.commit()
|
||||
job_paths = ['foo.txt', 'bar.txt']
|
||||
elif test_case == 'creates_filegroups':
|
||||
make_files_structure({'foo.txt': 'foo', 'bar.txt': 'bar'})
|
||||
job_paths = ['foo.txt', 'bar.txt']
|
||||
elif test_case == 'recursive_directory':
|
||||
make_files_structure({'mydir/foo.txt': 'foo', 'mydir/subdir/bar.txt': 'bar'})
|
||||
job_paths = ['mydir/']
|
||||
elif test_case == 'multi_file_filegroup':
|
||||
make_files_structure({
|
||||
'video.mp4': video_bytes,
|
||||
'video.srt': srt_text,
|
||||
'video.info.json': '{"title": "Test Video"}',
|
||||
})
|
||||
job_paths = ['video.mp4']
|
||||
|
||||
tag = await tag_factory('new_tag')
|
||||
test_session.commit()
|
||||
|
||||
job = {'paths': job_paths, 'add_tag_names': ['new_tag'], 'remove_tag_names': []}
|
||||
await lib._process_bulk_tag_job(job)
|
||||
|
||||
test_session.expire_all()
|
||||
fgs = test_session.query(FileGroup).all()
|
||||
assert len(fgs) == expected_fg_count
|
||||
for fg in fgs:
|
||||
assert 'new_tag' in fg.tag_names
|
||||
|
||||
# Additional assertion for multi-file FileGroup
|
||||
if test_case == 'multi_file_filegroup':
|
||||
file_paths = {f['path'].name for f in fgs[0].files}
|
||||
assert file_paths == {'video.mp4', 'video.srt', 'video.info.json'}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_bulk_tag_job_remove_tags(async_client, test_session, make_files_structure, tag_factory):
|
||||
"""_process_bulk_tag_job removes tags from files."""
|
||||
foo, bar = make_files_structure({
|
||||
'foo.txt': 'foo',
|
||||
'bar.txt': 'bar',
|
||||
})
|
||||
|
||||
# Create FileGroups with a tag
|
||||
fg_foo = FileGroup.from_paths(test_session, foo)
|
||||
fg_bar = FileGroup.from_paths(test_session, bar)
|
||||
test_session.commit()
|
||||
|
||||
tag = await tag_factory('to_remove')
|
||||
fg_foo.add_tag(tag.id)
|
||||
fg_bar.add_tag(tag.id)
|
||||
test_session.commit()
|
||||
|
||||
job = {
|
||||
'paths': ['foo.txt', 'bar.txt'],
|
||||
'add_tag_names': [],
|
||||
'remove_tag_names': ['to_remove'],
|
||||
}
|
||||
|
||||
await lib._process_bulk_tag_job(job)
|
||||
|
||||
# Verify tags were removed
|
||||
test_session.expire_all()
|
||||
fgs = test_session.query(FileGroup).all()
|
||||
for fg in fgs:
|
||||
assert 'to_remove' not in fg.tag_names
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue