From f14eda2f7aa68556ebac77b2b2869eeb26d8e456 Mon Sep 17 00:00:00 2001 From: atrc445 Date: Sun, 22 Aug 2021 13:29:00 +0200 Subject: [PATCH] add comment count column to submissions ALTER TABLE submissions add column comment_count integer default 0; update submissions set comment_count=(select count(id) from comments where parent_submission=submissions.id); --- files/classes/submission.py | 6 +----- files/routes/comments.py | 6 ++++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/files/classes/submission.py b/files/classes/submission.py index 1eda5a81a..0e31ac095 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -52,6 +52,7 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing): stickied = Column(Boolean, default=False) is_pinned = Column(Boolean, default=False) private = Column(Boolean, default=False) + comment_count = Column(Integer, default=0) comments = relationship( "Comment", lazy="joined", @@ -95,11 +96,6 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing): def __repr__(self): return f"" - @property - @lazy - def comment_count(self): - return len(self.comments) - @property @lazy def score(self): diff --git a/files/routes/comments.py b/files/routes/comments.py index 14428fca8..adeae9fce 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -269,8 +269,14 @@ def api_comment(v): app_id=v.client.application.id if v.client else None, shadowbanned=v.shadowbanned ) + g.db.add(c) g.db.flush() + + parent_post.comment_count = g.db.query(Comment).filter_by(parent_submission=parent_post.id).count() + g.db.add(parent_post) + g.db.flush() + if request.files.get("file") and request.headers.get("cf-ipcountry") != "T1": file=request.files["file"] if not file.content_type.startswith('image/'): return {"error": "That wasn't an image!"}, 400