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

103 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.orderinglist import ordering_list
from sqlalchemy.ext.associationproxy import association_proxy
2020-05-04 02:53:28 +00:00
from szurubooru.model.base import Base
class PoolName(Base):
__tablename__ = 'pool_name'
pool_name_id = sa.Column('pool_name_id', sa.Integer, primary_key=True)
pool_id = sa.Column(
'pool_id',
sa.Integer,
sa.ForeignKey('pool.id'),
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)
def __init__(self, name: str, order: int) -> None:
self.name = name
self.order = order
2020-05-04 07:09:33 +00:00
class PoolPost(Base):
__tablename__ = 'pool_post'
pool_id = sa.Column(
'pool_id',
sa.Integer,
sa.ForeignKey('pool.id'),
nullable=False,
primary_key=True,
index=True)
post_id = sa.Column(
'post_id',
sa.Integer,
sa.ForeignKey('post.id'),
nullable=False,
primary_key=True,
index=True)
order = sa.Column('ord', sa.Integer, nullable=False, index=True)
pool = sa.orm.relationship('Pool', back_populates='_posts')
2020-05-04 09:20:23 +00:00
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
class Pool(Base):
__tablename__ = 'pool'
pool_id = sa.Column('id', sa.Integer, primary_key=True)
category_id = sa.Column(
'category_id',
sa.Integer,
sa.ForeignKey('pool_category.id'),
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')
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',
back_populates='pool',
cascade='all,delete-orphan',
lazy='joined',
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)])
.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)
__mapper_args__ = {
'version_id_col': version,
'version_id_generator': False,
}