迁移数据库为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'])
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
from flask import Blueprint, render_template, request, url_for, flash, redirect
|
||||
from flask import Blueprint, render_template, request, url_for, flash, redirect, session
|
||||
import os
|
||||
import sqlite3
|
||||
import psycopg2, json, hashlib
|
||||
|
||||
study_bp = Blueprint('study', __name__)
|
||||
|
||||
# pgdb_conn
|
||||
# 读取json文件
|
||||
with open('/var/open-ww3-project-ww3-tw/config/study/db_config.json', 'r') as file:
|
||||
data = json.load(file)
|
||||
print(data)
|
||||
# 设定数据库连接配置
|
||||
conn_params = {
|
||||
"dbname" : data['dbname'],
|
||||
"user" : data['user'],
|
||||
"password": data['password'],
|
||||
"host": data['host'],
|
||||
"port": data['port']
|
||||
}
|
||||
|
||||
def get_db_conn():
|
||||
conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/database.db')
|
||||
conn.row_factory = sqlite3.Row
|
||||
@@ -93,4 +108,99 @@ def delete_posts_id(posts_id):
|
||||
conn.execute('DELETE FROM posts WHERE id = ?', (posts_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return render_template('study/delete.html', posts=posts)
|
||||
return render_template('study/delete.html', posts=posts)
|
||||
|
||||
@study_bp.route('/bbs/', methods=['POST' , 'GET'])
|
||||
def pgdb():
|
||||
# 连接数据库
|
||||
try:
|
||||
conn = psycopg2.connect(**conn_params)
|
||||
print("连接没问题!!!")
|
||||
cur = conn.cursor()
|
||||
# cur.execute("select * from study_pg;")
|
||||
# db_cur = cur.fetchone()
|
||||
cur.close()
|
||||
conn.close()
|
||||
# return f"连接没问题!!!<br>数据库内容: {db_cur}"
|
||||
return render_template("study/bbs.html")
|
||||
# , db_cur = db_cur)
|
||||
except Exception as e:
|
||||
print(f"出现了一点状况: {e}")
|
||||
cur = conn.cursor()
|
||||
cur.close()
|
||||
conn.close()
|
||||
return f"出现了一点状况: {e}"
|
||||
|
||||
@study_bp.route('/bbs/register/', methods=['POST' , 'GET'])
|
||||
def pgdb_register():
|
||||
if request.method == "GET":
|
||||
return render_template('study/register.html')
|
||||
|
||||
if request.method == "POST":
|
||||
username = request.form.get('username')
|
||||
passwd = request.form.get('passwd')
|
||||
age = request.form.get('age')
|
||||
hash_obj = hashlib.sha256()
|
||||
hash_obj.update(passwd.encode())
|
||||
passwd_hash = hash_obj.hexdigest()
|
||||
nbsp = " " *4
|
||||
|
||||
# 数据库操作
|
||||
conn = psycopg2.connect(**conn_params)
|
||||
cur = conn.cursor()
|
||||
sql_check = f"select username from user_temp where username = '{username}';"
|
||||
cur.execute(sql_check)
|
||||
sql_name = cur.fetchone()
|
||||
# cur.close()
|
||||
# conn.close()
|
||||
if sql_name == None:
|
||||
sql_insert = f"insert into user_temp (username, passwd, age) values ('{username}', '{passwd_hash}', '{age}');"
|
||||
cur.execute(sql_insert)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return "名字没问题允许注册" \
|
||||
"用户名: " f"{nbsp}" f"{username}" "<br>" \
|
||||
"密码: " f"{nbsp}" f"{passwd}" "<br>" \
|
||||
"年龄: " f"{nbsp}" "**" "<br>" "<br>" \
|
||||
"<b>密码为哈希存储,只会展示一次<br>" \
|
||||
"如果忘记密码了,建议重新注册</b>" \
|
||||
# "<br>" "<br>" f"{sql_name}"
|
||||
else:
|
||||
conn.close()
|
||||
return "名字大错特错!!!不允许注册<br><a href='./'>返回注册页面</a>"
|
||||
# sql_insert = f"insert into user_temp (username, passwd, age) values ('{username}', '{passwd_hash}', '{age}');"
|
||||
# cur.execute(sql_insert)
|
||||
# conn.commit()
|
||||
# cur.close()
|
||||
# conn.close()
|
||||
|
||||
|
||||
@study_bp.route('/bbs/login/', methods=['POST', 'GET'])
|
||||
def pgdb_login():
|
||||
if request.method == "GET":
|
||||
return render_template('study/login.html')
|
||||
|
||||
if request.method == "POST":
|
||||
username = request.form.get('username', '')
|
||||
passwd = request.form.get('passwd', '')
|
||||
|
||||
if not username or not passwd:
|
||||
return "用户名或密码不能空<br><a href='./'>返回重登</a>"
|
||||
|
||||
hash_passwd = hashlib.sha256(passwd.encode()).hexdigest()
|
||||
|
||||
conn = psycopg2.connect(**conn_params)
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"SELECT id, passwd FROM user_temp WHERE username = %s",
|
||||
(username,)
|
||||
)
|
||||
row = cur.fetchone()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
if row and row[1] == hash_passwd:
|
||||
session['user_id'] = row[0]
|
||||
return "登录成功<br>"
|
||||
else:
|
||||
return "登录失败了.....<br><a href='./'>点击重新登录</a>"
|
||||
Reference in New Issue
Block a user