迁移数据库为postgresql
This commit is contained in:
@@ -9,6 +9,22 @@ import time
|
||||
import json
|
||||
import shutil
|
||||
|
||||
# postgresql
|
||||
import psycopg2
|
||||
with open('/var/open-ww3-project-ww3-tw/config/blog/db_config.json', 'r') as file:
|
||||
data = json.load(file)
|
||||
# 设定数据库连接配置
|
||||
conn_params = {
|
||||
"dbname" : data['dbname'],
|
||||
"user" : data['user'],
|
||||
"password": data['password'],
|
||||
"host": data['host'],
|
||||
"port": data['port']
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
def get_owp_db_conn():
|
||||
conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/sqlite/owp.db')
|
||||
conn.row_factory = sqlite3.Row
|
||||
@@ -42,13 +58,37 @@ def no_game(subpath=None):
|
||||
|
||||
@blog_bp.route('/')
|
||||
def home():
|
||||
db = get_owp_db()
|
||||
conn = get_owp_db_conn()
|
||||
sql_logs = "SELECT * from logs;"
|
||||
sql_posts = "SELECT * from posts where status= 1;"
|
||||
logs = conn.execute(sql_logs).fetchall()
|
||||
posts = conn.execute(sql_posts).fetchall()
|
||||
conn = psycopg2.connect(**conn_params)
|
||||
cur = conn.cursor()
|
||||
sql_logs = "SELECT * FROM logs;"
|
||||
sql_posts = "SELECT* FROM posts WHERE status = 1;"
|
||||
|
||||
cur.execute(sql_logs)
|
||||
cur_logs = cur.fetchall()
|
||||
cur.execute(sql_posts)
|
||||
cur_posts = cur.fetchall()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
# 格式转换
|
||||
dict_logs = [
|
||||
{
|
||||
"id": row[0],
|
||||
"date": row[1],
|
||||
"content": row[2]
|
||||
} for row in cur_logs
|
||||
]
|
||||
dict_posts = [
|
||||
{
|
||||
"id": row[0],
|
||||
"date": row[1],
|
||||
"title": row[2],
|
||||
"content": row[3]
|
||||
} for row in cur_posts
|
||||
]
|
||||
|
||||
|
||||
# 计数器
|
||||
count_file = '/var/open-ww3-project-ww3-tw/static/counter.txt'
|
||||
def load_couter():
|
||||
if os.path.exists(count_file):
|
||||
@@ -61,7 +101,8 @@ def home():
|
||||
counter = load_couter()
|
||||
counter += 1
|
||||
save_couter(counter)
|
||||
return render_template('blog/home.html', logs=logs[::-1], posts=posts[::-1], counter=counter)
|
||||
|
||||
return render_template('blog/home.html', counter = counter, logs = dict_logs[::-1], posts = dict_posts[::-1])
|
||||
|
||||
@blog_bp.route('/about/')
|
||||
def about():
|
||||
@@ -70,14 +111,27 @@ def about():
|
||||
|
||||
@blog_bp.route('/posts/')
|
||||
def posts_list():
|
||||
conn = get_owp_db_conn()
|
||||
conn = psycopg2.connect(**conn_params)
|
||||
sql_posts = "SELECT * from posts where status= 1;"
|
||||
posts = conn.execute(sql_posts).fetchall()
|
||||
cur = conn.cursor()
|
||||
cur.execute(sql_posts)
|
||||
cur_posts = cur.fetchall()
|
||||
conn.close()
|
||||
return render_template('blog/list.html', posts=posts[::-1])
|
||||
|
||||
dict_posts = [
|
||||
{
|
||||
"id": row[0],
|
||||
"date": row[1],
|
||||
"title": row[2],
|
||||
"content": row[3]
|
||||
} for row in cur_posts
|
||||
]
|
||||
return render_template('blog/list.html', posts = dict_posts[::-1])
|
||||
|
||||
# 迁移中
|
||||
@blog_bp.route('/posts/<int:posts_id>/')
|
||||
def show_posts_id(posts_id):
|
||||
# json
|
||||
message_path = f"/var/open-ww3-project-ww3-tw/static/message/{posts_id}"
|
||||
|
||||
if not os.path.exists(message_path):
|
||||
@@ -88,19 +142,35 @@ def show_posts_id(posts_id):
|
||||
json.dump([], file, ensure_ascii=False)
|
||||
message_ss = json.load(open(message_json, 'r', encoding='utf-8'))
|
||||
|
||||
|
||||
conn = get_owp_db_conn()
|
||||
conn = psycopg2.connect(**conn_params)
|
||||
cur = conn.cursor()
|
||||
sql_posts = f"SELECT * FROM posts WHERE status = 1 AND id = {posts_id};"
|
||||
cur.execute(sql_posts)
|
||||
posts = cur.fetchone()
|
||||
cur.close()
|
||||
conn.close()
|
||||
if posts is None:
|
||||
shutil.rmtree(message_path)
|
||||
return f'未找到该文章{posts_id}<title>未找到该文章{posts_id}/</title><br><a href="../">返回文章列表</a>', 404
|
||||
dict_posts = [
|
||||
{
|
||||
"id": posts[0],
|
||||
"date": posts[1],
|
||||
"title": posts[2],
|
||||
"content": posts[3],
|
||||
"status": posts[4],
|
||||
"update_time": posts[5]
|
||||
}
|
||||
]
|
||||
|
||||
conn_message = get_message_db_conn()
|
||||
sql_posts = "SELECT * FROM posts WHERE status = 1 AND id = ?"
|
||||
posts = conn.execute(sql_posts, (posts_id,)).fetchone()
|
||||
sql_message = "SELECT * FROM messages WHERE posts_id = ?"
|
||||
message = conn_message.execute(sql_message, (posts_id,)).fetchall()
|
||||
conn.close()
|
||||
conn_message.close()
|
||||
if posts is None:
|
||||
shutil.rmtree(message_path)
|
||||
return f'未找到该文章{posts_id}<title>未找到该文章{posts_id}/</title>', 404
|
||||
return render_template('blog/posts.html', posts=posts, message=message[::-1], message_ss=message_ss[::-1])
|
||||
|
||||
return render_template('blog/posts.html', posts = dict_posts, message = message[::-1], message_ss=message_ss[::-1])
|
||||
|
||||
|
||||
# posts 留言
|
||||
@blog_bp.route('/posts/<int:posts_id>/chat/', methods=['POST', 'GET'])
|
||||
|
||||
Reference in New Issue
Block a user