-
Notifications
You must be signed in to change notification settings - Fork 0
/
myapp.py
115 lines (94 loc) · 3.7 KB
/
myapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
from flask import Flask, render_template, flash, redirect, url_for
from flask_login import LoginManager, login_user, current_user, logout_user
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import check_password_hash
from flask_bootstrap import Bootstrap
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from forms import LoginForm, RegisterForm, PostForm
app = Flask(__name__)
app.secret_key = os.urandom(16)
app.config.from_pyfile('config.py')
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
from models import User, Post
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'login'
login_manager.init_app(app=app)
admin = Admin(app, name="z_blog", template_mode='bootstrap3')
admin.add_view(ModelView(User, db.session))
admin.add_view(ModelView(Post, db.session))
@login_manager.user_loader
def load_user(user_id):
from models import User
return User.query.get(int(user_id))
@app.route('/')
@app.route('/index')
def index():
all_posts = Post.query.order_by(-Post.post_time).all() # 查询所有已发布文章并根据发布时间逆序排列
return render_template('index.html', posts=all_posts, posts_len=len(all_posts))
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated: # 如果用户已登录则直接跳转到首页
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(user_name=form.user_name.data).first()
if user is None:
flash('此用户不存在!')
elif check_password_hash(user.password_hash, form.password.data):
login_user(user)
return redirect(url_for('index'))
else:
flash('密码错误!')
return render_template('form.html', form=form)
@app.route('/logout')
def logout():
if not current_user.is_authenticated:
flash('请先登录!')
return redirect(url_for('login'))
logout_user()
flash('你已退出登录.')
return redirect(url_for('index'))
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated: # 如果用户已登录则直接跳转到首页
return redirect(url_for('index'))
form = RegisterForm()
if form.validate_on_submit():
user = User.query.filter_by(user_name=form.user_name.data).first()
if user is not None:
flash('此用户已存在!')
return render_template('form.html', form=form)
elif form.password.data != form.password_check.data:
flash('两次输入密码不一致!')
return render_template('form.html', form=form)
else:
user = User(form.user_name.data, form.password.data)
db.session.add(user)
db.session.commit()
flash('注册成功, 请登录!')
return redirect(url_for('login'))
return render_template('form.html', form=form)
@app.route('/post', methods=['GET', 'POST'])
def post():
if not current_user.is_authenticated:
flash('请先登录!')
return redirect(url_for('login'))
form = PostForm()
if form.validate_on_submit():
# TODO: 文章名过长时的错误提示
post = Post(form.title.data, form.content.data)
post.author = current_user
db.session.add(post)
db.session.commit()
return redirect(url_for('index'))
return render_template('post.html', form=form)
@app.route('/posts/<int:post_id>')
def posts(post_id):
post = Post.query.filter_by(id=post_id).first()
if not post:
return redirect(url_for('index'))
return render_template('page.html', post=post)