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);
remotes/1693045480750635534/spooky-22
atrc445 2021-08-22 13:29:00 +02:00
parent 659ae686e2
commit f14eda2f7a
2 changed files with 7 additions and 5 deletions

View File

@ -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"<Submission(id={self.id})>"
@property
@lazy
def comment_count(self):
return len(self.comments)
@property
@lazy
def score(self):

View File

@ -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