2017-02-05 22:19:05 +00:00
|
|
|
import sqlalchemy as sa
|
2017-02-04 00:08:12 +00:00
|
|
|
from szurubooru.model.base import Base
|
|
|
|
from szurubooru.model.comment import Comment
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-22 16:36:12 +00:00
|
|
|
class PostFeature(Base):
|
|
|
|
__tablename__ = 'post_feature'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post_feature_id = sa.Column('id', sa.Integer, primary_key=True)
|
|
|
|
post_id = sa.Column(
|
|
|
|
'post_id',
|
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('post.id'),
|
|
|
|
nullable=False,
|
|
|
|
index=True)
|
|
|
|
user_id = sa.Column(
|
|
|
|
'user_id',
|
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('user.id'),
|
|
|
|
nullable=False,
|
|
|
|
index=True)
|
|
|
|
time = sa.Column('time', sa.DateTime, nullable=False)
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post = sa.orm.relationship('Post') # type: Post
|
|
|
|
user = sa.orm.relationship(
|
|
|
|
'User',
|
|
|
|
backref=sa.orm.backref(
|
|
|
|
'post_features', cascade='all, delete-orphan'))
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-22 16:36:12 +00:00
|
|
|
class PostScore(Base):
|
|
|
|
__tablename__ = 'post_score'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post_id = sa.Column(
|
2016-08-14 12:22:53 +00:00
|
|
|
'post_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('post.id'),
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
index=True)
|
2017-02-05 22:19:05 +00:00
|
|
|
user_id = sa.Column(
|
2016-08-14 12:22:53 +00:00
|
|
|
'user_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('user.id'),
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
index=True)
|
2017-02-05 22:19:05 +00:00
|
|
|
time = sa.Column('time', sa.DateTime, nullable=False)
|
|
|
|
score = sa.Column('score', sa.Integer, nullable=False)
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post = sa.orm.relationship('Post')
|
|
|
|
user = sa.orm.relationship(
|
2016-08-02 09:56:19 +00:00
|
|
|
'User',
|
2017-02-05 22:19:05 +00:00
|
|
|
backref=sa.orm.backref('post_scores', cascade='all, delete-orphan'))
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-22 16:36:12 +00:00
|
|
|
class PostFavorite(Base):
|
|
|
|
__tablename__ = 'post_favorite'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post_id = sa.Column(
|
2016-08-14 12:22:53 +00:00
|
|
|
'post_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('post.id'),
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
index=True)
|
2017-02-05 22:19:05 +00:00
|
|
|
user_id = sa.Column(
|
2016-08-14 12:22:53 +00:00
|
|
|
'user_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('user.id'),
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
index=True)
|
2017-02-05 22:19:05 +00:00
|
|
|
time = sa.Column('time', sa.DateTime, nullable=False)
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post = sa.orm.relationship('Post')
|
|
|
|
user = sa.orm.relationship(
|
2016-08-02 09:56:19 +00:00
|
|
|
'User',
|
2017-02-05 22:19:05 +00:00
|
|
|
backref=sa.orm.backref('post_favorites', cascade='all, delete-orphan'))
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-22 16:36:12 +00:00
|
|
|
class PostNote(Base):
|
|
|
|
__tablename__ = 'post_note'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post_note_id = sa.Column('id', sa.Integer, primary_key=True)
|
|
|
|
post_id = sa.Column(
|
|
|
|
'post_id',
|
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('post.id'),
|
|
|
|
nullable=False,
|
|
|
|
index=True)
|
|
|
|
polygon = sa.Column('polygon', sa.PickleType, nullable=False)
|
|
|
|
text = sa.Column('text', sa.UnicodeText, nullable=False)
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post = sa.orm.relationship('Post')
|
2016-04-22 16:36:12 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-17 10:55:07 +00:00
|
|
|
class PostRelation(Base):
|
|
|
|
__tablename__ = 'post_relation'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
parent_id = sa.Column(
|
2016-08-14 12:22:53 +00:00
|
|
|
'parent_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('post.id'),
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
index=True)
|
2017-02-05 22:19:05 +00:00
|
|
|
child_id = sa.Column(
|
2016-08-14 12:22:53 +00:00
|
|
|
'child_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('post.id'),
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
index=True)
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def __init__(self, parent_id: int, child_id: int) -> None:
|
2016-04-17 10:55:07 +00:00
|
|
|
self.parent_id = parent_id
|
|
|
|
self.child_id = child_id
|
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-17 10:55:07 +00:00
|
|
|
class PostTag(Base):
|
|
|
|
__tablename__ = 'post_tag'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
post_id = sa.Column(
|
2016-08-14 12:22:53 +00:00
|
|
|
'post_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('post.id'),
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
index=True)
|
2017-02-05 22:19:05 +00:00
|
|
|
tag_id = sa.Column(
|
2016-08-15 16:36:31 +00:00
|
|
|
'tag_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('tag.id'),
|
2016-08-15 16:36:31 +00:00
|
|
|
primary_key=True,
|
|
|
|
nullable=False,
|
|
|
|
index=True)
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def __init__(self, post_id: int, tag_id: int) -> None:
|
2016-04-17 10:55:07 +00:00
|
|
|
self.post_id = post_id
|
2016-05-08 20:18:13 +00:00
|
|
|
self.tag_id = tag_id
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-17 10:55:07 +00:00
|
|
|
class Post(Base):
|
|
|
|
__tablename__ = 'post'
|
|
|
|
|
|
|
|
SAFETY_SAFE = 'safe'
|
|
|
|
SAFETY_SKETCHY = 'sketchy'
|
|
|
|
SAFETY_UNSAFE = 'unsafe'
|
2016-05-08 20:18:13 +00:00
|
|
|
|
2016-04-30 21:17:08 +00:00
|
|
|
TYPE_IMAGE = 'image'
|
|
|
|
TYPE_ANIMATION = 'animation'
|
2016-04-17 10:55:07 +00:00
|
|
|
TYPE_VIDEO = 'video'
|
2016-04-30 21:17:08 +00:00
|
|
|
TYPE_FLASH = 'flash'
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-05-08 21:26:46 +00:00
|
|
|
FLAG_LOOP = 'loop'
|
|
|
|
|
2016-04-30 21:17:08 +00:00
|
|
|
# basic meta
|
2017-02-05 22:19:05 +00:00
|
|
|
post_id = sa.Column('id', sa.Integer, primary_key=True)
|
|
|
|
user_id = sa.Column(
|
2016-08-15 16:36:14 +00:00
|
|
|
'user_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('user.id', ondelete='SET NULL'),
|
2016-08-15 16:36:14 +00:00
|
|
|
nullable=True,
|
|
|
|
index=True)
|
2017-02-05 22:19:05 +00:00
|
|
|
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)
|
|
|
|
safety = sa.Column('safety', sa.Unicode(32), nullable=False)
|
|
|
|
source = sa.Column('source', sa.Unicode(200))
|
|
|
|
flags = sa.Column('flags', sa.PickleType, default=None)
|
2016-04-30 21:17:08 +00:00
|
|
|
|
|
|
|
# content description
|
2017-02-05 22:19:05 +00:00
|
|
|
type = sa.Column('type', sa.Unicode(32), nullable=False)
|
|
|
|
checksum = sa.Column('checksum', sa.Unicode(64), nullable=False)
|
|
|
|
file_size = sa.Column('file_size', sa.Integer)
|
|
|
|
canvas_width = sa.Column('image_width', sa.Integer)
|
|
|
|
canvas_height = sa.Column('image_height', sa.Integer)
|
|
|
|
mime_type = sa.Column('mime-type', sa.Unicode(32), nullable=False)
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-04-30 21:17:08 +00:00
|
|
|
# foreign tables
|
2017-02-05 22:19:05 +00:00
|
|
|
user = sa.orm.relationship('User')
|
|
|
|
tags = sa.orm.relationship('Tag', backref='posts', secondary='post_tag')
|
|
|
|
relations = sa.orm.relationship(
|
2016-04-17 10:55:07 +00:00
|
|
|
'Post',
|
|
|
|
secondary='post_relation',
|
|
|
|
primaryjoin=post_id == PostRelation.parent_id,
|
2016-08-31 20:49:45 +00:00
|
|
|
secondaryjoin=post_id == PostRelation.child_id, lazy='joined',
|
|
|
|
backref='related_by')
|
2017-02-05 22:19:05 +00:00
|
|
|
features = sa.orm.relationship(
|
2016-04-22 16:36:12 +00:00
|
|
|
'PostFeature', cascade='all, delete-orphan', lazy='joined')
|
2017-02-05 22:19:05 +00:00
|
|
|
scores = sa.orm.relationship(
|
2016-04-22 16:36:12 +00:00
|
|
|
'PostScore', cascade='all, delete-orphan', lazy='joined')
|
2017-02-05 22:19:05 +00:00
|
|
|
favorited_by = sa.orm.relationship(
|
2016-04-22 16:36:12 +00:00
|
|
|
'PostFavorite', cascade='all, delete-orphan', lazy='joined')
|
2017-02-05 22:19:05 +00:00
|
|
|
notes = sa.orm.relationship(
|
2016-04-22 16:36:12 +00:00
|
|
|
'PostNote', cascade='all, delete-orphan', lazy='joined')
|
2017-02-05 22:19:05 +00:00
|
|
|
comments = sa.orm.relationship('Comment', cascade='all, delete-orphan')
|
2016-04-30 21:17:08 +00:00
|
|
|
|
|
|
|
# dynamic columns
|
2017-02-05 22:19:05 +00:00
|
|
|
tag_count = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.count(PostTag.tag_id)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(PostTag.post_id == post_id)
|
2016-04-20 13:09:36 +00:00
|
|
|
.correlate_except(PostTag))
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
canvas_area = sa.orm.column_property(canvas_width * canvas_height)
|
2017-02-05 22:20:00 +00:00
|
|
|
canvas_aspect_ratio = sa.orm.column_property(
|
|
|
|
sa.sql.expression.func.cast(canvas_width, sa.Float) /
|
|
|
|
sa.sql.expression.func.cast(canvas_height, sa.Float))
|
2016-05-07 19:42:03 +00:00
|
|
|
|
2016-04-22 18:58:04 +00:00
|
|
|
@property
|
2017-02-04 00:08:12 +00:00
|
|
|
def is_featured(self) -> bool:
|
2017-02-05 22:19:05 +00:00
|
|
|
featured_post = sa.orm.object_session(self) \
|
2016-04-22 18:58:04 +00:00
|
|
|
.query(PostFeature) \
|
|
|
|
.order_by(PostFeature.time.desc()) \
|
|
|
|
.first()
|
|
|
|
return featured_post and featured_post.post_id == self.post_id
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
score = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.coalesce(
|
|
|
|
sa.sql.expression.func.sum(PostScore.score), 0)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(PostScore.post_id == post_id)
|
2016-05-07 19:42:03 +00:00
|
|
|
.correlate_except(PostScore))
|
2016-04-24 14:34:06 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
favorite_count = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.count(PostFavorite.post_id)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(PostFavorite.post_id == post_id)
|
2016-04-28 17:04:44 +00:00
|
|
|
.correlate_except(PostFavorite))
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
last_favorite_time = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.max(PostFavorite.time)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(PostFavorite.post_id == post_id)
|
2016-04-28 17:04:44 +00:00
|
|
|
.correlate_except(PostFavorite))
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
feature_count = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.count(PostFeature.post_id)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(PostFeature.post_id == post_id)
|
2016-04-24 14:34:06 +00:00
|
|
|
.correlate_except(PostFeature))
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
last_feature_time = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.max(PostFeature.time)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(PostFeature.post_id == post_id)
|
2016-04-24 14:34:06 +00:00
|
|
|
.correlate_except(PostFeature))
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
comment_count = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.count(Comment.post_id)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(Comment.post_id == post_id)
|
2016-05-07 19:42:03 +00:00
|
|
|
.correlate_except(Comment))
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
last_comment_creation_time = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.max(Comment.creation_time)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(Comment.post_id == post_id)
|
2016-05-07 19:42:03 +00:00
|
|
|
.correlate_except(Comment))
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
last_comment_edit_time = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.max(Comment.last_edit_time)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(Comment.post_id == post_id)
|
2016-05-07 19:42:03 +00:00
|
|
|
.correlate_except(Comment))
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
note_count = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.count(PostNote.post_id)])
|
2016-08-14 12:22:53 +00:00
|
|
|
.where(PostNote.post_id == post_id)
|
2016-05-07 19:42:03 +00:00
|
|
|
.correlate_except(PostNote))
|
2016-06-13 18:08:39 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
relation_count = sa.orm.column_property(
|
|
|
|
sa.sql.expression.select(
|
|
|
|
[sa.sql.expression.func.count(PostRelation.child_id)])
|
2016-07-03 12:22:53 +00:00
|
|
|
.where(
|
2017-02-03 20:42:15 +00:00
|
|
|
(PostRelation.parent_id == post_id) |
|
|
|
|
(PostRelation.child_id == post_id))
|
2016-06-13 18:08:39 +00:00
|
|
|
.correlate_except(PostRelation))
|
2016-08-14 18:30:48 +00:00
|
|
|
|
|
|
|
__mapper_args__ = {
|
|
|
|
'version_id_col': version,
|
|
|
|
'version_id_generator': False,
|
|
|
|
}
|