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 delete(request): """删除 todo 视图函数""" form = request.form logger(f'form: {form}') id = int(form.get('id', -1)) if id != -1: todo = Todo.get(id=id) if todo: todo.delete() return redirect('/index')
def test_todo(): """测试 Todo 模型类""" content = uuid.uuid4().hex t = Todo(content=content) t.save() ts = Todo.all() find_todo = Todo.get(t.id) t.delete() assert t.content == content assert t.id in [t.id for t in ts] assert t.id == find_todo.id
def test_todo(): """测试 Todo 模型类""" username = uuid.uuid4().hex raw_password = '******' password = User.generate_password(raw_password) u = User(username=username, password=password) u.save() content = uuid.uuid4().hex t = Todo(user_id=u.id, content=content) t.save() ts = Todo.all() find_todo = Todo.get(t.id) u.delete() t.delete() assert t.user_id == u.id assert t.content == content assert t.id in [t.id for t in ts] assert t.id == find_todo.id