Files
open-ww3-project-ww3-tw/blueprint/blog_views.py
2025-12-23 13:03:36 +08:00

85 lines
2.4 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from flask import Blueprint, render_template, request, url_for, flash, redirect, send_from_directory, make_response, current_app
from database import get_owp_db
import os
import sqlite3
def get_owp_db_conn():
conn = sqlite3.connect('/var/open-ww3-project-ww3-tw/databases/sqlite/owp.db')
conn.row_factory = sqlite3.Row
return conn
blog_bp = Blueprint('blog', __name__)
@blog_bp.route('/')
def home():
db = get_owp_db()
conn = get_owp_db_conn()
sql_logs = "SELECT * from logs;"
sql_posts = "SELECT * from posts;"
logs = conn.execute(sql_logs).fetchall()
posts = conn.execute(sql_posts).fetchall()
conn.close()
return render_template('home.html', logs=logs[::-1], posts=posts[::-1])
@blog_bp.route('/about/')
def about():
return render_template('about.html')
pass
@blog_bp.route('/posts/')
def posts_list():
conn = get_owp_db_conn()
sql_posts = "SELECT * from posts;"
posts = conn.execute(sql_posts).fetchall()
conn.close()
return render_template('list.html', posts=posts[::-1])
@blog_bp.route('/posts/<int:posts_id>/')
def show_posts_id(posts_id):
conn = get_owp_db_conn()
sql_posts = "SELECT * FROM posts WHERE id = ?"
posts = conn.execute(sql_posts, (posts_id,)).fetchone()
conn.close()
return render_template('posts.html', posts=posts)
# 数据库测试
@blog_bp.route('/db_test/')
def test_db():
db = get_owp_db()
try:
cur = db.execute('SELECT sqlite_version();')
ver = cur.fetchone()[0]
return f"数据库连接成功SQLite 版本是: {ver}"
except Exception as e:
return f"连接失败: {str(e)}"
######
## 网站地图 及其 robots 配置
@blog_bp.route('/robots.txt/')
def robots():
return send_from_directory(current_app.static_folder, 'robots.txt')
@blog_bp.route('/google02f6a3f6004a32c6.html')
def google02f6a3f6004a32c6():
return send_from_directory(blog_bp.static_folder, 'google02f6a3f6004a32c6.html')
@blog_bp.route('/sitemap.xml/')
def sitemap():
conn = get_owp_db_conn()
sql_posts = "SELECT * from posts;"
posts = conn.execute(sql_posts).fetchall()
conn.close()
template = render_template('sitemap.xml', posts=posts[::-1], base_url="https://open-ww3-project.ww3.tw/blog/")
response = make_response(template)
response.headers['Content-Type'] = 'application/xml'
return response
@blog_bp.errorhandler(404)
def not_found_error(error):
return '404 not found', 404