def route_login(request): """ 登录页面的路由函数 """ log('login, cookies', request.cookies) if request.method == 'POST': form = request.form() u = User(form) if u.validate_login(): session_id = random_str() u = User.find_by(username=u.username) s = Session.new(dict( session_id=session_id, user_id=u.id, )) log('session', s) headers = {'Set-Cookie': 'sid={}'.format(session_id)} # 登录后定向到 / return redirect('/', headers) # 显示登录页面 body = template('login.html') return http_response(body)
def route_login(request): """ 登录页面的路由函数 """ log('login, cookies', request.cookies) if request.method == 'POST': form = request.form() u = User(form) if u.validate_login(): session_id = random_str() u = User.find_by(username=u.username) s = Session.new(dict( session_id=session_id, user_id=u.id, )) s.save() log('session', s) headers = {'Set-Cookie': 'sid={}'.format(session_id)} # 登录后定向到 / return redirect('/', headers) else: username = '******' result = '用户名或密码不正确' body = template('login.html', username=username, result=result) return http_response(body) # 显示登录页面 u = current_user(request) if u is None: username = '******' result = '请登录' else: username = u.username result = '登录成功' body = template('login.html', username=username, result=result) return http_response(body)
def test(): connection = pymysql.connect(host='localhost', user='******', password=database_password, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) with connection.cursor() as cursor: cursor.execute('DROP DATABASE IF EXISTS `test_database`') cursor.execute('CREATE DATABASE `test_database` CHARACTER SET utf8mb4') cursor.execute('USE `test_database`') cursor.execute(User.sql_create) cursor.execute(Session.sql_create) cursor.execute(Todo.sql_create) cursor.execute(Comment.sql_create) cursor.execute(Weibo.sql_create) connection.commit() connection.close() form = dict( username='******', password='******', ) User.register_user(form) u, result = User.login_user(form) assert u is not None, result form = dict( username='******', password='******', ) User.register_user(form) session_id = random_string() form = dict( session_id=session_id, user_id=u.id, ) Session.new(form) s: Session = Session.one(session_id=session_id) assert s.session_id == session_id form = dict( title='test todo', user_id=u.id, ) t = Todo.add(form, u.id) assert t.title == 'test todo' form = dict( content='111', user_id=u.id, ) w = Weibo.add(form, u.id) assert w.content == '111' form = dict( content='2222', user_id=u.id, ) w2 = Weibo.add(form, u.id) assert w2.content == '2222' form = dict( content='123333', user_id=u.id, weibo_id=w.id, ) c = Comment.new(form) assert c.content == '123333'
password=password) except FailedLoginAttemptsExceeded, e: show_captcha = True error_message = 'You have reached the maximum number of sign in attempts.' except AccountNotExist, e: error_message = 'The account does not exist. <a href="/signup">Sign up now for free</a>' except AccountExistInvalidPassword, e: error_message = 'The username or password you entered is incorrect.' if valid_user: try: # step 1 create session new_session = Session.new(account_id=valid_user.account_id) # step 2 save session to secure cookie self.set_secure_cookie(name='session_id', value=new_session.session_id, expires_days=2) self.redirect('/dashboard') except Exception, e: self.redirect('/') else: if not error_invalid_email and not error_message: error_message = 'The username or password you entered is incorrect.' # check if user has failed logins recaptcha_public = self.settings['config']['recaptcha'][