Creating Video/Archive my_files to avoid checking all files.

This commit is contained in:
Roland 2022-10-24 17:50:25 -06:00
parent e504e595c0
commit 76a9c0d88c
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 51 additions and 77 deletions

View file

@ -49,7 +49,7 @@ class Archive(Base, ModelHelper):
f'domain={self.domain.domain}>'
return f'<Archive id={self.id} url={self.url} singlefile={repr(str(self.singlefile_path))}>'
def my_paths(self) -> Generator:
def my_paths(self) -> Generator[pathlib.Path, None, None]:
if self.singlefile_path:
yield self.singlefile_path
if self.readability_path:
@ -61,6 +61,18 @@ class Archive(Base, ModelHelper):
if self.screenshot_path:
yield self.screenshot_path
def my_files(self) -> Generator[File, None, None]:
if self.singlefile_file:
yield self.singlefile_file
if self.readability_file:
yield self.readability_file
if self.readability_json_file:
yield self.readability_json_file
if self.readability_txt_file:
yield self.readability_txt_file
if self.screenshot_file:
yield self.screenshot_file
def unlink(self):
"""
Remove any files in this archive.
@ -101,16 +113,8 @@ class Archive(Base, ModelHelper):
session = Session.object_session(self)
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)
for file in self.my_files():
session.delete(file)
session.delete(self)
@ -131,19 +135,6 @@ class Archive(Base, ModelHelper):
).order_by(Archive.archive_datetime))
return alternatives
def my_files(self) -> Generator[pathlib.Path, None, None]:
"""Return all paths of this Archive. Returns nothing if all paths are None.""" # noqa
if self.singlefile_path:
yield self.singlefile_path
if self.readability_path:
yield self.readability_path
if self.readability_txt_path:
yield self.readability_txt_path
if self.readability_json_path:
yield self.readability_json_path
if self.screenshot_path:
yield self.screenshot_path
@staticmethod
def find_by_path(path, session: Session) -> Base:
archive = session.query(Archive).filter_by(singlefile_path=path).one_or_none()

View file

@ -100,27 +100,27 @@ def test_archive_refresh_deleted_archive(test_client, test_session, archive_dire
check_counts(archive_count=5, domain_count=1)
# Delete archive2's files, it's the latest for 'https://example.com/1'
for path in archive2.my_files():
for path in archive2.my_paths():
path.unlink()
test_client.post('/api/files/refresh')
check_counts(archive_count=4, domain_count=1)
# Delete archive1's files, now the URL is empty.
for path in archive1.my_files():
for path in archive1.my_paths():
path.unlink()
test_client.post('/api/files/refresh')
check_counts(archive_count=3, domain_count=0)
# Delete archive3, now there is now example.com domain
for path in archive3.my_files():
for path in archive3.my_paths():
path.unlink()
test_client.post('/api/files/refresh')
check_counts(archive_count=2, domain_count=0)
# Delete all the rest of the archives
for path in archive4.my_files():
for path in archive4.my_paths():
path.unlink()
for path in archive5.my_files():
for path in archive5.my_paths():
path.unlink()
test_client.post('/api/files/refresh')
check_counts(archive_count=0, domain_count=0)
@ -179,9 +179,9 @@ def test_delete_archive(test_session, archive_factory):
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 archive1.my_paths() and all(i.is_file() for i in archive1.my_paths())
assert archive2.my_paths() and all(i.is_file() for i in archive2.my_paths())
assert archive3.my_paths() and all(i.is_file() for i in archive3.my_paths())
assert test_session.query(Archive).count() == 3
assert test_session.query(File).count() == 15
@ -191,10 +191,10 @@ def test_delete_archive(test_session, archive_factory):
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())
assert archive1.my_paths() and not any(i.is_file() for i in archive1.my_paths())
assert archive3.my_paths() and not any(i.is_file() for i in archive3.my_paths())
# Archive2 is untouched
assert archive2.my_files() and all(i.is_file() for i in archive2.my_files())
assert archive2.my_paths() and all(i.is_file() for i in archive2.my_paths())
# Delete the last archive. The Domain should also be deleted.
delete_archives(archive2.id)
@ -204,7 +204,7 @@ def test_delete_archive(test_session, archive_factory):
# 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())
assert archive2.my_paths() and not any(i.is_file() for i in archive2.my_paths())
def test_get_domains(test_session, archive_factory):
@ -617,9 +617,9 @@ def test_migrate_archive_files(test_session, archive_directory, archive_factory)
'example.org/2000-01-01-00-00-00_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.readability.json')),
]
# New Archive files still exist.
assert all(i.is_file() for i in archive1.my_files())
assert all(i.is_file() for i in archive2.my_files())
assert all(i.is_file() for i in archive3.my_files())
assert all(i.is_file() for i in archive1.my_paths())
assert all(i.is_file() for i in archive2.my_paths())
assert all(i.is_file() for i in archive3.my_paths())
def test_archive_files(test_directory):

View file

@ -75,45 +75,34 @@ class Video(ModelHelper, Base):
return video
def dict(self) -> dict:
d = super().dict()
if self.channel_id:
d['channel'] = self.channel.dict()
d['info_json'] = self.get_info_json()
return d
def my_paths(self) -> Generator[Path, None, None]:
"""Return all paths of this Video. Returns nothing if all paths are None."""
if self.poster_path:
yield self.poster_path
if self.caption_path:
yield self.caption_path
if self.info_json_path:
yield self.info_json_path
if self.video_path:
yield self.video_path
def get_minimize(self) -> dict:
"""
Get a dictionary representation of this Video suitable for sending out the API.
"""
from .common import minimize_video
return minimize_video(self.dict())
def _clear_files(self):
# Use setattr to avoid confusing PyCharm
setattr(self, 'caption_file', None)
setattr(self, 'description_file', None)
setattr(self, 'ext', None)
setattr(self, 'info_json_file', None)
setattr(self, 'poster_file', None)
setattr(self, 'video_file', None)
def my_files(self) -> Generator[Path, None, None]:
"""Return all paths of this Video. Returns nothing if all paths are None.""" # noqa
def my_files(self) -> Generator[File, None, None]:
"""Return all Files of this Video. Returns nothing if all paths are None."""
if self.poster_file:
yield self.poster_file.path
yield self.poster_file
if self.caption_file:
yield self.caption_file.path
yield self.caption_file
if self.info_json_file:
yield self.info_json_file.path
yield self.info_json_file
if self.video_file:
yield self.video_file.path
yield self.video_file
def delete(self):
"""
Remove all files related to this video. Add it to it's Channel's skip list.
Remove all files and File records related to this video. Delete this Video record.
Add it to it's Channel's skip list.
"""
for path in self.my_files():
for path in self.my_paths():
path.unlink(missing_ok=True)
needs_save = bool(self.favorite)
@ -124,14 +113,8 @@ class Video(ModelHelper, Base):
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)
for file in self.my_files():
session.delete(file)
if needs_save:
# Save the Channels config. This video was a favorite and that needs to be removed.