2016-05-08 20:18:13 +00:00
|
|
|
from sqlalchemy import (
|
|
|
|
Column, Integer, DateTime, Unicode, UnicodeText, PickleType, ForeignKey)
|
2016-04-22 18:58:04 +00:00
|
|
|
from sqlalchemy.orm import relationship, column_property, object_session
|
2016-04-17 10:55:07 +00:00
|
|
|
from sqlalchemy.sql.expression import func, select
|
|
|
|
from szurubooru.db.base import Base
|
2016-05-07 19:42:03 +00:00
|
|
|
from szurubooru.db.comment import Comment
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-04-22 16:36:12 +00:00
|
|
|
class PostFeature(Base):
|
|
|
|
__tablename__ = 'post_feature'
|
|
|
|
|
|
|
|
post_feature_id = Column('id', Integer, primary_key=True)
|
2016-05-09 07:43:00 +00:00
|
|
|
post_id = Column(
|
|
|
|
'post_id', Integer, ForeignKey('post.id'), nullable=False, index=True)
|
|
|
|
user_id = Column(
|
|
|
|
'user_id', Integer, ForeignKey('user.id'), nullable=False, index=True)
|
2016-04-22 16:36:12 +00:00
|
|
|
time = Column('time', DateTime, nullable=False)
|
|
|
|
|
|
|
|
post = relationship('Post')
|
|
|
|
user = relationship('User')
|
|
|
|
|
|
|
|
class PostScore(Base):
|
|
|
|
__tablename__ = 'post_score'
|
|
|
|
|
2016-05-09 07:43:00 +00:00
|
|
|
post_id = Column(
|
|
|
|
'post_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
|
|
|
user_id = Column(
|
|
|
|
'user_id', Integer, ForeignKey('user.id'), primary_key=True, index=True)
|
2016-04-22 16:36:12 +00:00
|
|
|
time = Column('time', DateTime, nullable=False)
|
|
|
|
score = Column('score', Integer, nullable=False)
|
|
|
|
|
|
|
|
post = relationship('Post')
|
|
|
|
user = relationship('User')
|
|
|
|
|
|
|
|
class PostFavorite(Base):
|
|
|
|
__tablename__ = 'post_favorite'
|
|
|
|
|
2016-05-09 07:43:00 +00:00
|
|
|
post_id = Column(
|
|
|
|
'post_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
|
|
|
user_id = Column(
|
2016-05-10 09:55:36 +00:00
|
|
|
'user_id', Integer, ForeignKey('user.id'), primary_key=True, index=True)
|
2016-04-22 16:36:12 +00:00
|
|
|
time = Column('time', DateTime, nullable=False)
|
|
|
|
|
|
|
|
post = relationship('Post')
|
|
|
|
user = relationship('User')
|
|
|
|
|
|
|
|
class PostNote(Base):
|
|
|
|
__tablename__ = 'post_note'
|
|
|
|
|
|
|
|
post_note_id = Column('id', Integer, primary_key=True)
|
2016-05-09 07:43:00 +00:00
|
|
|
post_id = Column(
|
|
|
|
'post_id', Integer, ForeignKey('post.id'), nullable=False, index=True)
|
2016-04-22 16:36:12 +00:00
|
|
|
polygon = Column('polygon', PickleType, nullable=False)
|
2016-05-08 20:18:13 +00:00
|
|
|
text = Column('text', UnicodeText, nullable=False)
|
2016-04-22 16:36:12 +00:00
|
|
|
|
|
|
|
post = relationship('Post')
|
|
|
|
|
2016-04-17 10:55:07 +00:00
|
|
|
class PostRelation(Base):
|
|
|
|
__tablename__ = 'post_relation'
|
|
|
|
|
2016-05-09 07:43:00 +00:00
|
|
|
parent_id = Column(
|
|
|
|
'parent_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
|
|
|
child_id = Column(
|
|
|
|
'child_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
2016-04-17 10:55:07 +00:00
|
|
|
|
|
|
|
def __init__(self, parent_id, child_id):
|
|
|
|
self.parent_id = parent_id
|
|
|
|
self.child_id = child_id
|
|
|
|
|
|
|
|
class PostTag(Base):
|
|
|
|
__tablename__ = 'post_tag'
|
|
|
|
|
2016-05-09 07:43:00 +00:00
|
|
|
post_id = Column(
|
|
|
|
'post_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
|
|
|
tag_id = Column(
|
|
|
|
'tag_id', Integer, ForeignKey('tag.id'), primary_key=True, index=True)
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-05-08 20:18:13 +00:00
|
|
|
def __init__(self, post_id, tag_id):
|
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
|
|
|
|
|
|
|
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
|
2016-04-17 10:55:07 +00:00
|
|
|
post_id = Column('id', Integer, primary_key=True)
|
2016-05-09 07:43:00 +00:00
|
|
|
user_id = Column('user_id', Integer, ForeignKey('user.id'), index=True)
|
2016-04-17 10:55:07 +00:00
|
|
|
creation_time = Column('creation_time', DateTime, nullable=False)
|
|
|
|
last_edit_time = Column('last_edit_time', DateTime)
|
2016-05-08 20:18:13 +00:00
|
|
|
safety = Column('safety', Unicode(32), nullable=False)
|
|
|
|
source = Column('source', Unicode(200))
|
2016-04-30 21:17:08 +00:00
|
|
|
flags = Column('flags', PickleType, default=None)
|
|
|
|
|
|
|
|
# content description
|
2016-05-08 20:18:13 +00:00
|
|
|
type = Column('type', Unicode(32), nullable=False)
|
|
|
|
checksum = Column('checksum', Unicode(64), nullable=False)
|
2016-04-17 10:55:07 +00:00
|
|
|
file_size = Column('file_size', Integer)
|
2016-04-22 18:58:04 +00:00
|
|
|
canvas_width = Column('image_width', Integer)
|
|
|
|
canvas_height = Column('image_height', Integer)
|
2016-05-08 20:18:13 +00:00
|
|
|
mime_type = Column('mime-type', Unicode(32), nullable=False)
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-04-30 21:17:08 +00:00
|
|
|
# foreign tables
|
2016-04-17 10:55:07 +00:00
|
|
|
user = relationship('User')
|
|
|
|
tags = relationship('Tag', backref='posts', secondary='post_tag')
|
|
|
|
relations = relationship(
|
|
|
|
'Post',
|
|
|
|
secondary='post_relation',
|
|
|
|
primaryjoin=post_id == PostRelation.parent_id,
|
2016-05-30 18:01:14 +00:00
|
|
|
secondaryjoin=post_id == PostRelation.child_id, lazy='joined')
|
2016-04-22 16:36:12 +00:00
|
|
|
features = relationship(
|
|
|
|
'PostFeature', cascade='all, delete-orphan', lazy='joined')
|
|
|
|
scores = relationship(
|
|
|
|
'PostScore', cascade='all, delete-orphan', lazy='joined')
|
2016-04-22 18:58:04 +00:00
|
|
|
favorited_by = relationship(
|
2016-04-22 16:36:12 +00:00
|
|
|
'PostFavorite', cascade='all, delete-orphan', lazy='joined')
|
|
|
|
notes = relationship(
|
|
|
|
'PostNote', cascade='all, delete-orphan', lazy='joined')
|
2016-04-25 08:48:15 +00:00
|
|
|
comments = relationship('Comment')
|
2016-04-30 21:17:08 +00:00
|
|
|
|
|
|
|
# dynamic columns
|
2016-04-17 10:55:07 +00:00
|
|
|
tag_count = column_property(
|
2016-04-20 13:09:36 +00:00
|
|
|
select([func.count(PostTag.tag_id)]) \
|
2016-04-17 16:05:45 +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
|
|
|
|
2016-05-07 19:42:03 +00:00
|
|
|
canvas_area = column_property(canvas_width * canvas_height)
|
|
|
|
|
2016-04-22 18:58:04 +00:00
|
|
|
@property
|
|
|
|
def is_featured(self):
|
|
|
|
featured_post = object_session(self) \
|
|
|
|
.query(PostFeature) \
|
|
|
|
.order_by(PostFeature.time.desc()) \
|
|
|
|
.first()
|
|
|
|
return featured_post and featured_post.post_id == self.post_id
|
|
|
|
|
2016-05-07 19:42:03 +00:00
|
|
|
score = column_property(
|
|
|
|
select([func.coalesce(func.sum(PostScore.score), 0)]) \
|
|
|
|
.where(PostScore.post_id == post_id) \
|
|
|
|
.correlate_except(PostScore))
|
2016-04-24 14:34:06 +00:00
|
|
|
|
2016-04-28 17:04:44 +00:00
|
|
|
favorite_count = column_property(
|
|
|
|
select([func.count(PostFavorite.post_id)]) \
|
|
|
|
.where(PostFavorite.post_id == post_id) \
|
|
|
|
.correlate_except(PostFavorite))
|
|
|
|
|
|
|
|
last_favorite_time = column_property(
|
|
|
|
select([func.max(PostFavorite.time)]) \
|
|
|
|
.where(PostFavorite.post_id == post_id) \
|
|
|
|
.correlate_except(PostFavorite))
|
|
|
|
|
2016-04-24 14:34:06 +00:00
|
|
|
feature_count = column_property(
|
|
|
|
select([func.count(PostFeature.post_id)]) \
|
|
|
|
.where(PostFeature.post_id == post_id) \
|
|
|
|
.correlate_except(PostFeature))
|
|
|
|
|
|
|
|
last_feature_time = column_property(
|
|
|
|
select([func.max(PostFeature.time)]) \
|
|
|
|
.where(PostFeature.post_id == post_id) \
|
|
|
|
.correlate_except(PostFeature))
|
|
|
|
|
2016-05-07 19:42:03 +00:00
|
|
|
comment_count = column_property(
|
|
|
|
select([func.count(Comment.post_id)]) \
|
|
|
|
.where(Comment.post_id == post_id) \
|
|
|
|
.correlate_except(Comment))
|
|
|
|
|
|
|
|
last_comment_creation_time = column_property(
|
|
|
|
select([func.max(Comment.creation_time)]) \
|
|
|
|
.where(Comment.post_id == post_id) \
|
|
|
|
.correlate_except(Comment))
|
|
|
|
|
|
|
|
last_comment_edit_time = column_property(
|
|
|
|
select([func.max(Comment.last_edit_time)]) \
|
|
|
|
.where(Comment.post_id == post_id) \
|
|
|
|
.correlate_except(Comment))
|
|
|
|
|
|
|
|
note_count = column_property(
|
|
|
|
select([func.count(PostNote.post_id)]) \
|
|
|
|
.where(PostNote.post_id == post_id) \
|
|
|
|
.correlate_except(PostNote))
|
2016-06-13 18:08:39 +00:00
|
|
|
|
|
|
|
relation_count = column_property(
|
|
|
|
select([func.count(PostRelation.child_id)]) \
|
|
|
|
.where(PostRelation.parent_id == post_id) \
|
|
|
|
.correlate_except(PostRelation))
|