mirror of
https://codeberg.org/catask-org/catask.git
synced 2025-04-19 21:33:41 -05:00
getAllQuestions function
This commit is contained in:
parent
cab955dbea
commit
4557b166bb
1 changed files with 41 additions and 0 deletions
41
functions.py
41
functions.py
|
@ -129,6 +129,47 @@ def getQuestion(question_id: int):
|
||||||
conn.close()
|
conn.close()
|
||||||
return question
|
return question
|
||||||
|
|
||||||
|
def getAllQuestions():
|
||||||
|
conn = connectToDb()
|
||||||
|
cursor = conn.cursor(dictionary=True)
|
||||||
|
|
||||||
|
app.logger.debug("[CatAsk/functions/getAllQuestions] SELECT'ing all questions with latest answers")
|
||||||
|
|
||||||
|
query = """
|
||||||
|
SELECT q.*, a.creation_date AS latest_answer_date
|
||||||
|
FROM questions q
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT question_id, MAX(creation_date) AS creation_date
|
||||||
|
FROM answers
|
||||||
|
GROUP BY question_id
|
||||||
|
) a ON q.id = a.question_id
|
||||||
|
WHERE q.answered = %s
|
||||||
|
ORDER BY (a.creation_date IS NULL), a.creation_date DESC, q.pinned DESC, q.creation_date DESC
|
||||||
|
"""
|
||||||
|
|
||||||
|
cursor.execute(query, (True,))
|
||||||
|
questions = cursor.fetchall()
|
||||||
|
|
||||||
|
app.logger.debug("[CatAsk/functions/getAllQuestions] SELECT'ing answers")
|
||||||
|
|
||||||
|
cursor.execute("SELECT * FROM answers ORDER BY creation_date DESC")
|
||||||
|
answers = cursor.fetchall()
|
||||||
|
|
||||||
|
metadata = generateMetadata()
|
||||||
|
|
||||||
|
combined = []
|
||||||
|
for question in questions:
|
||||||
|
question_answers = [answer for answer in answers if answer['question_id'] == question['id']]
|
||||||
|
combined.append({
|
||||||
|
'question': question,
|
||||||
|
'answers': question_answers
|
||||||
|
})
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return combined, metadata
|
||||||
|
|
||||||
def addQuestion(from_who, question, cw, noAntispam=False):
|
def addQuestion(from_who, question, cw, noAntispam=False):
|
||||||
|
|
||||||
if cfg['antispam']['type'] == 'basic':
|
if cfg['antispam']['type'] == 'basic':
|
||||||
|
|
Loading…
Add table
Reference in a new issue