def register(request): """注册 Args: request: 请求对象 Returns: GET 请求: 返回注册页面 POST 请求: 注册成功将重定向到登录页 注册失败返回失败原因提示短语 """ if request.method == 'POST': # 获取表单中的用户名和密码 form = request.form logger(f'form: {form}') username = form.get('username') raw_password = form.get('password') # 验证用户名和密码是否合法 if not username or not raw_password: return '无效的用户名或密码'.encode('utf-8') user = User.find_by(username=username, ensure_one=True) if user: return '用户名已存在'.encode('utf-8') # 对密码进行散列计算,创建并保存用户信息 password = User.generate_password(raw_password) user = User(username=username, password=password) user.save() # 注册成功后重定向到登录页面 return redirect('/login') return render_template('auth/register.html')
def edit(request): """编辑 todo 视图函数""" # 处理 POST 请求 if request.method == 'POST': form = request.form logger(f'form: {form}') id = int(form.get('id', -1)) content = form.get('content') if id != -1 and content: todo = Todo.get(id=id) if todo: todo.content = content todo.save() return redirect('/index') # 处理 GET 请求 args = request.args logger(f'args: {args}') id = int(args.get('id', -1)) if id == -1: return redirect('/index') todo = Todo.get(id=id) if not todo: return redirect('/index') context = { 'todo': todo, } return render_template('todo/edit.html', **context)
def index(request): """首页视图函数""" # 倒序排序,最近添加的 todo 排在前面 todo_list = Todo.all(sort=True, reverse=True) context = { 'todo_list': todo_list, } return render_template('todo/index.html', **context)
def index(request): """首页视图函数 Args: request: 请求对象 Returns: 返回首页(即列表页) """ user = current_user(request) todo_list = Todo.find_by(user_id=user.id, sort=True, reverse=True) context = { 'todo_list': todo_list, } return render_template('todo/index.html', **context)
def edit(request): """编辑 todo 视图函数 Args: request: 请求对象 Returns: GET 请求: 返回编辑页面 POST 请求: 更新当前编辑项,重定向到首页 """ if request.method == 'POST': form = request.form logger(f'form: {form}') id = int(form.get('id', -1)) content = form.get('content') if id != -1 and content: user = current_user(request) if user: todo = Todo.find_by(id=id, user_id=user.id, ensure_one=True) if todo: todo.content = content todo.save() return redirect('/index') args = request.args logger(f'args: {args}') id = int(args.get('id', -1)) if id == -1: return redirect('/index') user = current_user(request) if not user: return redirect('/index') todo = Todo.find_by(id=id, user_id=user.id, ensure_one=True) if not todo: return redirect('/index') context = { 'todo': todo, } return render_template('todo/edit.html', **context)
def login(request): """登录 Args: request: 请求对象 Returns: GET 请求: 返回登录页面 POST 请求: 登录成功将重定向到首页 登录失败返回失败原因提示短语 """ # 如果用户已经登录,直接重定向到首页 if current_user(request): return redirect('/index') if request.method == 'POST': message = '用户名或密码不正确'.encode('utf-8') # 获取表单中的用户名和密码 form = request.form logger(f'form: {form}') username = form.get('username') raw_password = form.get('password') # 验证用户名和密码是否正确 if not username or not raw_password: return message user = User.find_by(username=username, ensure_one=True) if not user: return message password = user.password if not User.validate_password(raw_password, password): return message # 创建 Session 并将 session_id 写入 Cookie 实现登录 session = Session(user_id=user.id) session.save() cookies = { 'session_id': session.id, } return redirect('/index', cookies=cookies) return render_template('auth/login.html')
def page_not_found(): """处理 400 异常""" body = render_template('error/404.html') return Response(body, status=400)
def internal_server_error(): """处理 500 异常""" body = render_template('error/500.html') return Response(body, status=500)