主要就是可以发布作业,查看提交作业的系统,是后台的程序
#!/usr/bin/env python
-*- coding: utf-8 -*-
import os.path
import sys
import time
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
reload(sys)
sys.setdefaultencoding('utf-8')
DATA = os.path.join(os.path.dirname(__file__), "static", "data.txt")
NOW = time.strftime('%Y-%m-%d %X', time.localtime(time.time()))
def timecmp(a, b):
return (time.strptime(a, '%Y-%m-%d %X') > time.strptime(b, '%Y-%m-%d %X'))
with open(DATA, 'r') as fr:
data = [line.strip().split(';') for line in fr.readlines()]
class IndexHandler(tornado.web.RequestHandler):
"""docstring for IndexHandler"""
def get(self):
search = self.get_argument('seclass', "")
global data
data_filter = [ele for ele in data if ele[1]==search]
#compare current time
data1 = [i for i in data_filter if not timecmp(NOW, i[3])]
data2 = [i for i in data_filter if timecmp(NOW, i[3])]
#sort
data1=sorted(data1, key=lambda x: time.strptime(x[3], '%Y-%m-%d %X'))
data2=sorted(data2, key=lambda x: time.strptime(x[3], '%Y-%m-%d %X'), reverse=True)
self.render('yourhomework.html', data1=data1, data2=data2, search=search)
def post(self):
cname = self.get_argument('classname', "")
cnumber = self.get_argument('classnumber', "")
detail = self.get_argument('hwdetail', "")
year = self.get_argument('year', "")
month = self.get_argument('month', "")
day = self.get_argument('day', "")
hour = self.get_argument('hour', "")
if (cname=="" or cnumber=="" or detail=="" or year=="" or month=="" or day=="" or hour==""):
self.redirect('/yourhomework.html')
else:
new_hw = [cname, cnumber, detail, year+"-"+month+"-"+day+" "+hour+":00"]
flag = True
global data
for ele in data:
if (ele==new_hw):
flag = False
if (flag):
data.append(new_hw)
data_filter = [ele for ele in data if ele[1]==cnumber]
data1 = [i for i in data_filter if not timecmp(NOW, i[3])]
data2 = [i for i in data_filter if timecmp(NOW, i[3])]
data1=sorted(data1, key=lambda x: time.strptime(x[3], '%Y-%m-%d %X'))
data2=sorted(data2, key=lambda x: time.strptime(x[3], '%Y-%m-%d %X'), reverse=True)
self.render('yourhomework.html', data1=data1, data2=data2, search=cnumber)
if name == '__main__':
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[(r"/", IndexHandler), (r"/yourhomework.html", IndexHandler)],
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()