修改主页,感觉还可以

This commit is contained in:
2025-12-27 13:35:20 +08:00
parent 7c94590c40
commit 8ca06abe4a
20 changed files with 353 additions and 337 deletions

View File

@@ -1,46 +1,54 @@
from flask import Blueprint, render_template, request, url_for, flash, redirect, send_from_directory, make_response, current_app
from database import get_owp_db
from flask_autoindex import AutoIndex
import os
import sqlite3
import time
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__)
index_path = '/var/open-ww3-project-ww3-tw/mirrors/'
index = AutoIndex(blog_bp, browse_root=index_path, add_url_rules=False)
@blog_bp.route('/')
def home():
db = get_owp_db()
conn = get_owp_db_conn()
sql_logs = "SELECT * from logs;"
sql_posts = "SELECT * from posts;"
sql_posts = "SELECT * from posts where status= 1;"
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])
return render_template('blog/home.html', logs=logs[::-1], posts=posts[::-1])
@blog_bp.route('/about/')
def about():
return render_template('about.html')
return render_template('blog/about.html')
pass
@blog_bp.route('/posts/')
def posts_list():
conn = get_owp_db_conn()
sql_posts = "SELECT * from posts;"
sql_posts = "SELECT * from posts where status= 1;"
posts = conn.execute(sql_posts).fetchall()
conn.close()
return render_template('list.html', posts=posts[::-1])
return render_template('blog/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 = ?"
sql_posts = "SELECT * FROM posts WHERE status = 1 AND id = ?"
posts = conn.execute(sql_posts, (posts_id,)).fetchone()
conn.close()
return render_template('posts.html', posts=posts)
if posts is None:
return f'未找到该文章{posts_id}<title>未找到该文章{posts_id}</title>', 404
return render_template('blog/posts.html', posts=posts)
@@ -72,14 +80,32 @@ def google02f6a3f6004a32c6():
@blog_bp.route('/sitemap.xml/')
def sitemap():
conn = get_owp_db_conn()
sql_posts = "SELECT * from posts;"
sql_posts = "SELECT * from posts where status = 1;"
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/")
template = render_template('blog/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
@blog_bp.route('/kami/')
def kami():
return redirect(url_for('kami.home'))
@blog_bp.route('/mirrors/')
@blog_bp.route('/mirrors/<path:path>')
def autoindex(path='.'):
return index.render_autoindex(path, template='blog/mirrors.html')
@blog_bp.route('/upload/', methods=['POST' , 'GET'])
def upload():
if request.method == "POST":
f = request.files.get('img')
filename = f.filename
with open(f'/var/open-ww3-project-ww3-tw/static/upload/img/{filename}', 'wb') as tf:
tf.write(f.read())
return '上传成功<meta http-equiv="refresh" content="1.2;url=https://ww3.tw/blog/upload/">'
img_files = '/var/open-ww3-project-ww3-tw/static/upload/img/'
images = [img for img in os.listdir(img_files) if img.endswith(('.png', '.jpg', '.jpeg', '.gif'))]
return render_template('blog/upload.html', images=images)