gallery.accords-library.com/server/szurubooru/db/comment.py

62 lines
1.9 KiB
Python
Raw Normal View History

from sqlalchemy import Column, Integer, DateTime, UnicodeText, ForeignKey
2016-08-02 09:56:19 +00:00
from sqlalchemy.orm import relationship, backref
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
2016-04-24 07:04:53 +00:00
class CommentScore(Base):
__tablename__ = 'comment_score'
2016-05-09 07:43:00 +00:00
comment_id = Column(
'comment_id',
Integer,
ForeignKey('comment.id'),
nullable=False,
primary_key=True)
2016-05-09 07:43:00 +00:00
user_id = Column(
'user_id',
Integer,
ForeignKey('user.id'),
nullable=False,
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')
2016-08-02 09:56:19 +00:00
user = relationship(
'User',
backref=backref('comment_scores', cascade='all, delete-orphan'))
2016-04-24 07:04:53 +00:00
2016-04-24 07:04:53 +00:00
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'), nullable=False, index=True)
user_id = Column(
'user_id', Integer, ForeignKey('user.id'), nullable=True, index=True)
2016-08-06 19:16:39 +00:00
version = Column('version', Integer, default=1, nullable=False)
2016-04-24 07:04:53 +00:00
creation_time = Column('creation_time', DateTime, nullable=False)
last_edit_time = Column('last_edit_time', DateTime)
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):
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
__mapper_args__ = {
'version_id_col': version,
'version_id_generator': False,
}