CZ_OpenSpice/handler/auth.py

84 lines
3.2 KiB
Python
Raw Normal View History

2021-01-20 20:01:46 +08:00
from .main import AuthBaseHandler
2021-04-01 15:27:56 +08:00
from .account import authenticate, add_user, check_register
2021-01-22 20:09:14 +08:00
from .MongoDB import *
2021-01-20 20:01:46 +08:00
2021-04-01 13:26:36 +08:00
DEBUG = False
2021-01-20 20:01:46 +08:00
class LoginHandler(AuthBaseHandler):
def get(self,*args,**kwargs):
2021-01-22 20:09:14 +08:00
self.render('auth/login.html')
2021-01-20 20:01:46 +08:00
def post(self,*args,**kwargs):
username = self.get_argument('username',None)
password = self.get_argument('password',None)
# check the password
passed = authenticate(username,password)
if passed:
# 保存cookie信息到redis数据库
self.session.set('username',username) #将前面设置的cookie设置为username保存用户登录信息
2021-04-01 13:26:36 +08:00
if DEBUG:
print(self.session.get('username')+' login success !!!')
2021-01-20 20:01:46 +08:00
next_url = self.get_argument('next', '') # 获取之前页面的路由
if next_url:
2021-01-22 20:09:14 +08:00
Mongo.connect(DataBase='example',Collection=username)
Mongo.update(behavior='login',tags='auth')
2021-01-20 20:01:46 +08:00
self.redirect(next_url) #跳转主页路由
else:
2021-01-22 20:09:14 +08:00
Mongo.connect(DataBase='example',Collection=username)
Mongo.update(behavior='login',tags='auth')
2021-03-30 17:47:52 +08:00
self.redirect('/index')
2021-01-20 20:01:46 +08:00
else:
self.write({'msg':'login fail'}) #不通过,有问题
class LogoutHandler(AuthBaseHandler):
def get(self, *args, **kwargs):
2021-01-22 20:09:14 +08:00
Mongo.connect(DataBase='example',Collection=self.get_current_user())
Mongo.update(behavior='logout',tags='auth')
2021-01-20 20:01:46 +08:00
#self.session.set('username','') #将用户的cookie清除
self.session.delete('username')
self.redirect('/login')
# def post(self,*args,**kwargs):
# self.session.delete('username')
# self.redirect('/login')
class RegisterHandler(AuthBaseHandler):
def get(self, *args, **kwargs):
2021-04-01 13:26:36 +08:00
if DEBUG:
print('register')
2021-01-22 20:09:14 +08:00
self.render('auth/register.html')
2021-01-20 20:01:46 +08:00
def post(self, *args, **kwargs):
2021-04-01 13:26:36 +08:00
if DEBUG:
print('registerpost')
2021-01-20 20:01:46 +08:00
username = self.get_argument('username','')
2021-01-26 20:47:02 +08:00
email = self.get_argument('email','')
2021-01-20 20:01:46 +08:00
password1 = self.get_argument('password1','')
password2 = self.get_argument('password2','')
if username and password1 and (password1 == password2):
2021-04-01 15:27:56 +08:00
flag_checck = check_register(username,password1,email)
if flag_checck == 0:
success = add_user(username,password1,email)
if success:
Mongo.connect(DataBase='example',Collection=username)
Mongo.update(behavior='register',tags='auth')
self.redirect('/login')
else:
self.write({'msg':'register fail'})
elif flag_checck == 1:
self.write({'msg':'the username is not avaliable!'})
elif flag_checck == 2:
self.write({'msg':'the email is not avaliable!'})
elif flag_checck == 3:
self.write({'msg':'the password is too shor!'})
2021-01-20 20:01:46 +08:00
else:
2021-04-01 13:26:36 +08:00
if DEBUG:
print('register again')
2021-01-22 20:09:14 +08:00
self.render('auth/register.html')