1+ # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+
15+ from flask import Flask , render_template , request , redirect , url_for , flash , session
16+ from models import db , Post , User
17+ from werkzeug .security import generate_password_hash , check_password_hash
18+ import os
19+
20+ app = Flask (__name__ )
21+ app .config ['SECRET_KEY' ] = 'your-secret-key-here'
22+ app .config ['SQLALCHEMY_DATABASE_URI' ] = 'sqlite:///blog.db'
23+ app .config ['SQLALCHEMY_TRACK_MODIFICATIONS' ] = False
24+
25+ db .init_app (app )
26+
27+ # 前台首页
28+ @app .route ('/' )
29+ def index ():
30+ page = request .args .get ('page' , 1 , type = int )
31+ posts = Post .query .order_by (Post .created_at .desc ()).paginate (
32+ page = page , per_page = 5 , error_out = False )
33+ return render_template ('index.html' , posts = posts )
34+
35+ # 文章详情页
36+ @app .route ('/post/<int:post_id>' )
37+ def post_detail (post_id ):
38+ post = Post .query .get_or_404 (post_id )
39+ return render_template ('post.html' , post = post )
40+
41+ # 后台登录页
42+ @app .route ('/admin/login' , methods = ['GET' , 'POST' ])
43+ def admin_login ():
44+ if request .method == 'POST' :
45+ username = request .form ['username' ]
46+ password = request .form ['password' ]
47+
48+ user = User .query .filter_by (username = username ).first ()
49+
50+ if user and check_password_hash (user .password , password ):
51+ session ['admin_logged_in' ] = True
52+ return redirect (url_for ('admin_dashboard' ))
53+ else :
54+ flash ('用户名或密码错误' )
55+
56+ return render_template ('admin/login.html' )
57+
58+ # 后台登出
59+ @app .route ('/admin/logout' )
60+ def admin_logout ():
61+ session .pop ('admin_logged_in' , None )
62+ return redirect (url_for ('admin_login' ))
63+
64+ # 后台管理面板
65+ @app .route ('/admin/dashboard' )
66+ def admin_dashboard ():
67+ if not session .get ('admin_logged_in' ):
68+ return redirect (url_for ('admin_login' ))
69+
70+ post_count = Post .query .count ()
71+ return render_template ('admin/dashboard.html' , post_count = post_count )
72+
73+ # 文章管理
74+ @app .route ('/admin/posts' )
75+ def admin_posts ():
76+ if not session .get ('admin_logged_in' ):
77+ return redirect (url_for ('admin_login' ))
78+
79+ page = request .args .get ('page' , 1 , type = int )
80+ posts = Post .query .order_by (Post .created_at .desc ()).paginate (
81+ page = page , per_page = 10 , error_out = False )
82+ return render_template ('admin/posts.html' , posts = posts )
83+
84+ # 创建/编辑文章
85+ @app .route ('/admin/post' , methods = ['GET' , 'POST' ])
86+ @app .route ('/admin/post/<int:post_id>' , methods = ['GET' , 'POST' ])
87+ def admin_edit_post (post_id = None ):
88+ if not session .get ('admin_logged_in' ):
89+ return redirect (url_for ('admin_login' ))
90+
91+ if post_id :
92+ post = Post .query .get_or_404 (post_id )
93+ else :
94+ post = Post ()
95+
96+ if request .method == 'POST' :
97+ post .title = request .form ['title' ]
98+ post .content = request .form ['content' ]
99+
100+ if post_id is None :
101+ db .session .add (post )
102+ db .session .commit ()
103+ flash ('文章保存成功' )
104+ return redirect (url_for ('admin_posts' ))
105+
106+ return render_template ('admin/edit_post.html' , post = post )
107+
108+ # 删除文章
109+ @app .route ('/admin/post/delete/<int:post_id>' , methods = ['POST' ])
110+ def admin_delete_post (post_id ):
111+ if not session .get ('admin_logged_in' ):
112+ return redirect (url_for ('admin_login' ))
113+
114+ post = Post .query .get_or_404 (post_id )
115+ db .session .delete (post )
116+ db .session .commit ()
117+ flash ('文章删除成功' )
118+ return redirect (url_for ('admin_posts' ))
119+
120+ if __name__ == '__main__' :
121+ app .run (debug = True )
0 commit comments