2016-05-08 20:18:13 +00:00
|
|
|
from sqlalchemy import Column, Integer, DateTime, UnicodeText, ForeignKey
|
2016-06-16 18:03:40 +00:00
|
|
|
from sqlalchemy.orm import relationship
|
2016-04-24 14:34:06 +00:00
|
|
|
from sqlalchemy.sql.expression import func
|
2016-04-24 07:04:53 +00:00
|
|
|
from szurubooru.db.base import Base
|
|
|
|
|
|
|
|
class CommentScore(Base):
|
|
|
|
__tablename__ = 'comment_score'
|
|
|
|
|
2016-05-09 07:43:00 +00:00
|
|
|
comment_id = Column(
|
|
|
|
'comment_id', Integer, ForeignKey('comment.id'), primary_key=True)
|
|
|
|
user_id = Column(
|
|
|
|
'user_id', Integer, ForeignKey('user.id'), primary_key=True, index=True)
|
2016-04-24 07:04:53 +00:00
|
|
|
time = Column('time', DateTime, nullable=False)
|
|
|
|
score = Column('score', Integer, nullable=False)
|
|
|
|
|
|
|
|
comment = relationship('Comment')
|
|
|
|
user = relationship('User')
|
|
|
|
|
|
|
|
class Comment(Base):
|
|
|
|
__tablename__ = 'comment'
|
|
|
|
|
|
|
|
comment_id = Column('id', Integer, primary_key=True)
|
2016-05-09 07:43:00 +00:00
|
|
|
post_id = Column(
|
|
|
|
'post_id', Integer, ForeignKey('post.id'), index=True, nullable=False)
|
|
|
|
user_id = Column(
|
|
|
|
'user_id', Integer, ForeignKey('user.id'), index=True)
|
2016-04-24 07:04:53 +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
|
|
|
text = Column('text', UnicodeText, default=None)
|
2016-04-24 07:04:53 +00:00
|
|
|
|
|
|
|
user = relationship('User')
|
|
|
|
post = relationship('Post')
|
|
|
|
scores = relationship(
|
|
|
|
'CommentScore', cascade='all, delete-orphan', lazy='joined')
|
2016-04-24 14:34:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def score(self):
|
2016-06-12 12:01:38 +00:00
|
|
|
from szurubooru.db import session
|
|
|
|
return session \
|
2016-04-24 14:34:06 +00:00
|
|
|
.query(func.sum(CommentScore.score)) \
|
|
|
|
.filter(CommentScore.comment_id == self.comment_id) \
|
|
|
|
.one()[0] or 0
|