From 8d52b5887dbb706b79a7196acaa2dd303f2c487c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyrill=20K=C3=BCttel?= Date: Thu, 4 Jul 2024 18:10:52 +0200 Subject: [PATCH] Don't Inherit from Associable in `File` but in models which derive --- src/privatim/models/comment.py | 2 +- src/privatim/models/commentable.py | 3 ++- src/privatim/models/file.py | 5 +++-- src/privatim/orm/abstract.py | 3 +-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/privatim/models/comment.py b/src/privatim/models/comment.py index 1b87406..1737dd1 100644 --- a/src/privatim/models/comment.py +++ b/src/privatim/models/comment.py @@ -36,7 +36,7 @@ class YourModel(Base, Commentable): model = YourModel(name='stuff') model.comments.append(Comment('Interesting sqlalchemy design pattern')) - """ + """ __tablename__ = 'comments' diff --git a/src/privatim/models/commentable.py b/src/privatim/models/commentable.py index 2ebce11..30aea55 100644 --- a/src/privatim/models/commentable.py +++ b/src/privatim/models/commentable.py @@ -1,10 +1,11 @@ from privatim.models.comment import Comment from privatim.orm.associable import associated +from sqlalchemy.orm import Mapped class Commentable: """ Use this in your model to attach a list[Comment] """ - comments = associated( + comments: 'Mapped[list[Comment]]' = associated( Comment, 'comments', 'one-to-many' ) diff --git a/src/privatim/models/file.py b/src/privatim/models/file.py index 06ef876..90c82dc 100644 --- a/src/privatim/models/file.py +++ b/src/privatim/models/file.py @@ -1,10 +1,11 @@ from sqlalchemy.orm import Mapped, mapped_column, deferred from privatim.orm.abstract import AbstractFile +from privatim.orm.associable import Associable from privatim.orm.meta import UUIDStrPK from sqlalchemy import Text, ForeignKey, Integer -class GeneralFile(AbstractFile): +class GeneralFile(AbstractFile, Associable): """A general file (image, document, pdf, etc), referenced in the database. A thin wrapper around the `File` from sqlalchemy-file so that we can easily @@ -19,7 +20,7 @@ class GeneralFile(AbstractFile): } -class SearchableFile(AbstractFile): +class SearchableFile(AbstractFile, Associable): """ A file with the intention of being searchable. Should to be used with SearchableAssociatedFiles. diff --git a/src/privatim/orm/abstract.py b/src/privatim/orm/abstract.py index 46a1547..b944936 100644 --- a/src/privatim/orm/abstract.py +++ b/src/privatim/orm/abstract.py @@ -4,7 +4,6 @@ from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy_file import File -from privatim.orm.associable import Associable from privatim.orm.meta import UUIDStrPK, AttachedFile from privatim.orm import Base @@ -14,7 +13,7 @@ from privatim.types import ACL -class AbstractFile(Base, Associable): +class AbstractFile(Base): __abstract__ = True id: Mapped[UUIDStrPK] = mapped_column(primary_key=True)