From d8ef7e175ef00e9c025d869baa274df917742f94 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 6 Aug 2024 20:04:24 -0600 Subject: [PATCH] Preserving FileGroup.title when moving. --- modules/videos/test/test_models.py | 16 ++++++++++++---- wrolpi/common.py | 2 +- wrolpi/files/lib.py | 6 ++++-- wrolpi/files/models.py | 6 ++++-- wrolpi/files/test/test_lib.py | 2 ++ 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/modules/videos/test/test_models.py b/modules/videos/test/test_models.py index 436d4cb6..f1681765 100644 --- a/modules/videos/test/test_models.py +++ b/modules/videos/test/test_models.py @@ -1,9 +1,7 @@ -import pathlib - import pytest -from modules.videos.models import Video, Channel -from wrolpi.common import get_relative_to_media_directory, get_wrolpi_config +from modules.videos.models import Video +from wrolpi.common import get_relative_to_media_directory @pytest.mark.asyncio() @@ -25,13 +23,19 @@ async def test_channel_move(test_async_client, test_session, test_directory, cha """A Channel can be moved to another directory, any files in the Channel's directory are moved.""" channel = channel_factory(name='Channel Name') video = video_factory(title='Vid', channel_id=channel.id) + extra_file = (channel.directory / 'extra.txt') + extra_file.write_text('extra stuff') test_session.commit() assert video.channel_id == channel.id assert str(get_relative_to_media_directory(video.video_path)) == 'videos/Channel Name/Vid.mp4' + # Destination must already exist. foo = test_directory / 'foo/New Channel Directory' foo.mkdir(parents=True) + + # Move the Channel. await channel.move(foo, test_session) + assert channel.directory == foo assert channel.directory != (test_directory / 'New Channel Directory') video = test_session.query(Video).one() @@ -39,3 +43,7 @@ async def test_channel_move(test_async_client, test_session, test_directory, cha assert str(video.video_path).startswith(str(channel.directory)) # Old directory is deleted. assert not (test_directory / 'Channel Name').exists() + # Extra file was moved. + assert not extra_file.is_file() + assert (test_directory / 'foo/New Channel Directory/extra.txt').is_file() + assert (test_directory / 'foo/New Channel Directory/extra.txt').read_text() == 'extra stuff' diff --git a/wrolpi/common.py b/wrolpi/common.py index 2ac12c31..e9fb2e9e 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -275,7 +275,7 @@ class ModelHelper: raise NotImplementedError('This model has not defined this method.') def flush(self): - """A convenience function which flushes this record using its DB Session.""" + """A convenience method which flushes this record using its DB Session.""" if session := Session.object_session(self): session.flush([self]) else: diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index b1a0133d..e6fb16a0 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -1276,11 +1276,11 @@ async def move(destination: pathlib.Path, *sources: pathlib.Path) -> List[Tuple[ # Files were moved, migrate any Tags. plan_map = {old: new for old, new in plan} + old_file_paths = list(plan_map.keys()) with get_db_session(commit=True) as session: - old_file_paths = list(plan_map.keys()) old_files: List[Tuple[FileGroup, TagFile]] = session.query(FileGroup, TagFile) \ .filter(FileGroup.primary_path.in_(old_file_paths)) \ - .join(TagFile, TagFile.file_group_id == FileGroup.id).all() + .outerjoin(TagFile).all() logger.info(f'Moving {len(old_files)} tagged files') for old_file, _ in old_files: try: @@ -1290,6 +1290,8 @@ async def move(destination: pathlib.Path, *sources: pathlib.Path) -> List[Tuple[ except KeyError as e: # Got a duplicate? logger.error('Error during tag migration', exc_info=e) + if PYTEST: + raise session.commit() if PYTEST: diff --git a/wrolpi/files/models.py b/wrolpi/files/models.py index 44ddd66e..0ca41891 100644 --- a/wrolpi/files/models.py +++ b/wrolpi/files/models.py @@ -345,12 +345,14 @@ class FileGroup(ModelHelper, Base): shutil.move(file['path'], new_path) new_files[idx]['path'] = new_path self.files = new_files + if self.title == self.primary_path.name: + # Do not overwrite title from modeler. + self.title = new_primary_path.name self.primary_path = new_primary_path - self.title = self.primary_path.name # Need to re-index for self.data. self.indexed = False # Flush the changes to the FileGroup. - Session.object_session(self).flush([self, ]) + self.flush() @property def location(self): diff --git a/wrolpi/files/test/test_lib.py b/wrolpi/files/test/test_lib.py index 450000d6..9a79584f 100644 --- a/wrolpi/files/test/test_lib.py +++ b/wrolpi/files/test/test_lib.py @@ -937,6 +937,7 @@ async def test_move_tagged(test_async_client, test_session, test_directory, make }) await lib.refresh_files() bar_file_group, foo_file_group = test_session.query(FileGroup).order_by(FileGroup.primary_path) + foo_file_group.title = 'custom title' # Should not be overwritten. bar_file_group.add_tag(tag.id) test_session.commit() @@ -962,6 +963,7 @@ async def test_move_tagged(test_async_client, test_session, test_directory, make assert not foo_file_group.tag_files assert foo_file_group.primary_path.is_file() assert foo_file_group.primary_path == new_foo == foo_file_group.files[0]['path'] + assert foo_file_group.title == 'custom title', 'Custom title should not have been overwritten.' # Rename "bar.txt" to "baz.txt" await lib.rename(new_bar, 'baz.txt')