From e504e595c0ccb1cc5fc59076d9e509924aa88a69 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 24 Oct 2022 17:37:40 -0600 Subject: [PATCH] Deleting Video/Archive File records when deleting them. --- modules/archive/models.py | 13 ++++++++++++- modules/archive/test/test_lib.py | 17 +++++++++++++++++ modules/videos/models.py | 14 +++++++++++--- modules/videos/test/test_video.py | 18 ++++++++++++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/modules/archive/models.py b/modules/archive/models.py index 3669e46e..b46ee3a6 100644 --- a/modules/archive/models.py +++ b/modules/archive/models.py @@ -101,7 +101,18 @@ class Archive(Base, ModelHelper): session = Session.object_session(self) - session.query(Archive).filter_by(id=self.id).delete() + if self.screenshot_file: + session.delete(self.screenshot_file) + if self.readability_json_file: + session.delete(self.readability_json_file) + if self.readability_file: + session.delete(self.readability_file) + if self.singlefile_file: + session.delete(self.singlefile_file) + if self.readability_txt_file: + session.delete(self.readability_txt_file) + + session.delete(self) if self.domain: domain_archives = [i.id for i in self.domain.archives] diff --git a/modules/archive/test/test_lib.py b/modules/archive/test/test_lib.py index 4390a2a9..e005bf23 100644 --- a/modules/archive/test/test_lib.py +++ b/modules/archive/test/test_lib.py @@ -178,17 +178,34 @@ def test_delete_archive(test_session, archive_factory): archive3 = archive_factory('example.com', 'https://example.com/1') test_session.commit() + # Files are real. + assert archive1.my_files() and all(i.is_file() for i in archive1.my_files()) + assert archive2.my_files() and all(i.is_file() for i in archive2.my_files()) + assert archive3.my_files() and all(i.is_file() for i in archive3.my_files()) + assert test_session.query(Archive).count() == 3 + assert test_session.query(File).count() == 15 # Delete the oldest. delete_archives(archive1.id, archive3.id) assert test_session.query(Archive).count() == 1 + assert test_session.query(File).count() == 5 + # Files were deleted. + assert archive1.my_files() and not any(i.is_file() for i in archive1.my_files()) + assert archive3.my_files() and not any(i.is_file() for i in archive3.my_files()) + # Archive2 is untouched + assert archive2.my_files() and all(i.is_file() for i in archive2.my_files()) # Delete the last archive. The Domain should also be deleted. delete_archives(archive2.id) + assert test_session.query(Archive).count() == 0 domain = test_session.query(Domain).one_or_none() assert domain is None + # All Files were deleted. + assert test_session.query(File).count() == 0 + assert archive2.my_files() and not any(i.is_file() for i in archive2.my_files()) + def test_get_domains(test_session, archive_factory): """ diff --git a/modules/videos/models.py b/modules/videos/models.py index bd71e114..0d1ab942 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -8,7 +8,7 @@ from sqlalchemy import Column, Integer, String, Boolean, JSON, Date, ARRAY, Fore from sqlalchemy.orm import relationship, Session from sqlalchemy.orm.collections import InstrumentedList -from wrolpi.common import Base, ModelHelper, logger, get_media_directory, background_task +from wrolpi.common import Base, ModelHelper, logger, get_media_directory from wrolpi.dates import now, TZDateTime from wrolpi.db import get_db_curs from wrolpi.downloader import Download, download_manager @@ -116,14 +116,22 @@ class Video(ModelHelper, Base): for path in self.my_files(): path.unlink(missing_ok=True) - self._clear_files() - needs_save = bool(self.favorite) self.add_to_skip_list() self.favorite = None session = Session.object_session(self) session.commit() + session.delete(self) + + if self.poster_file: + session.delete(self.poster_file) + if self.video_file: + session.delete(self.video_file) + if self.caption_file: + session.delete(self.caption_file) + if self.info_json_file: + session.delete(self.info_json_file) if needs_save: # Save the Channels config. This video was a favorite and that needs to be removed. diff --git a/modules/videos/test/test_video.py b/modules/videos/test/test_video.py index 9f5c4048..59e4611d 100644 --- a/modules/videos/test/test_video.py +++ b/modules/videos/test/test_video.py @@ -1,5 +1,6 @@ from modules.videos.models import Video from wrolpi.dates import now +from wrolpi.files.models import File def test_timezone(test_session): @@ -25,3 +26,20 @@ def test_delete_video_no_channel(test_session, simple_video): assert not simple_video.channel simple_video.delete() + + +def test_delete_video(test_session, simple_video, image_file): + """When a Video record is deleted, all referenced file records should be deleted.""" + simple_video.poster_file = File(path=image_file) + test_session.add(simple_video.poster_file) + test_session.commit() + + assert simple_video.video_path.is_file(), 'Video file was not created.' + assert simple_video.poster_path.is_file(), 'Video poster was not created.' + assert test_session.query(Video).count() == 1, 'Video was not created.' + assert test_session.query(File).count() == 2, 'Video file and poster file were not created.' + + simple_video.delete() + test_session.commit() + assert test_session.query(Video).count() == 0, 'Video was not deleted.' + assert test_session.query(File).count() == 0, 'Video files were not deleted.'