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 return conn def get_posts(posts_id): conn = get_db_conn() posts = conn.execute("SELECT *, datetime(created, '+8 hours') AS created_8 FROM posts WHERE id = ?", (posts_id,)).fetchone() conn.close() return posts @study_bp.route('/') def home(): conn = get_db_conn() sql = "SELECT *, datetime(created, '+8 hours') AS created_8 FROM posts;" posts = conn.execute(sql).fetchall() conn.close() return render_template('study/home.html', posts=posts[::-1]) @study_bp.route('/about/') def about(): return render_template('study/about.html') @study_bp.route('/posts/') def show_posts_id(posts_id): conn = get_db_conn() posts = get_posts(posts_id) if posts is None: return '抱歉,未查到该文章' return render_template('study/posts.html', posts=posts) @study_bp.route('/posts/new/', methods=('GET', 'POST')) def new(): if request.method == "POST": title = request.form['title'] content = request.form['content'] if not title: flash('标题不能为空') elif not content: flash('内容不能为空') else: conn = get_db_conn() conn.execute('insert into posts (title, content) values (?, ?)', (title, content)) conn.commit() conn.close() flash('文章发布成功') return redirect(url_for('study.home')) return render_template('study/new.html') @study_bp.route('/posts//edit', methods=('GET', 'POST')) def edit_posts_id(posts_id): posts = get_posts(posts_id) if request.method == "POST": title = request.form['title'] content = request.form['content'] if not title: flash('标题不能为空') elif not content: flash('内容不能为空') else: conn = get_db_conn() conn.execute('update posts set title= ?, content = ? where id = ?', (title, content, posts_id)) conn.commit() conn.close() return redirect(url_for('study.home')) return render_template('study/edit.html', posts=posts) @study_bp.route('/posts//delete/', methods=['POST' , 'GET']) def delete_posts_id(posts_id): if request.method == "POST": posts = get_posts(posts_id) conn = get_db_conn() conn.execute('DELETE FROM posts WHERE id = ?', (posts_id,)) conn.commit() conn.close() flash('删除成功') return redirect(url_for('study.home')) elif request.method == "GET": posts = get_posts(posts_id) conn = get_db_conn() conn.execute('DELETE FROM posts WHERE id = ?', (posts_id,)) conn.commit() conn.close() 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 "登录失败了.....
点击重新登录"