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

78 lines
2.0 KiB
Python
Raw Permalink Normal View History

import sqlalchemy as sa
from szurubooru.db import get_session
from szurubooru.model.base import Base
2016-04-24 07:04:53 +00:00
2016-04-24 07:04:53 +00:00
class CommentScore(Base):
__tablename__ = "comment_score"
2016-04-24 07:04:53 +00:00
comment_id = sa.Column(
"comment_id",
sa.Integer,
sa.ForeignKey("comment.id"),
nullable=False,
primary_key=True,
)
user_id = sa.Column(
"user_id",
sa.Integer,
sa.ForeignKey("user.id"),
nullable=False,
primary_key=True,
index=True,
)
time = sa.Column("time", sa.DateTime, nullable=False)
score = sa.Column("score", sa.Integer, nullable=False)
2016-04-24 07:04:53 +00:00
comment = sa.orm.relationship("Comment")
user = sa.orm.relationship(
"User",
backref=sa.orm.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"
2016-04-24 07:04:53 +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(
"CommentScore", cascade="all, delete-orphan", lazy="joined"
)
2016-04-24 14:34:06 +00:00
@property
def score(self) -> int:
return (
get_session()
.query(sa.sql.expression.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,
}