self.last_update = _now_string()
return super().save(conn)
- def test_deletion(self, do_raise: bool) -> bool:
- """If 'delete' flag set, return True or raise NotFound, else False."""
- if self.is_flag_set(FlagName('delete')):
- if do_raise:
- raise NotFoundException('not showing entry marked as deleted')
- return True
- return False
+ @property
+ def deleted(self) -> bool:
+ """Return if 'delete' flag set."""
+ return self.is_flag_set(FlagName('delete'))
+
+ def ensure_not_deleted(self) -> None:
+ """If 'delete' flag set, raise appropriate NotFoundException."""
+ if self.deleted:
+ raise NotFoundException('not showing entry marked as deleted')
@classmethod
def get_one(cls, conn: DbConn, id_: str | Hash) -> Self:
"""Extend super by .test_deletion."""
file = super().get_one(conn, id_)
- file.test_deletion(do_raise=True) # pylint: disable=no-member
+ file.ensure_not_deleted() # pylint: disable=no-member
# NB: mypy recognizes file as VideoFile without below assert and
# if-isinstance-else, yet less type-smart pylint only does due to the
# latter (also the reason for the disable=no-member above; but wouldn't
@classmethod
def get_all(cls, conn: DbConn) -> list[Self]:
- """Extend super by excluding matches in .test_deletion."""
- files = super().get_all(conn)
- return [f for f in files if not f.test_deletion(do_raise=False)]
+ """Extend super by excluding deleteds."""
+ return [f for f in super().get_all(conn) if not f.deleted]
+
+ @classmethod
+ def get_deleteds(cls, conn: DbConn) -> list[Self]:
+ """Get _only_ deleteds."""
+ return [f for f in super().get_all(conn) if f.deleted]
@classmethod
def get_by_yt_id(cls, conn: DbConn, yt_id: YoutubeId) -> Self:
if not row:
raise NotFoundException(f'no entry for file to Youtube ID {yt_id}')
file = cls._from_table_row(row)
- file.test_deletion(do_raise=False)
+ file.ensure_not_deleted()
return file
@classmethod
"""Return if flag of flag_name is set in flags field."""
return bool(self.flags & FILE_FLAGS[flag_name])
- def ensure_absence_if_deleted(self) -> None:
+ def ensure_unlinked_if_deleted(self) -> None:
"""If 'delete' flag set, ensure no actual file in filesystem."""
if self.is_flag_set(FlagName('delete')) and self.present:
self.unlink_locally()
@classmethod
def purge_deleteds(cls, conn: DbConn) -> None:
"""For all of .is_flag_set("deleted"), remove file _and_ DB entry."""
- for file in [f for f in cls.get_all(conn)
- if f.is_flag_set(FlagName('delete'))]:
+ for file in cls.get_deleteds(conn):
if file.present:
file.unlink_locally()
print(f'SYNC: purging off DB: {file.digest.b64} ({file.rel_path})')
yt_id=yt_id,
tags_str=VideoFile.tags_default.joined)
file.save(conn)
+ for file in VideoFile.get_deleteds(conn):
+ file.ensure_unlinked_if_deleted()
self._files = VideoFile.get_all(conn)
- for file in self._files:
- file.ensure_absence_if_deleted()
chdir(old_cwd)
conn.commit()