gallery.accords-library.com/server/szurubooru/model/pool.py

114 lines
3.0 KiB
Python
Raw Normal View History

2020-05-04 02:53:28 +00:00
import sqlalchemy as sa
2020-05-04 07:09:33 +00:00
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.orderinglist import ordering_list
2020-05-04 02:53:28 +00:00
from szurubooru.model.base import Base
class PoolName(Base):
__tablename__ = "pool_name"
2020-05-04 02:53:28 +00:00
pool_name_id = sa.Column("pool_name_id", sa.Integer, primary_key=True)
2020-05-04 02:53:28 +00:00
pool_id = sa.Column(
"pool_id",
2020-05-04 02:53:28 +00:00
sa.Integer,
sa.ForeignKey("pool.id"),
2020-05-04 02:53:28 +00:00
nullable=False,
index=True,
)
name = sa.Column("name", sa.Unicode(128), nullable=False, unique=True)
order = sa.Column("ord", sa.Integer, nullable=False, index=True)
2020-05-04 02:53:28 +00:00
def __init__(self, name: str, order: int) -> None:
self.name = name
self.order = order
2020-06-03 15:55:50 +00:00
2020-05-04 07:09:33 +00:00
class PoolPost(Base):
__tablename__ = "pool_post"
2020-05-04 07:09:33 +00:00
pool_id = sa.Column(
"pool_id",
2020-05-04 07:09:33 +00:00
sa.Integer,
sa.ForeignKey("pool.id"),
2020-05-04 07:09:33 +00:00
nullable=False,
primary_key=True,
index=True,
)
2020-05-04 07:09:33 +00:00
post_id = sa.Column(
"post_id",
2020-05-04 07:09:33 +00:00
sa.Integer,
sa.ForeignKey("post.id"),
2020-05-04 07:09:33 +00:00
nullable=False,
primary_key=True,
index=True,
)
order = sa.Column("ord", sa.Integer, nullable=False, index=True)
2020-05-04 07:09:33 +00:00
pool = sa.orm.relationship("Pool", back_populates="_posts")
post = sa.orm.relationship("Post", back_populates="_pools")
2020-05-04 07:09:33 +00:00
2020-05-04 09:20:23 +00:00
def __init__(self, post) -> None:
2020-05-04 07:09:33 +00:00
self.post_id = post.post_id
2020-05-04 02:53:28 +00:00
2020-06-03 15:55:50 +00:00
2020-05-04 02:53:28 +00:00
class Pool(Base):
__tablename__ = "pool"
2020-05-04 02:53:28 +00:00
pool_id = sa.Column("id", sa.Integer, primary_key=True)
2020-05-04 02:53:28 +00:00
category_id = sa.Column(
"category_id",
2020-05-04 02:53:28 +00:00
sa.Integer,
sa.ForeignKey("pool_category.id"),
2020-05-04 02:53:28 +00:00
nullable=False,
index=True,
)
version = sa.Column("version", sa.Integer, default=1, nullable=False)
creation_time = sa.Column("creation_time", sa.DateTime, nullable=False)
last_edit_time = sa.Column("last_edit_time", sa.DateTime)
description = sa.Column("description", sa.UnicodeText, default=None)
category = sa.orm.relationship("PoolCategory", lazy="joined")
2020-05-04 02:53:28 +00:00
names = sa.orm.relationship(
"PoolName",
cascade="all,delete-orphan",
lazy="joined",
order_by="PoolName.order",
)
2020-05-04 07:09:33 +00:00
_posts = sa.orm.relationship(
"PoolPost",
cascade="all,delete-orphan",
lazy="joined",
back_populates="pool",
order_by="PoolPost.order",
collection_class=ordering_list("order"),
)
posts = association_proxy("_posts", "post")
2020-05-04 02:53:28 +00:00
post_count = sa.orm.column_property(
2020-05-04 07:09:33 +00:00
(
sa.sql.expression.select(
[sa.sql.expression.func.count(PoolPost.post_id)]
)
2020-05-04 07:09:33 +00:00
.where(PoolPost.pool_id == pool_id)
.as_scalar()
),
deferred=True,
)
2020-05-04 02:53:28 +00:00
first_name = sa.orm.column_property(
(
sa.sql.expression.select([PoolName.name])
.where(PoolName.pool_id == pool_id)
.order_by(PoolName.order)
.limit(1)
.as_scalar()
),
deferred=True,
)
2020-05-04 02:53:28 +00:00
__mapper_args__ = {
"version_id_col": version,
"version_id_generator": False,
2020-05-04 02:53:28 +00:00
}