-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
82 lines (68 loc) · 2.49 KB
/
app.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
'''
2020/1/25
donke
'''
from flask import session, jsonify, request, url_for, redirect
from flask import Flask, make_response, render_template
from models import Chengyu
from sqlalchemy import func
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import os
import json
from pypinyin import lazy_pinyin
from handler import AlchemyEncoder
from datetime import timedelta
app = Flask(__name__)
# app.config['SECRET_KEY'] = os.urandom(24) # 设置为24位的字符,每次运行服务器都是不同的,所以服务器启动一次上次的session就清除。
app.config['SECRET_KEY'] = '[email protected]**o&vxd.-cv*pp#sdl' # session key
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7) # 设置session的保存时间。
engine = create_engine('mysql+mysqlconnector://root:password@localhost/chengyu',
encoding='utf-8', echo=False, pool_size=100, pool_recycle=10)
Base = declarative_base()
DBSession = sessionmaker(bind=engine)
@app.route('/')
def hello_world():
ip = request.remote_addr
session.permanent = True # 默认session的时间持续31天
session['ip'] = ip
session['is_login'] = True
print(ip)
return render_template('index.html')
@app.route('/search')
def test():
if not session.get('is_login'):
return redirect(url_for('hello_world'))
key = request.args.get('key')
key = lazy_pinyin(key)[0]
print(key)
db_session = DBSession()
page_index = 1
data = db_session.query(Chengyu).filter(Chengyu.spy == key).limit(50).offset((page_index-1)*50).all()
print(data)
res = {"data": []}
for i in data:
temp = json.dumps(i, cls=AlchemyEncoder)
temp = json.loads(temp)
res["data"].append(temp)
res["status"] = 0
res["count"] = db_session.query(Chengyu).filter(Chengyu.spy == key).count()
res["respNum"] = len(data)
print(res)
return render_template('index.html', chengyu=res)
# return make_response(jsonify(res))
@app.route('/json/test/<int:pn>/', methods=['GET'])
def json_test(pn=1):
db_session = DBSession()
page_index = pn
data = db_session.query(Chengyu).filter(Chengyu.spy == 'ce').limit(50).offset((page_index - 1) * 50).all()
res = {"data": []}
for i in data:
temp = json.dumps(i, cls=AlchemyEncoder)
temp = json.loads(temp)
res["data"].append(temp)
return make_response(jsonify(res))
if __name__ == '__main__':
app.run(port=8000)
# test()