CZ_OpenSpice/handler/main.py

55 lines
2.5 KiB
Python
Raw Normal View History

2021-01-20 12:01:46 +00:00
import tornado.web
from pycket.session import SessionMixin
2021-01-22 12:09:14 +00:00
from .MongoDB import *
2021-01-20 12:01:46 +00:00
class AuthBaseHandler(tornado.web.RequestHandler,SessionMixin):
def get_current_user(self): #重写get_current_user()方法
return self.session.get('username',None) #session是一种会话状态跟数据库的session可能不一样
2021-01-22 12:09:14 +00:00
2021-01-20 12:01:46 +00:00
#添加装饰器,装饰需要验证的请求
2021-03-30 09:47:52 +00:00
class IndexHandler(AuthBaseHandler):
2021-01-20 12:01:46 +00:00
@tornado.web.authenticated #@tornado.web.authenticated装饰器包裹get方法时表示这个方法只有在用户合法时才会调用authenticated装饰器会调用get_current_user()方法获取current_user的值若值为False则重定向到登录url装饰器判断有没有登录如果没有则跳转到配置的路由下去但是要在app.py里面设置login_url
def get(self,*args,**kwargs):
2021-03-30 09:47:52 +00:00
# self.render('main/index_Zh.html')
2021-01-22 12:09:14 +00:00
username = self.get_current_user()
self.render('index.html',user=username)
2021-01-20 12:01:46 +00:00
2021-01-22 12:09:14 +00:00
class TestHandler(AuthBaseHandler):
@tornado.web.authenticated #@tornado.web.authenticated装饰器包裹get方法时表示这个方法只有在用户合法时才会调用authenticated装饰器会调用get_current_user()方法获取current_user的值若值为False则重定向到登录url装饰器判断有没有登录如果没有则跳转到配置的路由下去但是要在app.py里面设置login_url
def post(self,*args,**kwargs):
username = self.get_current_user()
message = self.get_argument('message')
2021-01-20 12:01:46 +00:00
2021-01-22 12:09:14 +00:00
print('TestHandler: '+username+' '+message)
Mongo.connect(DataBase='example',Collection=username)
Mongo.update(behavior=message,tags='test')
2021-01-20 12:01:46 +00:00
2021-01-22 12:09:14 +00:00
self.write("success")
2021-01-20 12:01:46 +00:00
2021-03-30 09:47:52 +00:00
#添加装饰器,装饰需要验证的请求
class Index_Zh_Handler(AuthBaseHandler):
@tornado.web.authenticated
def get(self,*args,**kwargs):
self.render('main/index_Zh.html')
#添加装饰器,装饰需要验证的请求
class Index_En_Handler(AuthBaseHandler):
@tornado.web.authenticated
def get(self,*args,**kwargs):
self.render('main/index_En.html')
#添加装饰器,装饰需要验证的请求
class Open_Index_Zh_Handler(tornado.web.RequestHandler):
def get(self,*args,**kwargs):
self.render('main/open_index_Zh.html')
#添加装饰器,装饰需要验证的请求
class Open_Index_En_Handler(tornado.web.RequestHandler):
def get(self,*args,**kwargs):
self.render('main/open_index_En.html')