-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·311 lines (260 loc) · 11.9 KB
/
run.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding:utf-8 -*-
# all the imports
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
import sys,os
reload(sys)
sys.setdefaultencoding("utf-8")
import json
import datetime
import dump_csv
# configuration
DATABASE = 'data/zjucompass.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
g.db.close()
@app.route('/')
@app.route('/teachers')
def show_entries():
cur = g.db.execute('select * from teacher_info where department="计算机科学与技术学院"')
entries = [dict(teacher_id=row[0], name=row[1], department=row[2], title=row[3], phone=row[4], email=row[5], building=row[6], room=row[7], field=row[8]) for row in cur.fetchall()]
if not session.get('logged_in'):
return redirect(url_for('login'))
return render_template('show_entries.html', type="teacher", entries=entries)
@app.route('/feedback')
def show_feedback():
cur = g.db.execute('select * from feedback order by timestamp desc')
entries = [dict(timestamp=row[0], feedback=row[1], email=row[2], phone=row[3]) for row in cur.fetchall()]
print len(entries)
if not session.get('logged_in'):
return redirect(url_for('login'))
return render_template('show_feedback.html', type="feedback", entries=entries)
@app.route('/correct')
def show_correct():
cur = g.db.execute('select * from correct_teacher order by timestamp desc')
entries = [dict(teacher_id=row[1], timestamp=row[2], name=row[3], department=row[4], title=row[5], phone=row[6], email=row[7], building=row[8], room=row[9], field=row[10]) for row in cur.fetchall()]
print len(entries)
print "Hello World"
for entry in entries:
print entry
if not session.get('logged_in'):
return redirect(url_for('login'))
return render_template('show_correct.html', type="correct", entries=entries)
@app.route('/matters')
def show_matters():
cur = g.db.execute('select matter_id,matter_name,matter_meterial,matter_description,matter_time,matter_consult_phone,matter_address,matter_detail_url from matter_info;')
entries = [dict(matter_id=row[0], matter_name=row[1], matter_meterial=row[2], matter_description=row[3], matter_time=row[4], matter_consult_phone=row[5], matter_address=row[6], matter_detail_url=row[7]) for row in cur.fetchall()]
if not session.get('logged_in'):
return redirect(url_for('login'))
return render_template('show_matters.html', type="matter", entries=entries)
@app.route('/api/download_teacher',methods=['GET'])
def download_teacher():
dump_csv.dump_info("static/teachers.csv","teacher")
return redirect("/static/teachers.csv")
@app.route('/api/download_matter',methods=['GET'])
def download_matter():
dump_csv.dump_info("static/matters.csv","teacher")
return redirect("/static/matters.csv")
@app.route('/api/feedback',methods=['POST'])
def get_feedback():
jsonStr = request.data
print type(jsonStr)
print jsonStr
print jsonStr.decode("utf-8")
data = json.loads(jsonStr)
feedback = data['feedback']
email = data['email']
phone = data['phone']
print feedback
print email
print phone
currTime = datetime.datetime.now()
timeStr = currTime.strftime("%Y-%m-%d %H:%M:%S")
print timeStr
sqlcmd = "insert into feedback(timestamp, feedback, email, phone) values('%s','%s','%s','%s')"%(timeStr, feedback, email, phone)
g.db.execute(sqlcmd)
g.db.commit()
return '{"code":200}'
@app.route('/api/correct_teacher',methods=['POST'])
def get_correct_teacher():
jsonStr = request.data
data = json.loads(jsonStr)
print data
currTime = datetime.datetime.now()
timeStr = currTime.strftime("%Y-%m-%d %H:%M:%S")
sqlcmd = "insert into correct_teacher(timestamp,teacher_id,name,email,phone,title,room,field,department) values('%s','%s','%s','%s','%s','%s','%s','%s','%s')"%(timeStr,data['id'],data['name'],data['email'],data['phone'],data['title'],data['location'],data['research'],data['dept'])
g.db.execute(sqlcmd)
g.db.commit()
return '{"code":200}'
@app.route('/api/check_update',methods=['POST','GET'])
def get_version():
return "1.0.0"
@app.route('/add', methods=['GET','POST'])
def add_entry():
if request.method == 'GET':
if not session.get('logged_in'):
return redirect(url_for('login'))
return render_template('add_entry.html')
else:
name = request.form['name']
department = request.form['department']
title = request.form['title']
phone = request.form['phone']
email = request.form['email']
building = request.form['building']
room = request.form['room']
field = request.form['field']
sqlcmd = "insert into teacher_info (name, department, title, phone, email, building, room, field) values('%s','%s','%s','%s','%s','%s','%s','%s')" % (name,department,title,phone,email,building,room,field)
g.db.execute(sqlcmd)
g.db.commit()
flash('The new entry was added')
return redirect(url_for('show_entries'))
@app.route('/add_matter', methods=['GET','POST'])
def add_matter():
if request.method == 'GET':
if not session.get('logged_in'):
return redirect(url_for('login'))
return render_template('add_matter.html')
else:
name = request.form['name']
m_id = request.form['id']
meterial = request.form['meterial']
description = request.form['description']
time = request.form['time']
address = request.form['address']
consult_phone = request.form['consult_phone']
detail_url = request.form['detail_url']
sqlcmd = "insert into matter_info (matter_id, matter_name, matter_meterial, matter_description, matter_time, matter_address, matter_consult_phone, matter_detail_url) values('%s','%s','%s','%s','%s','%s','%s','%s')" % (m_id, name, meterial, description, time, address, consult_phone, detail_url)
g.db.execute(sqlcmd)
g.db.commit()
flash('The new matter was added')
return redirect(url_for('show_matters'))
@app.route('/del',methods=['GET'])
def del_entry():
if not session.get('logged_in'):
return redirect(url_for('login'))
teacher_id = request.args.get('id','-1')
if teacher_id == "-1":
flash("Bad teacher_id!")
return redirect(url_for('show_entries'))
sqlcmd = "delete from teacher_info where teacher_id='%s'" % (teacher_id)
g.db.execute(sqlcmd)
g.db.commit()
flash('This entry was successfully deleted')
return redirect(url_for('show_entries'))
@app.route('/del_matter',methods=['GET'])
def del_matter():
if not session.get('logged_in'):
return redirect(url_for('login'))
matter_id = request.args.get('matter_id','-1')
if matter_id == "-1":
flash("Bad teacher_id!")
return redirect(url_for('show_matters'))
sqlcmd = "delete from matter_info where matter_id='%s'" % (matter_id)
g.db.execute(sqlcmd)
g.db.commit()
flash('This matter was successfully deleted')
return redirect(url_for('show_matters'))
@app.route('/edit', methods=['GET','POST'])
def edit_entry():
if request.method == 'POST':
teacher_id = request.form['teacher_id']
name = request.form['name']
department = request.form['department']
title = request.form['title']
phone = request.form['phone']
email = request.form['email']
building = request.form['building']
room = request.form['room']
field = request.form['field']
sqlcmd = "update teacher_info set name='%s',department='%s',title='%s',phone='%s',email='%s',building='%s',room='%s',field='%s' where teacher_id='%s'" % (name,department,title,phone,email,building,room,field,teacher_id)
g.db.execute(sqlcmd)
g.db.commit()
flash('This entry was successfully updated')
return redirect(url_for('show_entries'))
else:
if not session.get('logged_in'):
return redirect(url_for('login'))
teacher_id = request.args.get('id','-1')
if teacher_id == "-1":
flash("Bad teacher_id!")
return redirect(url_for('show_entries'))
cur = g.db.execute('select * from teacher_info where teacher_id = "%s"'%teacher_id)
#print cur.fetchall()[0]
results = cur.fetchall()
if not results:
flash("This teacher_id does not exist!")
return redirect(url_for('show_entries'))
row = results[0]
entry = dict(teacher_id=row[0], name=row[1], department=row[2], title=row[3], phone=row[4], email=row[5], building=row[6], room=row[7], field=row[8])
return render_template('edit_entry.html', entry=entry)
#entrieds = [dict(teacher_id=row[0], name=row[1], department=row[2], title=row[3], phone=row[4], email=row[5], building=row[6], room=row[7], field=row[8]) for row in cur.fetchall()]
@app.route('/edit_matter', methods=['GET','POST'])
def edit_matter():
if request.method == 'POST':
matter_id = request.form['id']
matter_name = request.form['name']
matter_meterial = request.form['meterial']
matter_description = request.form['description']
matter_time = request.form['time']
matter_address = request.form['address']
matter_consult_phone = request.form['consult_phone']
matter_detail_url = request.form['detail_url']
sqlcmd = "update matter_info set matter_name='%s',matter_meterial='%s',matter_description='%s',matter_time='%s',matter_address='%s',matter_consult_phone='%s',matter_detail_url='%s' where matter_id='%s'" % (matter_name,matter_meterial,matter_description,matter_time,matter_address,matter_consult_phone,matter_detail_url,matter_id)
print sqlcmd
g.db.execute(sqlcmd)
g.db.commit()
flash('This matter was successfully updated')
return redirect(url_for('show_matters'))
else:
if not session.get('logged_in'):
return redirect(url_for('login'))
matter_id = request.args.get('id','-1')
if matter_id == "-1":
flash("Bad teacher_id!")
return redirect(url_for('show_matters'))
cur = g.db.execute('select matter_name,matter_id,matter_meterial,matter_description,matter_time,matter_address,matter_consult_phone,matter_detail_url from matter_info where matter_id = "%s"'%matter_id)
#print cur.fetchall()[0]
results = cur.fetchall()
if not results:
flash("This matter_id does not exist!")
return redirect(url_for('show_matters'))
row = results[0]
entry = dict(matter_name=row[0], matter_id=row[1], matter_meterial=row[2], matter_description=row[3], matter_time=row[4], matter_address=row[5], matter_consult_phone=row[6], matter_detail_url=row[7])
return render_template('edit_matter.html', entry=entry)
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == "POST":
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_entries'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_entries'))
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')