mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 11:26:25 -05:00
Creating Video/Archive get_by_id/find_by_id methods. find_by_id will raise an exception when not found, get_by_id will not.
This commit is contained in:
parent
7f54e592ed
commit
14017d43bb
15 changed files with 78 additions and 43 deletions
|
|
@ -304,9 +304,7 @@ def get_title_from_html(html: str, url: str = None) -> str:
|
|||
@optional_session
|
||||
def get_archive(session, archive_id: int) -> Archive:
|
||||
"""Get an Archive."""
|
||||
archive = session.query(Archive).filter_by(id=archive_id).one_or_none()
|
||||
if not archive:
|
||||
raise UnknownArchive(f'Could not find archive with id: {archive_id}')
|
||||
archive = Archive.find_by_id(archive_id, session=session)
|
||||
return archive
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ from sqlalchemy.orm.collections import InstrumentedList
|
|||
|
||||
from wrolpi.common import ModelHelper, Base, logger
|
||||
from wrolpi.dates import TZDateTime
|
||||
from wrolpi.db import optional_session
|
||||
from wrolpi.errors import UnknownArchive
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.media_path import MediaPathType
|
||||
from wrolpi.tags import Tag, TagFile
|
||||
|
|
@ -43,6 +45,24 @@ class Archive(Base, ModelHelper):
|
|||
f'domain={self.domain.domain}>'
|
||||
return f'<Archive id={self.id} url={self.url} singlefile={repr(str(self.singlefile_path))}>'
|
||||
|
||||
@staticmethod
|
||||
@optional_session
|
||||
def get_by_id(id_: int, session: Session = None) -> Optional[Base]:
|
||||
"""Attempt to find an Archive with the provided id. Returns None if it cannot be found."""
|
||||
archive = session.query(Archive).filter_by(id=id_).one_or_none()
|
||||
return archive
|
||||
|
||||
@staticmethod
|
||||
@optional_session
|
||||
def find_by_id(id_: int, session: Session = None) -> Base:
|
||||
"""Find an Archive with the provided id, raises an exception when no Archive is found.
|
||||
|
||||
@raise UnknownArchive: if the Archive can not be found"""
|
||||
archive = Archive.get_by_id(id_, session=session)
|
||||
if not archive:
|
||||
raise UnknownArchive(f'Cannot find Archive with id {id_}')
|
||||
return archive
|
||||
|
||||
def my_paths(self, *mimetypes: str) -> List[pathlib.Path]:
|
||||
return self.file_group.my_paths(*mimetypes)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue