100 lines
4.7 KiB
Python
100 lines
4.7 KiB
Python
from flask import Flask, render_template, request, redirect, url_for
|
|
import os,shutil,hashlib
|
|
from database_tools_2 import *
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
makedir("static")
|
|
return render_template('remarkindex.html')
|
|
|
|
@app.route('/show_image', methods=['POST'])
|
|
def show_image():
|
|
if request.method == 'POST':
|
|
global id,username
|
|
username = request.form['username'].strip()
|
|
# print(username)
|
|
if len(username) <= 1:
|
|
return "姓名有误."
|
|
id = request.form['question_number'].zfill(6)
|
|
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = "tiku")
|
|
mycursor = mydb.cursor()
|
|
sql = "SELECT content FROM problems WHERE ID = %s AND not obsolete;"
|
|
val = (id,)
|
|
mycursor.execute(sql,val)
|
|
content = mycursor.fetchall()[0][0]
|
|
sql = "SELECT date,remark_content FROM remarks WHERE ID = %s AND not obsolete;"
|
|
mycursor.execute(sql,val)
|
|
remark_list = sorted(mycursor.fetchall(),key=lambda item:item[0],reverse=True)
|
|
# print(remark_list)
|
|
mydb.close()
|
|
content_hash = hashlib.sha256(content.encode("utf8")).hexdigest()
|
|
# print(content_hash,type(content_hash))
|
|
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = "tikupics")
|
|
mycursor = mydb.cursor()
|
|
mycursor.execute("SELECT hash,pic FROM pics WHERE ID = %s;",(id,))
|
|
ret_list = mycursor.fetchall()
|
|
if len(ret_list) > 0 and ret_list[0][0] == content_hash:
|
|
with open(f"static/{id}.png","wb") as f:
|
|
f.write(ret_list[0][1])
|
|
else:
|
|
latex_raw = ReadTextFile("模板文件/独立文件模板.txt")
|
|
latexdata = StringSubstitute(r"<<待替换\d*>>",latex_raw,[content])
|
|
makedir("临时文件/pics")
|
|
currentdate = GetDate()
|
|
currenttime = GetTime()
|
|
SaveTextFile(latexdata,f"临时文件/pics/ID{id}-{currentdate}-{currenttime}.tex")
|
|
os.system(f"xelatex -interaction=batchmode -output-directory=临时文件/pics ID{id}-{currentdate}-{currenttime}.tex")
|
|
os.chdir("临时文件/pics")
|
|
os.system(f"pdftocairo -png -r 600 ID{id}-{currentdate}-{currenttime}.pdf")
|
|
os.chdir("../..")
|
|
shutil.copy(f"临时文件/pics/ID{id}-{currentdate}-{currenttime}-1.png",f"static/{id}.png")
|
|
with open(f"static/{id}.png","rb") as f:
|
|
image_data = f.read()
|
|
mycursor.execute("DELETE FROM pics WHERE ID = %s;",(id,))
|
|
mycursor.execute("INSERT INTO pics (ID,pic,date,hash) VALUES (%s,%s,%s,%s)",(id,image_data,currentdate,content_hash))
|
|
mydb.commit()
|
|
mydb.close()
|
|
output_string = ""
|
|
for rem in remark_list:
|
|
output_string += f"{rem[0]} {rem[1]}\n"
|
|
output_string = re.sub(r"\n","<br><br>\n",output_string) + "<br>\n"
|
|
return render_template('show_image.html', image_path=f'/static/{id}.png', old_remarks = output_string)
|
|
|
|
|
|
@app.route('/submit_remarks', methods=['POST'])
|
|
def submit_remarks():
|
|
category = request.form['category']
|
|
remarks = request.form['remarks'].replace("\n"," ").strip()
|
|
remarks = RefinePunctuations(remarks)
|
|
date = GetDate()
|
|
# mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = "tikutest")
|
|
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = "tiku")
|
|
mycursor = mydb.cursor()
|
|
sql = f"SELECT * FROM remarks WHERE ID = %s and remark_content = %s AND not obsolete;"
|
|
val = (id,f"({category}){remarks}")
|
|
print(f"({category}){remarks}")
|
|
mycursor.execute(sql,val)
|
|
ret = mycursor.fetchall()
|
|
if len(ret) == 0:
|
|
sql = f"INSERT INTO remarks SET ID = %s, date = %s, remark_content = %s;"
|
|
val = (id,date,f"({category}){remarks}")
|
|
mycursor.execute(sql,val)
|
|
ip_address = request.headers.get('X-Forwarded-For', request.remote_addr)
|
|
# 如果 X-Forwarded-For 头存在多个 IP 地址,则取第一个
|
|
if ',' in ip_address:
|
|
ip_address = ip_address.split(',')[0]
|
|
sql = "INSERT INTO logs (DATE,TIME,username,ID,action,db_content) VALUE (%s,%s,%s,%s,%s,%s);"
|
|
val = (GetDate(),GetTime(),f"{username} at {ip_address}",id,f"网页新增备注",f"({category}){remarks}")
|
|
mycursor.execute(sql,val)
|
|
mydb.commit()
|
|
mydb.close()
|
|
return render_template('show_remarks.html', category=category, remarks=remarks)
|
|
else:
|
|
mydb.close()
|
|
return f"已有备注 ({category}){remarks}"
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host = "0.0.0.0", port = 25433) |