-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
276 lines (236 loc) · 8.65 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import datetime
import re
from flask import Flask, render_template, session, redirect, request
from pymysql import MySQLError
from data_format import Problem, Submission, Table, ProblemList, TableList
from sql_factory import SQLFactory
app = Flask(__name__,
static_folder='static',
static_url_path='/'
)
app.config['SECRET_KEY'] = '\x0fW\xdc\x13\xeel(\xe9\xb5\xbaa\xa4\xc0\xb5\xaaK^\xd1Y\x81)#\x92T'
app.config['PERMANENT_SESSION_LIFETIME'] = datetime.timedelta(days=7)
@app.route('/')
def index():
if session.get("username") is not None:
factory = SQLFactory()
cursor = factory.get_root_cursor()
cursor.execute("select set_id,test_id from manage.test_table limit 1")
result = cursor.fetchone()
return redirect(f"/question/{result[0]}/{result[1]}")
return redirect('/login')
@app.route('/login')
def login():
failed = request.args.get("failed", 'false') == 'true'
return render_template("login.html", failed=failed)
@app.route('/login/action', methods=["POST"])
def login_action():
session.clear()
factory = SQLFactory()
username = request.form.get('username')
password = request.form.get('password')
if factory.user_login(username, password):
session['username'] = username
session['password'] = password
return redirect("/")
else:
return redirect("/login?failed=true")
@app.route('/user/execute', methods=["POST"])
def run_sql():
if 'username' not in session:
return {
"result": "failure",
"errno": -1,
"errmsg": "Access denied",
}, 401
query = request.form.get("sql", "")
page_size = request.form.get("page-size", "")
page_size = int(page_size) if page_size.isdigit() else 15
factory = SQLFactory()
if not factory.user_login(session['username'], session['password']):
factory.closeAll()
return {
"result": "failure",
"errno": -2,
"errmsg": "Password changed",
}, 401
try:
cursor = factory.get_user_cursor()
rowcount = cursor.execute(query)
return {
"result": "success",
"rowcount": rowcount,
"data": cursor.fetchmany(min(rowcount, page_size)),
"description": cursor.description,
}
except MySQLError as e:
errno, errmsg = e.args
return {
"result": "failure",
"errno": errno,
"errmsg": errmsg,
}
finally:
factory.closeAll()
@app.route('/user/verify', methods=["POST"])
def verify():
if 'username' not in session:
return {
"result": "failure",
"errno": -1,
"errmsg": "Access denied",
}, 401
test = request.form.get("test", "test0_0")
username = session.get("username")
factory = SQLFactory()
try:
cursor = factory.get_root_cursor()
message = check_same_table(username, test, cursor)
cursor.execute("select result from manage.record where sid=%s and test_id=%s", (username, test))
if cursor.rowcount == 0:
cursor.execute("insert into manage.record values (%s,now(),%s,%s)", (username, test, message))
else:
cursor.execute("update manage.record set result=%s,submission_time=now() where sid=%s and test_id=%s",
(message, username, test))
return {
"result": "success" if message == "success" else "failure",
"message": message,
}
except MySQLError as e:
errno, errmsg = e.args
return {
"result": "failure",
"errno": errno,
"errmsg": errmsg,
}
finally:
factory.closeAll()
@app.route('/search/test', methods=["POST"])
def search_t():
ask = request.form.get("the_search_test", "")
factory = SQLFactory()
try:
cursor = factory.get_root_cursor()
answer = cursor.execute("select set_id,test_id from manage.test_table where test_id=%s", (ask,))
if answer:
result = cursor.fetchone()
return {
"resp": "success",
"url": "/question/{}/{}".format(result[0], result[1]),
"info": "成功搜索到题目{}".format(ask),
}
else:
return {
"resp": "failure",
"url": "",
"info": "失败搜索到题目{}".format(ask),
}
except MySQLError as e:
errno, errmsg = e.args
return {
"resp": "failure",
"url": "",
"info": "失败搜索到题目{}".format(ask),
}
finally:
factory.closeAll()
def check_same_table(user_db, test, cursor):
cursor.execute(f"select * from answer.{test}")
std_rownum = cursor.rowcount
std_description = set([desc[0] for desc in cursor.description])
std_description_list = [desc[0] for desc in cursor.description]
std_rows = set(cursor.fetchall())
cursor.execute(f"select TABLE_NAME from INFORMATION_SCHEMA.TABLES "
f"where TABLE_SCHEMA='{user_db}' and TABLE_NAME='{test}'")
if cursor.rowcount == 0:
return f"用户表 {test} 不存在。"
cursor.execute(f"select * from {user_db}.{test}")
user_rownum = cursor.rowcount
user_description = cursor.description
user_description = set([desc[0] for desc in user_description])
leak = std_description - user_description
more = user_description - std_description
if len(leak) or len(more):
return f"缺少表头 {','.join(leak)}, 多余表头 {','.join(more)}。"
if std_rownum < user_rownum:
return f"比标准答案多 {user_rownum - std_rownum} 行。"
if std_rownum > user_rownum:
return f"比标准答案少 {std_rownum - user_rownum} 行。"
cursor.execute(f"select {','.join(std_description_list)} from {user_db}.{test}")
err_rownum = 0
for row in cursor.fetchall():
if row not in std_rows:
err_rownum += 1
if err_rownum > 0:
return f"与标准答案相比错 {err_rownum} 行。"
return "success"
@app.route('/logout')
def logout():
session.clear()
return redirect('/')
@app.route('/question/<set_id>/<test_id>')
def test(set_id, test_id):
if session.get("username") is not None:
factory = SQLFactory()
cursor = factory.get_root_cursor()
problem = Problem(test_id, cursor)
submit = Submission(session.get("username"), test_id, cursor)
table = Table("username", "pub.sc", cursor)
path = {"set": set_id, "test": test_id}
problem_list = ProblemList(cursor)
factory.user_login(session['username'], session['password'])
user_cursor = factory.get_user_cursor()
table_list = TableList(user_cursor)
return render_template("index.html",
problem=problem,
problem_list=problem_list,
submit=submit,
table=table,
path=path,
table_list=table_list,
)
return redirect('/login')
@app.route('/profile')
def profile():
return render_template("profile.html")
@app.route('/user/find_table', methods=["POST"])
def find_table():
if 'username' not in session:
return {
"result": "failure",
"errno": -1,
"errmsg": "Access denied",
}, 401
regex = re.compile(r"^[a-zA-Z_.]+$")
name = request.form.get('name')
name = regex.match(name).group()
page_size = request.form.get("page-size", "")
page_size = int(page_size) if page_size.isdigit() else 15
factory = SQLFactory()
if not factory.user_login(session['username'], session['password']):
factory.closeAll()
return {
"result": "failure",
"errno": -2,
"errmsg": "Password changed",
}, 401
try:
cursor = factory.get_user_cursor()
rowcount = cursor.execute("select * from " + name)
return {
"result": "success",
"rowcount": rowcount,
"data": cursor.fetchmany(min(rowcount, page_size)),
"description": cursor.description,
}
except MySQLError as e:
errno, errmsg = e.args
return {
"result": "failure",
"errno": errno,
"errmsg": errmsg,
}
finally:
factory.closeAll()
if __name__ == '__main__':
app.run(debug=True)