forked from kingname/SourceCodeofMongoRedis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (133 loc) · 6.19 KB
/
Copy pathmain.py
File metadata and controls
157 lines (133 loc) · 6.19 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import json
import datetime
from werkzeug.security import check_password_hash, generate_password_hash
from your_code_here.MongoUtil import MongoUtil
from your_code_here.RedisUtil import RedisUtil
from util.utils import check_answer_valid, check_question_valid, check_vote, generate_session
from flask import Flask, render_template, request, make_response
app = Flask(__name__)
mongo = MongoUtil()
redis = RedisUtil()
@app.route('/')
def index():
session_data = redis.fetch_session(request.cookies.get('session'))
question_list = mongo.query_question()
return render_template('index.html', question_list=question_list, session=session_data)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
body = request.json
user = body.get('user', '')
password = body.get('password', '')
if not all([user, password]):
return json.dumps({'success': False, 'reason': '用户名和密码不能同时为空!'}, ensure_ascii=False)
user_info = mongo.get_user_info(user)
if not user_info['success']:
return json.dumps(user_info)
password_hash = user_info['password_hash']
if not check_password_hash(password_hash, password):
return json.dumps({'success': False, 'reason': '用户名与密码不匹配!'})
session_id, session_data = generate_session(str(user_info['user_info']['_id']), user)
response = make_response(json.dumps({'success': True}))
response.set_cookie('session', value=session_id)
return response
@app.route('/register', methods=['POST'])
def register():
body = request.json
user = body.get('user', '')
password = body.get('password', '')
if not all([user, password]):
return json.dumps({'success': False, 'reason': '用户名和密码不能同时为空!'}, ensure_ascii=False)
if redis.check_user_registered(user):
return json.dumps({'success': False, 'reason': '用户名已被注册!请更换'})
password_hash = generate_password_hash(password)
user_id = mongo.save_user_info(user, password_hash)
session_id, session_data = generate_session(user_id, user)
redis.save_session(session_id, session_data)
response = make_response(json.dumps({'success': True}))
response.set_cookie('session', value=session_id)
return response
@app.route('/question/<question_id>')
def question_detail(question_id=None):
session_data = redis.fetch_session(request.cookies.get('session'))
question_answer_dict = mongo.query_answer(question_id)
could_answer_this_question = True
if 'user' not in session_data or redis.check_user_answer_question(session_data['user'], question_id):
could_answer_this_question = False
session_data['could_answer_this_question'] = could_answer_this_question
return render_template('answer_list.html', question_answer_dict=question_answer_dict, session=session_data)
@app.route('/post_answer', methods=['POST'])
def post_answer():
session_data = redis.fetch_session(request.cookies.get('session'))
if not session_data:
return json.dumps({'success': False, 'reason': '未登录!'})
answer = request.json
is_valid = check_answer_valid(answer)
if not is_valid['success']:
return json.dumps(is_valid, ensure_ascii=False)
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
question_id = answer['question_id']
if redis.check_user_answer_question(session_data['user'], question_id):
return json.dumps({'success': False, 'reason': '已经回答过这个问题,不能回答两次。'})
author = session_data['user']
answer = answer['answer']
mongo.insert_answer(question_id, answer, author, now)
redis.set_answer_flag(question_id, author)
return json.dumps({'success': True})
@app.route('/post_question', methods=['POST'])
def post_question():
session_data = redis.fetch_session(request.cookies.get('session'))
if not session_data:
return json.dumps({'success': False, 'reason': '未登录!'})
question = request.json
is_valid = check_question_valid(question)
if not is_valid['success']:
return json.dumps(is_valid, ensure_ascii=False)
title = question['title']
detail = question['detail']
author = session_data['user']
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
doc_id = mongo.insert_question(title, detail, author, now)
return json.dumps({'success': True, 'doc_id': doc_id})
def update_question(info):
if 'doc_id' not in info or 'title' not in info or 'text' not in info:
return {'success': False, 'reason': '字段不全,必要字段:doc_id, title, text'}
mongo.update_question(info['doc_id'], info['title'], info['text'])
return {'success': True}
def update_answer(info):
if 'doc_id' not in info or 'text' not in info:
return {'success': False, 'reason': '字段不全,必要字段:doc_id, text'}
mongo.update_answer(info['doc_id'], info['text'])
return {'success': True}
@app.route('/update', methods=['POST'])
def update():
session_data = redis.fetch_session(request.cookies.get('session'))
if not session_data:
return json.dumps({'success': False, 'reason': '未登录!'})
info = request.json
update_type = info.get('update_type')
if update_type == 'question':
return json.dumps(update_question(info))
elif update_type == 'answer':
return json.dumps(update_answer(info))
else:
return json.dumps({'success': False, 'reason': 'update_type只能是qustion或者answer'})
@app.route('/vote', methods=['POST'])
def vote():
session_data = redis.fetch_session(request.cookies.get('session'))
if not session_data:
return json.dumps({'success': False, 'reason': '未登录!'})
vote_data = request.json
is_valid = check_vote(vote_data)
if not is_valid['success']:
return json.dumps(is_valid)
doc_type = vote_data['doc_type']
value = vote_data['value']
doc_id = vote_data['doc_id']
if doc_type == 'question':
mongo.vote_for_question(doc_id, value)
else:
mongo.vote_for_answer(doc_id, value)
return json.dumps({'success': True})