From fb5e12580ec5927beca6f6c4939be5a7ff46afb5 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 24 Dec 2025 09:14:55 -0700 Subject: [PATCH] Renaming direcotries also moves collections. --- wrolpi/collections/models.py | 59 +++++++++++++----------- wrolpi/files/lib.py | 44 +++++++++++++----- wrolpi/files/test/test_api.py | 84 +++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 37 deletions(-) diff --git a/wrolpi/collections/models.py b/wrolpi/collections/models.py index a792f2e0..b6798df4 100644 --- a/wrolpi/collections/models.py +++ b/wrolpi/collections/models.py @@ -912,10 +912,14 @@ class Collection(ModelHelper, Base): return destination - async def move_collection(self, directory: pathlib.Path, session: Session, send_events: bool = False): + async def move_collection(self, directory: pathlib.Path, session: Session, send_events: bool = False, + with_files: bool = True): """Move all files under this Collection's directory to a new directory. Also updates download destinations for downloads associated with this collection. + + If with_files=False, only updates the directory reference without moving files. + Use this when files have already been moved by a parent directory rename. """ if not directory.is_dir(): raise FileNotFoundError(f'Destination directory does not exist: {directory}') @@ -948,32 +952,37 @@ class Collection(ModelHelper, Base): save_downloads_config.activate_switch() save_tags_config.activate_switch() - try: - if not old_directory.exists(): - # Old directory does not exist; refresh both - await refresh_files([old_directory, directory]) + if with_files: + try: + if not old_directory.exists(): + # Old directory does not exist; refresh both + await refresh_files([old_directory, directory]) + if send_events: + Events.send_file_move_completed(f'Collection {repr(self.name)} was moved (directory missing)') + else: + files_to_move = list(old_directory.iterdir()) + if __debug__ and logger.isEnabledFor(TRACE_LEVEL): + logger.trace(f'move_collection: moving {len(files_to_move)} items from {old_directory}') + await move_files(session, directory, *files_to_move) + if send_events: + Events.send_file_move_completed(f'Collection {repr(self.name)} was moved') + except Exception as e: + logger.error(f'Collection move failed! Reverting changes...', exc_info=e) + # Revert download destinations + change_download_destinations(directory, old_directory) + self.directory = old_directory + self.flush(session) if send_events: - Events.send_file_move_completed(f'Collection {repr(self.name)} was moved (directory missing)') - else: - files_to_move = list(old_directory.iterdir()) - if __debug__ and logger.isEnabledFor(TRACE_LEVEL): - logger.trace(f'move_collection: moving {len(files_to_move)} items from {old_directory}') - await move_files(session, directory, *files_to_move) - if send_events: - Events.send_file_move_completed(f'Collection {repr(self.name)} was moved') - except Exception as e: - logger.error(f'Collection move failed! Reverting changes...', exc_info=e) - # Revert download destinations - change_download_destinations(directory, old_directory) - self.directory = old_directory - self.flush(session) + Events.send_file_move_failed(f'Moving Collection {self.name} has failed') + raise + finally: + session.commit() + if old_directory.exists() and not next(iter(old_directory.iterdir()), None): + old_directory.rmdir() + else: + # Files already moved, just send event if send_events: - Events.send_file_move_failed(f'Moving Collection {self.name} has failed') - raise - finally: - session.commit() - if old_directory.exists() and not next(iter(old_directory.iterdir()), None): - old_directory.rmdir() + Events.send_file_move_completed(f'Collection {repr(self.name)} directory updated') class CollectionItem(ModelHelper, Base): diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index 2ecefe6a..35847b35 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -1615,18 +1615,38 @@ async def rename_directory(session: Session, directory: pathlib.Path, new_name: if new_directory.exists(): raise FileConflict(f'Cannot rename {directory} to {new_directory} because it already exists.') - # Move all paths into the new directory. - paths = list(directory.iterdir()) - try: - await move(session, new_directory, *paths) - if send_events: - Events.send_file_move_completed(f'Directory has been renamed: {directory}') - except Exception: - if send_events: - Events.send_file_move_failed(f'Directory rename has failed: {directory}') - raise - # Remove the old directory. - delete_directory(directory) + # Check if this directory is a Collection's directory + from wrolpi.collections.models import Collection + collection: Collection = session.query(Collection).filter(Collection.directory == directory).one_or_none() + + if collection: + # Use Collection's move method which handles files, config saves, and cleanup + new_directory.mkdir(parents=True, exist_ok=True) + await collection.move_collection(new_directory, session, send_events=send_events) + else: + # Regular directory rename - move all paths into the new directory. + paths = list(directory.iterdir()) + try: + await move(session, new_directory, *paths) + if send_events: + Events.send_file_move_completed(f'Directory has been renamed: {directory}') + except Exception: + if send_events: + Events.send_file_move_failed(f'Directory rename has failed: {directory}') + raise + + # Update nested Collections (files already moved by move() above) + directory_str = str(directory) + nested_collections = session.query(Collection).filter( + Collection.directory.like(f'{directory_str}/%') + ).all() + for coll in nested_collections: + relative = coll.directory.relative_to(directory) + new_coll_dir = new_directory / relative + await coll.move_collection(new_coll_dir, session, send_events=send_events, with_files=False) + + # Remove the old directory. + delete_directory(directory) return new_directory diff --git a/wrolpi/files/test/test_api.py b/wrolpi/files/test/test_api.py index 58314cc1..7f7231bb 100644 --- a/wrolpi/files/test/test_api.py +++ b/wrolpi/files/test/test_api.py @@ -889,6 +889,90 @@ async def test_rename_directory(test_session, test_directory, make_files_structu assert foo.name == 'foo' and qux.name == 'qux' +@pytest.mark.asyncio +async def test_rename_collection_directory(test_session, test_directory, make_files_structure, async_client): + """Renaming a Collection's directory updates the Collection.directory.""" + from wrolpi.collections.models import Collection + + # Create a collection with a directory + collection_dir = test_directory / 'my_collection' + collection_dir.mkdir() + (collection_dir / 'file.txt').write_text('contents') + collection = Collection(name='Test Collection', kind='channel', directory=collection_dir) + test_session.add(collection) + test_session.commit() + collection_id = collection.id + + await lib.refresh_files() + + # Rename the directory + content = dict(path='my_collection', new_name='renamed_collection') + request, response = await async_client.post('/api/files/rename', content=json.dumps(content)) + assert response.status_code == HTTPStatus.NO_CONTENT + + # Verify Collection.directory was updated + test_session.expire_all() + collection = test_session.query(Collection).filter_by(id=collection_id).one() + assert collection.directory == test_directory / 'renamed_collection' + assert not (test_directory / 'my_collection').exists() + assert (test_directory / 'renamed_collection' / 'file.txt').is_file() + + +@pytest.mark.asyncio +async def test_rename_directory_with_nested_collections(test_session, test_directory, async_client): + """Renaming a parent directory updates nested Collection directories and download destinations.""" + from wrolpi.collections.models import Collection + from wrolpi.downloader import Download + + # Create parent directory with nested collection directories + parent_dir = test_directory / 'videos' + parent_dir.mkdir() + + channel_a_dir = parent_dir / 'channel_a' + channel_a_dir.mkdir() + (channel_a_dir / 'video1.mp4').write_bytes(b'fake video 1') + + channel_b_dir = parent_dir / 'channel_b' + channel_b_dir.mkdir() + (channel_b_dir / 'video2.mp4').write_bytes(b'fake video 2') + + # Create two channel collections for the nested directories + coll_a = Collection(name='Channel A', kind='channel', directory=channel_a_dir) + coll_b = Collection(name='Channel B', kind='channel', directory=channel_b_dir) + test_session.add_all([coll_a, coll_b]) + test_session.commit() + + # Create a download associated with channel_a + download = Download(url='https://example.com/channel_a', destination=channel_a_dir, collection_id=coll_a.id) + test_session.add(download) + test_session.commit() + coll_a_id, coll_b_id, download_id = coll_a.id, coll_b.id, download.id + + await lib.refresh_files() + + # Rename the parent directory + content = dict(path='videos', new_name='my_videos') + request, response = await async_client.post('/api/files/rename', content=json.dumps(content)) + assert response.status_code == HTTPStatus.NO_CONTENT + + # Verify nested Collection directories were updated + test_session.expire_all() + coll_a = test_session.query(Collection).filter_by(id=coll_a_id).one() + coll_b = test_session.query(Collection).filter_by(id=coll_b_id).one() + + assert coll_a.directory == test_directory / 'my_videos' / 'channel_a' + assert coll_b.directory == test_directory / 'my_videos' / 'channel_b' + + # Verify download destination was updated + download = test_session.query(Download).filter_by(id=download_id).one() + assert download.destination == test_directory / 'my_videos' / 'channel_a' + + # Verify files were moved + assert (test_directory / 'my_videos' / 'channel_a' / 'video1.mp4').exists() + assert (test_directory / 'my_videos' / 'channel_b' / 'video2.mp4').exists() + assert not (test_directory / 'videos').exists() + + @pytest.mark.asyncio async def test_delete_directory_recursive(test_session, test_directory, make_files_structure, async_client, assert_files):