-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeb.py
255 lines (227 loc) · 6.93 KB
/
Web.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
__author__ = "Hua777"
__copyright__ = "Copyright 2018, Hua777"
__version__ = "2.0"
__email__ = "[email protected]"
from flask import Flask, jsonify, request, make_response, render_template, redirect, url_for
import traceback as TB
import os as OS
import requests as REQ
import time
from Agent import Agent
from Schedule import Schedule
from ClassesDB import ClassesDB as DB
from ValidCode import ValidCode, LoadForwardData
app = Flask(__name__, static_url_path='')
app.config['JSON_AS_ASCII'] = False
ValidCodeModel = None
def MyAgent(req):
try:
id_ = req.cookies.get('Id')
return Global['Agents'][id_]
except:
return None
Global = {
'MyAgent': MyAgent,
'Agents': {},
'Id': 0
}
# 首页
@app.route('/', methods=['GET'])
def index():
try:
global Global
global ValidCodeModel
a = Global['MyAgent'](request)
if a is not None and a.Logined:
return render_template('index.html', agent = a)
else:
a = Agent()
resp = make_response(render_template('index.html', agent = a, vc = ValidCodeModel, vcread = LoadForwardData))
resp.set_cookie('Id', str(Global['Id']), expires = time.time() + 1 * 60 * 60)
Global['Agents'][str(Global['Id'])] = a
Global['Id'] += 1
return resp
except REQ.exceptions.Timeout:
TB.print_exc()
return render_template('timeout.html')
except:
TB.print_exc()
return render_template('error.html')
# 关于
@app.route('/about', methods=['GET'])
def about():
try:
global Global
global ValidCodeModel
a = Global['MyAgent'](request)
return render_template('about.html', agent = a, vc = ValidCodeModel, vcread = LoadForwardData)
except:
TB.print_exc()
return redirect(url_for('logout'))
# 课程查询
@app.route('/feedback', methods=['GET'])
def feedback():
try:
global Global
global ValidCodeModel
a = Global['MyAgent'](request)
return render_template('feedback.html', agent = a, vc = ValidCodeModel, vcread = LoadForwardData)
except:
TB.print_exc()
return redirect(url_for('logout'))
# 登入首页(废弃)
@app.route('/main', methods=['GET'])
def main():
try:
return redirect(url_for('selected'))
except:
TB.print_exc()
return redirect(url_for('logout'))
# 目前已经选的课
@app.route('/selected', methods=['GET'])
def selected():
try:
global Global
a = Global['MyAgent'](request)
return render_template('selected.html', agent = a, schedule = Schedule(a))
except:
TB.print_exc()
return redirect(url_for('logout'))
# 课程查询
@app.route('/classes', methods=['GET'])
def classes():
try:
global Global
a = Global['MyAgent'](request)
return render_template('classes.html', agent = a, schedule = Schedule(a), db = DB())
except:
TB.print_exc()
return redirect(url_for('logout'))
# 加退选1
@app.route('/select', methods=['GET'])
def select():
try:
return redirect(url_for('classes'))
except:
TB.print_exc()
return redirect(url_for('logout'))
# 课程查询
@app.route('/logout', methods=['GET'])
def logout():
try:
global Global
Global['Agents'].pop(request.cookies.get('Id'))
return redirect(url_for('index'))
except:
TB.print_exc()
return redirect(url_for('index'))
'''''''''''''''''''''''''''''''''''''''''''''
API Start
'''''''''''''''''''''''''''''''''''''''''''''
# 登入
@app.route('/api/login', methods=['POST'])
def api_login():
try:
global Global
a = Global['MyAgent'](request)
data = request.get_json(force=True)
db = DB()
login = a.Login(data['username'], data['password'], data['validcode'])
if login[0]:
srcpath = 'static/img/validcode/' + data['validcodeSrc']
corpath = 'static/img/validcode/' + data['validcode'] + '.' + data['validcodeSrc']
OS.rename(srcpath, corpath)
return (jsonify({'check': login[0], 'msg': login[1]}))
except:
TB.print_exc()
return (jsonify({'check': False}))
# 单一课程资讯查询
@app.route('/api/classinfo', methods=['POST'])
def api_classinfo():
try:
data = request.get_json(force=True)
db = DB()
c = db.CrawlClassByNumber(data['number'])
d = c.ToDict()
d['Check'] = True
return (jsonify(d))
except:
TB.print_exc()
return (jsonify({'check': False}))
# 课程资讯查询
@app.route('/api/classes', methods=['POST'])
def api_classes():
try:
data = request.get_json(force=True)
db = DB()
cs = db.Classes(data['dept'], data['teacher'], data['name'], data['number'], data['week'], data['sect'])
result = []
for c in cs:
result.append(c.ToDict())
return (jsonify(result))
except:
TB.print_exc()
return (jsonify({'check': False}))
# 選課
@app.route('/api/select', methods=['POST'])
def api_select():
try:
global Global
a = Global['MyAgent'](request)
data = request.get_json(force=True)
if a.Stage == '選課關閉':
return (jsonify({'check': False}))
else:
a.Select2(data['type'], data['number'], data['point'])
return (jsonify({'check': True}))
except:
TB.print_exc()
return (jsonify({'check': False}))
# FeedBack
@app.route('/api/feedback', methods=['POST'])
def api_feedback():
try:
data = request.get_json(force=True)
db = DB()
db.FeedBack(data['name'], data['email'], data['subject'], data['message'])
return (jsonify({'check': True}))
except:
TB.print_exc()
return (jsonify({'check': False}))
# Schedule
@app.route('/api/schedule', methods=['POST'])
def api_schedule():
try:
global Global
a = Global['MyAgent'](request)
s = Schedule(a)
return (jsonify({'check': True, 'html': s.ToHtml()}))
except:
TB.print_exc()
return (jsonify({'check': False}))
# Get FeedBack
@app.route('/api/get/feedbacks', methods=['GET'])
def api_get_feedbacks():
db = DB()
return (jsonify(db.GetFeedBacks()))
'''''''''''''''''''''''''''''''''''''''''''''
API End
'''''''''''''''''''''''''''''''''''''''''''''
@app.errorhandler(404)
def not_found(error):
return render_template('notfound.html')
@app.errorhandler(500)
def program_error(error):
return render_template('error.html')
if __name__ == '__main__':
ValidCodeModel = ValidCode()
'''
Start bug: 必須建立模型後隨便預測一次,否則出錯。
'''
images_f, numbers_f = LoadForwardData('static/img/validcode/1136.test.jpg')
print(ValidCodeModel.Forward(images_f))
'''
End bug
'''
app.run(port=80, host='0.0.0.0', debug=True)