def messages_add(request): log('本次请求的 method', request.method) data = request.form() if len(data) > 0: Message.new(data) body = template('message.html', messages=Message.all()) return http_response(body)
def admin_users(request): """ 增加一个路由 /admin/users 只有 id 为 1 的用户可以访问这个页面, 其他用户访问会定向到 /login 这个页面显示了所有的用户 包括 id username password """ body = template('admin_users.html', user_list=User.all()) return http_response(body)
def messages_index(request): log('本次请求的 method', request.method) data = request.query if len(data) > 0: Message.new(data) # 处理msg便于输出 body = template('message.html', messages=Message.all()) return http_response(body)
def user_profile(request): us = User.find_by(username=current_user(request).username) body = template( 'profile.html', id=str(getattr(us, 'id')), username=current_user(request).username, note=current_user(request).username ) return http_response(body)
def user_register(request, all_user=''): if request.method == 'POST': form = request.form() u = User.new(form) if u.validate_register(): result = '注册成功<br>' all_user = User.all() else: result = '用户名或者密码长度必须大于2' else: result = '' body = template('register.html', result=result, all_user=all_user) return http_response(body)
def user_login(request): # header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n' # 仅添加content-type字段 session = None if request.method == 'POST': form = request.form() username = form.get('username') password = form.get('password') if User.validate_login(username, password): u = User.find_by(username=username) # 向headers中添加set-cookie字段 # 通过设置一个随机字符串生成session_id session_id = random_str() form = dict( session_id=session_id, user_id=u.id, ) s = Session.new(form) s.save() session = {'Set-Cookie': 'session_id={}'.format( session_id )} result = '登录成功' else: result = '用户名或者密码错误' u = User.guest() else: result = '' u = current_user(request) body = template( 'login.html', username=u.username, result=result ) return http_response(body, session)
def todo_edit(request): todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) body = template('todo_edit.html', todo_id=str(t.id), todo_title=t.title) return http_response(body)
def todo_index(request): body = template('todo_index.html', todos=Todo.find_all(user_id=current_user(request).id)) return http_response(body)