-
Notifications
You must be signed in to change notification settings - Fork 33
/
platform.py
134 lines (116 loc) · 4.48 KB
/
platform.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#encoding: utf-8
from flask import Flask, render_template, request, redirect, url_for, session
import config
from models import User, Question, Answer
from exts import db
from decorators import login_required
from sqlalchemy import or_
app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
@app.route('/')
def index():
context = {
'questions': Question.query.order_by('-create_time').all()
}
return render_template('index.html', **context)
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
telephone = request.form.get('telephone')
password = request.form.get('password')
user = User.query.filter(User.telephone == telephone, User.password ==
password).first()
if user:
session['user_id'] = user.id
# 如果想在31天内都不需要登录
session.permanent = True
return redirect(url_for('index'))
else:
return u'手机号码或者密码错误,请确认好在登录'
@app.route('/regist/', methods=['GET', 'POST'])
def regist():
if request.method == 'GET':
return render_template('regist.html')
else:
telephone = request.form.get('telephone')
username = request.form.get('username')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
# 手机号码验证,如果被注册了就不能用了
user = User.query.filter(User.telephone == telephone).first()
if user:
return u'该手机号码被注册,请更换手机'
else:
# password1 要和password2相等才可以
if password1 != password2:
return u'两次密码不相等,请核实后再填写'
else:
user = User(telephone=telephone, username=username, password=password1)
db.session.add(user)
db.session.commit()
# 如果注册成功,就让页面跳转到登录的页面
return redirect(url_for('login'))
# 判断用户是否登录,只要我们从session中拿到数据就好了 注销函数
@app.route('/logout/')
def logout():
# session.pop('user_id')
# del session('user_id')
session.clear()
return redirect(url_for('login'))
@app.route('/question/', methods=['GET', 'POST'])
@login_required
def question():
if request.method == 'GET':
return render_template('question.html')
else:
title = request.form.get('title')
content = request.form.get('content')
question = Question(title=title, content=content)
user_id = session.get('user_id')
user = User.query.filter(User.id == user_id).first()
question.author = user
db.session.add(question)
db.session.commit()
return redirect(url_for('index'))
@app.route('/detail/<question_id>/')
def detail(question_id):
question_model = Question.query.filter(Question.id == question_id).first()
return render_template('detail.html', question=question_model)
@app.route('/add_answer/', methods=['POST'])
@login_required
def add_answer():
content = request.form.get('answer_content')
question_id = request.form.get('question_id')
answer = Answer(content=content)
user_id = session['user_id']
user = User.query.filter(User.id == user_id).first()
answer.author = user
question = Question.query.filter(Question.id == question_id).first()
answer.question = question
db.session.add(answer)
db.session.commit()
return redirect(url_for('detail', question_id=question_id))
@app.route('/search/')
def search():
q = request.args.get('q')
# title, content
# 或 查找方式(通过标题和内容来查找)
# questions = Question.query.filter(or_(Question.title.contains(q),
# Question.content.constraints(q))).order_by('-create_time')
# 与 查找(只能通过标题来查找)
questions = Question.query.filter(Question.title.contains(q), Question.content.contains(q))
return render_template('index.html', questions=questions)
# 钩子函数(注销)
@app.context_processor
def my_context_processor():
user_id = session.get('user_id')
if user_id:
user = User.query.filter(User.id == user_id).first()
if user:
return {'user': user}
return {}
if __name__ == '__main__':
app.run(debug=True)