2017-02-05 22:19:05 +00:00
|
|
|
import sqlalchemy as sa
|
2017-02-04 00:08:12 +00:00
|
|
|
from szurubooru.db import get_session
|
|
|
|
from szurubooru.model.base import Base
|
2016-04-24 07:04:53 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-24 07:04:53 +00:00
|
|
|
class CommentScore(Base):
|
|
|
|
__tablename__ = 'comment_score'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
comment_id = sa.Column(
|
2016-08-15 16:36:31 +00:00
|
|
|
'comment_id',
|
2017-02-05 22:19:05 +00:00
|
|
|
sa.Integer,
|
|
|
|
sa.ForeignKey('comment.id'),
|
2016-08-15 16:36:31 +00:00
|
|
|
nullable=False,
|
|
|
|
primary_key=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-15 16:36:31 +00:00
|
|
|
nullable=False,
|
2016-08-14 12:22:53 +00:00
|
|
|
primary_key=True,
|
|
|
|
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-24 07:04:53 +00:00
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
comment = sa.orm.relationship('Comment')
|
|
|
|
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('comment_scores', cascade='all, delete-orphan'))
|
2016-04-24 07:04:53 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-24 07:04:53 +00:00
|
|
|
class Comment(Base):
|
|
|
|
__tablename__ = 'comment'
|
|
|
|
|
2017-02-05 22:19:05 +00:00
|
|
|
comment_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=True,
|
|
|
|
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)
|
|
|
|
text = sa.Column('text', sa.UnicodeText, default=None)
|
|
|
|
|
|
|
|
user = sa.orm.relationship('User')
|
|
|
|
post = sa.orm.relationship('Post')
|
|
|
|
scores = sa.orm.relationship(
|
2016-04-24 07:04:53 +00:00
|
|
|
'CommentScore', cascade='all, delete-orphan', lazy='joined')
|
2016-04-24 14:34:06 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-04 00:08:12 +00:00
|
|
|
def score(self) -> int:
|
|
|
|
return (
|
|
|
|
get_session()
|
2017-02-05 22:19:05 +00:00
|
|
|
.query(sa.sql.expression.func.sum(CommentScore.score))
|
2017-02-04 00:08:12 +00:00
|
|
|
.filter(CommentScore.comment_id == self.comment_id)
|
|
|
|
.one()[0] or 0)
|
2016-08-14 18:30:48 +00:00
|
|
|
|
|
|
|
__mapper_args__ = {
|
|
|
|
'version_id_col': version,
|
|
|
|
'version_id_generator': False,
|
|
|
|
}
|