mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-22 16:06:19 -05:00
Model reprs read only loaded state (#819)
A repr runs inside log calls. Reading a relationship or an expired column there runs a query, and on an object whose session is gone it raises DetachedInstanceError instead of logging. Archive's repr also stat'ed its SingleFile file on disk. `wrolpi.common.loaded()` follows attributes through state that is already loaded (primary keys from the identity key) and never runs SQL. Every model repr uses it; with everything loaded the output is unchanged, except Video and Archive now show the FileGroup's primary path. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c717f11821
commit
3ea8eb81f6
11 changed files with 154 additions and 52 deletions
|
|
@ -9,8 +9,8 @@ from sqlalchemy.orm import Mapped, relationship, Session, joinedload
|
|||
|
||||
from wrolpi import dates
|
||||
from wrolpi.collections import Collection
|
||||
from wrolpi.common import ModelHelper, Base, logger, get_title_from_html, get_wrolpi_config, get_media_directory, \
|
||||
strip_surrogates
|
||||
from wrolpi.common import loaded, ModelHelper, Base, logger, get_title_from_html, get_wrolpi_config, \
|
||||
get_media_directory, strip_surrogates
|
||||
from wrolpi.dates import now
|
||||
from wrolpi.db import serializer
|
||||
from wrolpi.errors import UnknownArchive
|
||||
|
|
@ -60,11 +60,11 @@ class Archive(Base, ModelHelper):
|
|||
return d
|
||||
|
||||
def __repr__(self):
|
||||
domain_name = self.domain if self.collection else None
|
||||
if domain_name:
|
||||
return f'<Archive id={self.id} url={self.file_group.url} singlefile={repr(str(self.singlefile_path))} ' \
|
||||
f'domain={domain_name}>'
|
||||
return f'<Archive id={self.id} url={self.file_group.url} singlefile={repr(str(self.singlefile_path))}>'
|
||||
id_, url = loaded(self, 'id'), loaded(self, 'file_group', 'url')
|
||||
path = repr(str(loaded(self, 'file_group', 'primary_path')))
|
||||
if domain_name := loaded(self, 'collection', 'name'):
|
||||
return f'<Archive id={id_} url={url} path={path} domain={domain_name}>'
|
||||
return f'<Archive id={id_} url={url} path={path}>'
|
||||
|
||||
@property
|
||||
def domain(self) -> str | None:
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from typing import Optional
|
|||
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, String, Text, Index, or_
|
||||
from sqlalchemy.orm import Mapped, relationship, Session, joinedload
|
||||
|
||||
from wrolpi.common import ModelHelper, Base
|
||||
from wrolpi.common import loaded, ModelHelper, Base
|
||||
from wrolpi.db import get_db_session, serializer
|
||||
from wrolpi.files.models import FileGroup
|
||||
|
||||
|
|
@ -62,8 +62,8 @@ class Doc(ModelHelper, Base):
|
|||
passive_deletes=True)
|
||||
|
||||
def __repr__(self):
|
||||
path = str(self.file_group.primary_path) if self.file_group else 'None'
|
||||
return f'<Doc id={self.id} path={repr(path)} file_group_id={self.file_group_id}>'
|
||||
path = str(loaded(self, 'file_group', 'primary_path'))
|
||||
return f'<Doc id={loaded(self, "id")} path={repr(path)} file_group_id={loaded(self, "file_group_id")}>'
|
||||
|
||||
@staticmethod
|
||||
def json_options() -> tuple:
|
||||
|
|
@ -166,4 +166,5 @@ class DocSection(ModelHelper, Base):
|
|||
doc = relationship('Doc', back_populates='sections')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<DocSection id={self.id} doc_id={self.doc_id} kind={self.kind} ordinal={self.ordinal}>'
|
||||
return f'<DocSection id={loaded(self, "id")} doc_id={loaded(self, "doc_id")} kind={loaded(self, "kind")} ' \
|
||||
f'ordinal={loaded(self, "ordinal")}>'
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from sqlalchemy import Column, Integer, String, ForeignKey, BigInteger, Text, Bo
|
|||
from sqlalchemy.orm import relationship, Session, joinedload, selectinload, deferred
|
||||
|
||||
from wrolpi.collections.models import Collection
|
||||
from wrolpi.common import Base, ModelHelper, get_relative_to_media_directory
|
||||
from wrolpi.common import loaded, Base, ModelHelper, get_relative_to_media_directory
|
||||
from wrolpi.dates import TZDateTime
|
||||
from wrolpi.db import serializer
|
||||
from wrolpi.downloader import Download
|
||||
|
|
@ -57,7 +57,8 @@ class Repository(ModelHelper, Base):
|
|||
uselist=False)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Repository id={self.id} url={self.url} directory={self.directory}>'
|
||||
return f'<Repository id={loaded(self, "id")} url={loaded(self, "url")} ' \
|
||||
f'directory={loaded(self, "collection", "directory")}>'
|
||||
|
||||
@property
|
||||
def name(self) -> Optional[str]:
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ from sqlalchemy.orm import Mapped, relationship, Session, deferred, undefer, joi
|
|||
|
||||
from modules.videos.errors import UnknownVideo, UnknownChannel
|
||||
from wrolpi.captions import read_captions, read_captions_with_timestamps
|
||||
from wrolpi.common import Base, ModelHelper, logger, get_media_directory, get_relative_to_media_directory, \
|
||||
from wrolpi.common import loaded, Base, ModelHelper, logger, get_media_directory, get_relative_to_media_directory, \
|
||||
background_task
|
||||
from wrolpi.db import get_db_curs, get_db_session, session_curs, serializer
|
||||
from wrolpi.collections.models import Collection
|
||||
|
|
@ -53,11 +53,10 @@ class Video(ModelHelper, Base):
|
|||
file_group: Mapped[FileGroup] = relationship('FileGroup')
|
||||
|
||||
def __repr__(self):
|
||||
v = None
|
||||
if self.video_path:
|
||||
v = repr(str(self.video_path.relative_to(get_media_directory())))
|
||||
return f'<Video id={self.id} title={repr(self.file_group.title)} path={v} channel={self.channel_id} ' \
|
||||
f'source_id={repr(self.source_id)}>'
|
||||
title = loaded(self, 'file_group', 'title')
|
||||
path = repr(str(loaded(self, 'file_group', 'primary_path')))
|
||||
return f'<Video id={loaded(self, "id")} title={repr(title)} path={path} channel={loaded(self, "channel_id")} ' \
|
||||
f'source_id={repr(loaded(self, "source_id"))}>'
|
||||
|
||||
@staticmethod
|
||||
def json_options() -> tuple:
|
||||
|
|
@ -665,7 +664,8 @@ class Channel(ModelHelper, Base):
|
|||
return [d for d in self.collection.downloads if d.frequency] if self.collection else []
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Channel id={self.id} name={repr(self.name)} url={self.url} directory={self.directory}>'
|
||||
return f'<Channel id={loaded(self, "id")} name={repr(loaded(self, "collection", "name"))} ' \
|
||||
f'url={loaded(self, "url")} directory={loaded(self, "collection", "directory")}>'
|
||||
|
||||
@property
|
||||
def name(self) -> str | None:
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ from sqlalchemy.orm.exc import NoResultFound # noqa
|
|||
|
||||
from modules.zim.errors import UnknownZimEntry, UnknownZimTagEntry, UnknownZim
|
||||
from wrolpi import dates, tags
|
||||
from wrolpi.common import Base, logger, get_relative_to_media_directory, ModelHelper
|
||||
from wrolpi.common import loaded, Base, logger, get_relative_to_media_directory, ModelHelper
|
||||
from wrolpi.dates import TZDateTime
|
||||
from wrolpi.db import session_curs, serializer
|
||||
from wrolpi.downloader import Download, download_manager
|
||||
|
|
@ -78,7 +78,8 @@ class Zim(Base, ModelHelper):
|
|||
auto_search = Column(Boolean, default=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Zim id={self.id} file_group_id={self.file_group_id} path={self.path}>'
|
||||
return f'<Zim id={loaded(self, "id")} file_group_id={loaded(self, "file_group_id")} ' \
|
||||
f'path={loaded(self, "path")}>'
|
||||
|
||||
@staticmethod
|
||||
def json_options() -> tuple:
|
||||
|
|
@ -366,13 +367,11 @@ class TagZimEntry(Base):
|
|||
created_at: Mapped[datetime] = Column(TZDateTime, default=dates.now)
|
||||
|
||||
def __repr__(self):
|
||||
tag = self.tag_id
|
||||
if self.tag:
|
||||
tag = self.tag.name
|
||||
zim = self.zim_id
|
||||
if self.zim:
|
||||
zim = get_relative_to_media_directory(self.zim.path)
|
||||
return f'<TagZimEntry {tag=} zim={str(zim)} zim_entry={self.zim_entry}>'
|
||||
tag = loaded(self, 'tag', 'name') or loaded(self, 'tag_id')
|
||||
zim = loaded(self, 'zim_id')
|
||||
if zim_path := loaded(self, 'zim', 'path'):
|
||||
zim = get_relative_to_media_directory(zim_path)
|
||||
return f'<TagZimEntry {tag=} zim={str(zim)} zim_entry={loaded(self, "zim_entry")}>'
|
||||
|
||||
@staticmethod
|
||||
def get_by_primary_keys(tag_id: int, zim_id: int, zim_entry: str, session: Session):
|
||||
|
|
@ -391,10 +390,10 @@ class ZimSubscription(Base):
|
|||
download: Mapped[Download] = relationship('Download', primaryjoin='ZimSubscription.download_id==Download.id')
|
||||
|
||||
def __repr__(self):
|
||||
name = self.name
|
||||
language = self.language
|
||||
download_id = self.download_id
|
||||
return f'<ZimSubscription id={self.id} {name=} {language=} {download_id=}>'
|
||||
name = loaded(self, 'name')
|
||||
language = loaded(self, 'language')
|
||||
download_id = loaded(self, 'download_id')
|
||||
return f'<ZimSubscription id={loaded(self, "id")} {name=} {language=} {download_id=}>'
|
||||
|
||||
@staticmethod
|
||||
def json_options() -> tuple:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue