Preserving FileGroup.title when moving.

This commit is contained in:
Roland 2024-08-06 20:04:24 -06:00
parent 2a83517d93
commit d8ef7e175e
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 23 additions and 9 deletions

View file

@ -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'

View file

@ -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:

View file

@ -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:

View file

@ -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):

View file

@ -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')