-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
99 lines (74 loc) · 2.61 KB
/
app.py
File metadata and controls
99 lines (74 loc) · 2.61 KB
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
from flask import Flask, render_template, request, session, Response
from sqlalchemy import desc
import config
from model import db, User, Article
app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
db.create_all(app=app)
def isLogin():
return 'uid' in session and session['uid']
@app.route('/')
def index():
return render_template('list.html', alist=Article.query.order_by(desc(Article.create_time)))
@app.route('/a/<int:aid>')
def archive(aid):
a: Article = Article.query.filter_by(id=aid).first()
if not a:
return Response('资源未找到', status=404)
u = User.query.filter_by(id=a.uid).first()
return render_template('archive.html', title=a.title, content=a.content, time=a.create_time, username=u.username)
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method != 'POST':
return render_template('register.html')
username, password = request.form.get('username', ''), request.form.get('password', '')
u = User()
u.username = username
u.password = password
db.session.add(u)
db.session.commit()
return '注册成功'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method != 'POST':
return render_template('login.html')
username, password = request.form.get('username', ''), request.form.get('password', '')
a = User.query.filter_by(username=username, password=password).first()
if a:
session['uid'] = a.id
return '登录成功'
else:
return '用户名或密码错误'
@app.route('/main')
def main():
a = User.query.filter_by(id=session['uid']).first()
return a.username
@app.route('/publish', methods=['GET', 'POST'])
def publish():
if request.method != 'POST':
return render_template('publish.html')
title, content = request.form.get('title', ''), request.form.get('content', '')
a = Article()
a.title = title
a.content = content
a.uid = session['uid']
db.session.add(a) # insert
db.session.commit()
return '发布成功'
@app.route('/edit/<int:aid>', methods=['GET', 'POST'])
def edit(aid):
a: Article = Article.query.filter_by(id=aid).first()
if not a:
return Response('资源未找到', status=404)
if request.method != 'POST':
return render_template('edit.html', title=a.title, content=a.content)
title, content = request.form.get('title', ''), request.form.get('content', '')
a.title = title
a.content = content
a.uid = session['uid']
db.session.merge(a)
db.session.commit()
return '发布成功'
if __name__ == '__main__':
app.run()