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

45 lines
1.6 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
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')
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
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)
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