mirror of
https://codeberg.org/catask-org/catask.git
synced 2025-04-19 21:33:41 -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'])
|
@app.route('/', methods=['GET'])
|
||||||
def index():
|
def index():
|
||||||
# func.getAllQuestions() returns combined and metadata
|
per_page = 25
|
||||||
func_val = func.getAllQuestions()
|
offset = 0
|
||||||
|
page = 1
|
||||||
|
|
||||||
|
func_val = func.getAllQuestions(limit=per_page, offset=offset)
|
||||||
combined = func_val[0]
|
combined = func_val[0]
|
||||||
metadata = func_val[1]
|
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'])
|
@app.route('/inbox/', methods=['GET'])
|
||||||
@loginRequired
|
@loginRequired
|
||||||
def inbox():
|
def inbox():
|
||||||
conn = func.connectToDb()
|
conn = func.connectToDb()
|
||||||
cursor = conn.cursor(dictionary=True)
|
cursor = conn.cursor()
|
||||||
|
|
||||||
app.logger.debug("[CatAsk/Inbox] SELECT'ing unanswered questions")
|
app.logger.debug("[CatAsk/Inbox] SELECT'ing unanswered questions")
|
||||||
|
|
||||||
cursor.execute("SELECT * FROM questions WHERE answered=%s ORDER BY creation_date DESC", (False,))
|
cursor.execute("SELECT * FROM questions WHERE answered=%s ORDER BY creation_date DESC", (False,))
|
||||||
questions = cursor.fetchall()
|
questions = cursor.fetchall()
|
||||||
|
# postgres shenanigans
|
||||||
for question in questions:
|
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']))
|
cursor.execute("UPDATE questions SET unread=%s WHERE id=%s", (False, question['id']))
|
||||||
|
conn.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
return render_template('inbox.html', questions=questions, formatRelativeTime=func.formatRelativeTime)
|
return render_template('inbox.html', questions=questions, formatRelativeTime=func.formatRelativeTime)
|
||||||
|
@ -670,21 +690,32 @@ def updateBlacklist():
|
||||||
blacklist = request.form.get('blacklist')
|
blacklist = request.form.get('blacklist')
|
||||||
with open(const.blacklistFile, 'w') as file:
|
with open(const.blacklistFile, 'w') as file:
|
||||||
file.write(blacklist)
|
file.write(blacklist)
|
||||||
return {'message': 'Blacklist updated!'}, 200
|
return {'message': _("Blacklist updated!")}, 200
|
||||||
|
|
||||||
@api_bp.route('/get_question_count/', methods=['GET'])
|
@api_bp.route('/get_question_count/', methods=['GET'])
|
||||||
def getQuestionCount():
|
def getQuestionCount(answered: bool = None, unread: bool = None):
|
||||||
conn = func.connectToDb()
|
conn = func.connectToDb()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
app.logger.debug("[CatAsk/API/get_question_count] SELECT'ing question count from database")
|
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()
|
question_count = cursor.fetchone()
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
return str(question_count[0])
|
return str(question_count['count'])
|
||||||
|
|
||||||
# -- import/export --
|
# -- 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 markupsafe import Markup
|
||||||
from bleach.sanitizer import Cleaner
|
from bleach.sanitizer import Cleaner
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from mistune import HTMLRenderer, escape
|
from mistune import HTMLRenderer, escape
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from psycopg.rows import dict_row
|
||||||
import base64
|
import base64
|
||||||
import time
|
import time
|
||||||
import zipfile
|
import zipfile
|
||||||
|
@ -12,7 +14,7 @@ import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import mistune
|
import mistune
|
||||||
import humanize
|
import humanize
|
||||||
import mysql.connector
|
import psycopg
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
|
Loading…
Add table
Reference in a new issue