You cannot delete a tagged FileGroup.

This commit is contained in:
Roland 2023-04-12 11:42:13 -06:00
parent 14c69f5295
commit 6fe536558a
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
9 changed files with 48 additions and 12 deletions

View file

@ -154,8 +154,6 @@ async def archive_modeler():
.outerjoin(Archive, Archive.file_group_id == FileGroup.id) \
.limit(20)
logger.debug(f'{len(list(invalid_archives))=}')
processed = 0
for file_group, archive in results:
processed += 1

View file

@ -67,11 +67,9 @@ class Archive(Base, ModelHelper):
return self.archive_datetime > other.archive_datetime
def delete(self):
self.unlink()
self.file_group.delete()
session = Session.object_session(self)
session.delete(self.file_group)
session.delete(self)
if self.domain:

View file

@ -13,7 +13,7 @@ from wrolpi.common import Base, ModelHelper, logger, get_media_directory, backgr
from wrolpi.dates import now, TZDateTime
from wrolpi.db import get_db_curs, get_db_session, optional_session
from wrolpi.downloader import Download, download_manager
from wrolpi.errors import UnknownVideo
from wrolpi.errors import UnknownVideo, FileGroupIsTagged
from wrolpi.files.lib import refresh_files, get_mimetype, split_path_stem_and_suffix
from wrolpi.files.models import FileGroup
from wrolpi.media_path import MediaPathType
@ -78,10 +78,11 @@ class Video(ModelHelper, Base):
def delete(self):
"""Remove all files and File records related to this video. Delete this Video record.
Add it to it's Channel's skip list."""
self.file_group.delete()
self.add_to_skip_list()
session = Session.object_session(self)
session.delete(self)
self.file_group.delete()
def add_to_skip_list(self):
"""Add this video to it's Channel's skip list."""

View file

@ -2,6 +2,7 @@ import pytest
from modules.videos.models import Video
from wrolpi.dates import now
from wrolpi.errors import FileGroupIsTagged
from wrolpi.files import lib as files_lib
from wrolpi.files.models import FileGroup
@ -43,6 +44,22 @@ def test_delete_video(test_session, video_factory):
assert test_session.query(FileGroup).count() == 0, 'Video files were not deleted.'
def test_delete_video_with_tag(test_session, video_factory, tag_factory):
"""You cannot delete a video with a tag."""
video = video_factory(with_video_file=True, with_poster_ext='png')
tag = tag_factory()
video.add_tag(tag)
test_session.commit()
with pytest.raises(FileGroupIsTagged):
video.delete()
# Video was not deleted.
test_session.commit()
assert test_session.query(Video).count() == 1
assert test_session.query(FileGroup).count() == 1
@pytest.mark.asyncio
async def test_video_channel_refresh(test_session, test_directory, channel_factory, video_factory):
"""A Video will be associated with a Channel when it's files are in that Channel's directory."""

View file

@ -947,7 +947,7 @@ def slow_logger(max_seconds: int, message: str):
finally:
elapsed = (now() - before).total_seconds()
if elapsed >= max_seconds:
logger.warning(message % elapsed)
logger.warning(message % dict(elapsed=elapsed))
TIMERS = dict()

View file

@ -164,6 +164,10 @@ class InvalidTag(APIError):
pass
class FileGroupIsTagged(APIError):
pass
error_codes = iter(range(1, 1000))
API_ERRORS = {
@ -362,4 +366,9 @@ API_ERRORS = {
'message': 'Tag is not allowed',
'status': HTTPStatus.BAD_REQUEST,
},
FileGroupIsTagged: {
'code': next(error_codes),
'message': 'File group is tagged',
'status': HTTPStatus.CONFLICT,
},
}

View file

@ -10,6 +10,7 @@ from sqlalchemy.orm.collections import InstrumentedList
from wrolpi.common import Base, ModelHelper, tsvector, logger, recursive_map, get_media_directory
from wrolpi.dates import TZDateTime, now, from_timestamp, strptime_ms, strftime
from wrolpi.db import optional_session
from wrolpi.errors import FileGroupIsTagged
from wrolpi.files import indexers
from wrolpi.media_path import MediaPathType
from wrolpi.tags import Tag, TagFile
@ -187,12 +188,16 @@ class FileGroup(ModelHelper, Base):
return self.my_files('application/epub', 'application/x-mobipocket-ebook')
def delete(self):
"""Delete this FileGroup record, and all of its files."""
"""Delete this FileGroup record, and all of its files.
@raise FileGroupIsTagged: If any tags are related to me."""
if self.tag_files:
raise FileGroupIsTagged(f'Cannot delete {self} because it has tags')
for path in self.my_paths():
path.unlink()
session = Session.object_session(self)
if session:
if session := Session.object_session(self):
session.delete(self)
@property

View file

@ -101,8 +101,11 @@ class Flag:
@contextlib.asynccontextmanager
async def wait_for(self, timeout: int = 0):
"""Wait for this Flag to be set."""
logger.debug(f'Waiting for flag {self.name}')
async with wait_for_flag(self, timeout=timeout):
logger.debug(f'Entered flag {self.name}')
yield
logger.debug(f'Exiting flag {self.name}')
# Set by `check_db_is_up`.

View file

@ -6,6 +6,7 @@ import yaml
from wrolpi import tags
from wrolpi.dates import now
from wrolpi.errors import FileGroupIsTagged
from wrolpi.files import lib as files_lib
from wrolpi.files.models import FileGroup
from wrolpi.tags import TagFile
@ -167,6 +168,7 @@ def test_tags_crud(test_session, test_client, example_pdf, assert_tags_config):
@pytest.mark.asyncio
async def test_delete_tagged_file(test_session, example_pdf, tag_factory):
"""You cannot delete a FileGroup if it is tagged."""
await files_lib.refresh_files()
tag = tag_factory()
@ -174,8 +176,11 @@ async def test_delete_tagged_file(test_session, example_pdf, tag_factory):
pdf.add_tag(tag)
test_session.commit()
pdf.delete()
with pytest.raises(FileGroupIsTagged):
pdf.delete()
test_session.commit()
assert test_session.query(FileGroup).count() == 1
def test_import_empty_tags_config(test_session, test_tags_config):