mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 22:46:20 -05:00
Deleting Video/Archive File records when deleting them.
This commit is contained in:
parent
797bdc431c
commit
e504e595c0
4 changed files with 58 additions and 4 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue