forum: fix a bug in c59e84485 for forums with no topics

WHERE would be on its own without argument.
This commit is contained in:
Lephe 2021-07-07 19:22:36 +02:00
parent e29c73d09e
commit fe039e4092
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 6 additions and 5 deletions

View File

@ -83,12 +83,13 @@ def forum_page(f, page=1):
# Count comments; this direct request avoids performing one request for
# each topic.thread.comments.count() in the view, which the database
# doesn't really appreciate performance-wise.
req_comment_counts = \
"SELECT thread_id, COUNT(*) FROM comment WHERE " + \
" OR ".join(f"thread_id={t.thread.id}" for t in topics.items) + \
" GROUP BY thread_id"
selection = " OR ".join(f"thread_id={t.thread.id}" for t in topics.items)
selection = "WHERE " + selection if selection else ""
comment_counts = db.session.execute(req_comment_counts)
comment_counts = db.session.execute(f"""
SELECT thread_id, COUNT(*) FROM comment {selection}
GROUP BY thread_id
""")
comment_counts = dict(list(comment_counts))
return render('/forum/forum.html', f=f, topics=topics, form=form,