mirror of
https://codeberg.org/catask-org/catask.git
synced 2025-04-20 13:53:42 -05:00
more postgres stuff
This commit is contained in:
parent
6e2056a685
commit
38b012c276
2 changed files with 44 additions and 11 deletions
49
app.py
49
app.py
|
@ -166,26 +166,46 @@ def badGateway(e):
|
|||
|
||||
@app.route('/', methods=['GET'])
|
||||
def index():
|
||||
# func.getAllQuestions() returns combined and metadata
|
||||
func_val = func.getAllQuestions()
|
||||
per_page = 25
|
||||
offset = 0
|
||||
page = 1
|
||||
|
||||
func_val = func.getAllQuestions(limit=per_page, offset=offset)
|
||||
combined = func_val[0]
|
||||
metadata = func_val[1]
|
||||
emojis = func.listEmojis()
|
||||
packs = func.listEmojiPacks()
|
||||
|
||||
return render_template(
|
||||
'index.html',
|
||||
combined=combined,
|
||||
urllib=urllib,
|
||||
trimContent=func.trimContent,
|
||||
metadata=metadata,
|
||||
getRandomWord=func.getRandomWord,
|
||||
formatRelativeTime=func.formatRelativeTime,
|
||||
emojis=emojis,
|
||||
packs=packs,
|
||||
page=page,
|
||||
per_page=per_page
|
||||
)
|
||||
|
||||
return render_template('index.html', combined=combined, urllib=urllib, trimContent=func.trimContent, metadata=metadata, getRandomWord=func.getRandomWord, formatRelativeTime=func.formatRelativeTime)
|
||||
|
||||
@app.route('/inbox/', methods=['GET'])
|
||||
@loginRequired
|
||||
def inbox():
|
||||
conn = func.connectToDb()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor = conn.cursor()
|
||||
|
||||
app.logger.debug("[CatAsk/Inbox] SELECT'ing unanswered questions")
|
||||
|
||||
cursor.execute("SELECT * FROM questions WHERE answered=%s ORDER BY creation_date DESC", (False,))
|
||||
questions = cursor.fetchall()
|
||||
# postgres shenanigans
|
||||
for question in questions:
|
||||
question['creation_date'] = question['creation_date'].replace(microsecond=0).replace(tzinfo=None)
|
||||
cursor.execute("UPDATE questions SET unread=%s WHERE id=%s", (False, question['id']))
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return render_template('inbox.html', questions=questions, formatRelativeTime=func.formatRelativeTime)
|
||||
|
@ -670,21 +690,32 @@ def updateBlacklist():
|
|||
blacklist = request.form.get('blacklist')
|
||||
with open(const.blacklistFile, 'w') as file:
|
||||
file.write(blacklist)
|
||||
return {'message': 'Blacklist updated!'}, 200
|
||||
return {'message': _("Blacklist updated!")}, 200
|
||||
|
||||
@api_bp.route('/get_question_count/', methods=['GET'])
|
||||
def getQuestionCount():
|
||||
def getQuestionCount(answered: bool = None, unread: bool = None):
|
||||
conn = func.connectToDb()
|
||||
cursor = conn.cursor()
|
||||
|
||||
app.logger.debug("[CatAsk/API/get_question_count] SELECT'ing question count from database")
|
||||
query = "SELECT COUNT(id) FROM questions"
|
||||
if (answered != None) and not unread:
|
||||
query += " WHERE answered=%s"
|
||||
cursor.execute(query, (answered,))
|
||||
elif (answered != None) and (unread != None):
|
||||
query += " WHERE answered=%s AND unread=%s"
|
||||
cursor.execute(query, (answered, unread))
|
||||
elif (unread != None) and not answered:
|
||||
query += " WHERE unread=%s"
|
||||
cursor.execute(query, (unread,))
|
||||
else:
|
||||
cursor.execute(query)
|
||||
|
||||
cursor.execute("SELECT COUNT(id) FROM questions WHERE answered=%s AND unread=%s", (False, True))
|
||||
question_count = cursor.fetchone()
|
||||
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return str(question_count[0])
|
||||
return str(question_count['count'])
|
||||
|
||||
# -- import/export --
|
||||
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
from flask import url_for, request, jsonify, Flask, abort
|
||||
from flask import url_for, request, jsonify, Flask, abort, session
|
||||
from flask_babel import Babel, _, refresh
|
||||
from markupsafe import Markup
|
||||
from bleach.sanitizer import Cleaner
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from mistune import HTMLRenderer, escape
|
||||
from PIL import Image
|
||||
from psycopg.rows import dict_row
|
||||
import base64
|
||||
import time
|
||||
import zipfile
|
||||
|
@ -12,7 +14,7 @@ import shutil
|
|||
import subprocess
|
||||
import mistune
|
||||
import humanize
|
||||
import mysql.connector
|
||||
import psycopg
|
||||
import re
|
||||
import os
|
||||
import random
|
||||
|
|
Loading…
Add table
Reference in a new issue