From 7f0f9df727550dbbdb5ee0b6962bc4a4fc38ea74 Mon Sep 17 00:00:00 2001 From: skimrme Date: Fri, 17 Jul 2026 18:43:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E4=B8=BApostgresql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +- blueprint/blog_views.py | 106 +++++++++++++++++++++++++------ blueprint/study_views.py | 114 +++++++++++++++++++++++++++++++++- requirements.txt | 1 + start-web.sh | 5 +- templates/blog/about.html | 7 ++- templates/blog/posts.html | 22 ++++--- templates/study/bbs.html | 21 +++++++ templates/study/login.html | 19 ++++++ templates/study/register.html | 23 +++++++ 10 files changed, 290 insertions(+), 33 deletions(-) create mode 100644 templates/study/bbs.html create mode 100644 templates/study/login.html create mode 100644 templates/study/register.html diff --git a/.gitignore b/.gitignore index 5cbe4d1..f372889 100755 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,7 @@ templates/message/* static/upload/* static/counter.txt static/message/* -static/test/* \ No newline at end of file +static/test/* + +# config +config/** \ No newline at end of file diff --git a/blueprint/blog_views.py b/blueprint/blog_views.py index 74d6762..fa1db52 100755 --- a/blueprint/blog_views.py +++ b/blueprint/blog_views.py @@ -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//') 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}未找到该文章{posts_id}/
返回文章列表', 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}未找到该文章{posts_id}/', 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//chat/', methods=['POST', 'GET']) diff --git a/blueprint/study_views.py b/blueprint/study_views.py index 75564f5..a363fc5 100755 --- a/blueprint/study_views.py +++ b/blueprint/study_views.py @@ -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) \ No newline at end of file + 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"连接没问题!!!
数据库内容: {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}" "
" \ + "密码: " f"{nbsp}" f"{passwd}" "
" \ + "年龄: " f"{nbsp}" "**" "
" "
" \ + "密码为哈希存储,只会展示一次
" \ + "如果忘记密码了,建议重新注册
" \ + # "
" "
" f"{sql_name}" + else: + conn.close() + return "名字大错特错!!!不允许注册
返回注册页面" + # 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 "用户名或密码不能空
返回重登" + + 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 "登录成功
" + else: + return "登录失败了.....
点击重新登录" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index f227d2c..c0bc94e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ future==1.0.0 itsdangerous==2.2.0 Jinja2==3.1.6 MarkupSafe==3.0.3 +psycopg2==2.9.12 Werkzeug==3.1.4 diff --git a/start-web.sh b/start-web.sh index 4faf9e8..2fe1d3d 100755 --- a/start-web.sh +++ b/start-web.sh @@ -1,3 +1,6 @@ #!/bin/sh -ln -svf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && python3 /var/open-ww3-project-ww3-tw/main.py +#ln -svf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && python3 /var/open-ww3-project-ww3-tw/main.py + +ln -svf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ +/var/open-ww3-project-ww3-tw/venv/bin/python /var/open-ww3-project-ww3-tw/main.py diff --git a/templates/blog/about.html b/templates/blog/about.html index 2412b19..1017782 100755 --- a/templates/blog/about.html +++ b/templates/blog/about.html @@ -10,7 +10,12 @@
返回首页__网页地图
- 本站建立于大概2025年8月前后
+ 数据库迁移至postgresql +
+

2026.07.11

+
+
+ 本站建立于大概2025年8月前后
差不多是个人博客多次重建后的成果
如今我已经成长很多

diff --git a/templates/blog/posts.html b/templates/blog/posts.html index eb06da2..5c3f111 100755 --- a/templates/blog/posts.html +++ b/templates/blog/posts.html @@ -1,6 +1,6 @@ - + {% for posts in posts %} {{ posts['title'] }} @@ -8,19 +8,20 @@ - + {% endfor %} 返回主页

{{ posts['title'] }}

-
-
- {{ posts['content'].replace('&a&a','') | safe}} -
-
-
- 最后修改时间: 【{{ posts['update_time'].replace('.', '-') }}】 + {% for posts in posts %} + {{ posts['content'].replace('&a&a','') | safe}} +
+
+
+ 最后修改时间: 【{{ posts['update_time'].replace('.', '-') }}】 + {% endfor %} +

留言区:

@@ -45,6 +46,7 @@ {% endfor %} +