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 test_delete(): """测试删除 todo""" username = uuid.uuid4().hex raw_password = '******' password = User.generate_password(raw_password) u = User(username=username, password=password) u.save() s = Session(user_id=u.id, expire_in='2099-12-31 00:00:00') s.save() content = uuid.uuid4().hex t = Todo(user_id=u.id, content=content) t.save() request_message = f'POST /delete HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n' \ f'Cookie: session_id={s.id}\r\n\r\nid={t.id}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(user_id=u.id, content=content, ensure_one=True) u.delete() s.delete() assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t is None r = route(request) assert b'302 FOUND' in bytes(r) assert b'/login' in bytes(r)
def new(request): """新建 todo 视图函数""" form = request.form logger(f'form: {form}') content = form.get('content') if content: todo = Todo(content=content) todo.save() return redirect('/index')
def test_delete(): """测试删除 todo""" content = uuid.uuid4().hex t = Todo(content=content) t.save() request_message = f'POST /delete HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\nid={t.id}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(content=content, ensure_one=True) assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t is None
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 index(request): """首页视图函数""" # 倒序排序,最近添加的 todo 排在前面 todo_list = Todo.all(sort=True, reverse=True) context = { 'todo_list': todo_list, } return render_template('todo/index.html', **context)
def new(request): """新建 todo 视图函数 Args: request: 请求对象 Returns: 重定向到首页 """ form = request.form logger(f'form: {form}') content = form.get('content') if content: user = current_user(request) if user: todo = Todo(content=content, user_id=user.id) todo.save() 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 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_new(): """测试新增 todo""" content = uuid.uuid4().hex request_message = f'POST /new HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\ncontent={content}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(content=content, ensure_one=True) t.delete() assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t.content == content
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 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
def test_edit(): """测试编辑 todo""" content = uuid.uuid4().hex t = Todo(content=content) t.save() edit_content = 'Edit: ' + content request_message = f'POST /edit HTTP/1.1\r\nHost: 127.0.0.1:8000\r\n\r\n' \ f'id={t.id}&content={edit_content}' request = Request(request_message) route, method = routes.get(request.path) r = route(request) t = Todo.find_by(content=edit_content, ensure_one=True) t.delete() assert b'302 FOUND' in bytes(r) assert b'/index' in bytes(r) assert t.content == edit_content
def delete(request): """删除 todo 视图函数 Args: request: 请求对象 Returns: 删除当前项,重定向到首页 """ form = request.form logger(f'form: {form}') id = int(form.get('id', -1)) if id != -1: user = current_user(request) if user: todo = Todo.find_by(id=id, user_id=user.id, ensure_one=True) if todo: todo.delete() return redirect('/index')
def add_todos(self): todo1 = Todo(name="Todo 1") todo2 = Todo(name="Todo 2") todo3 = Todo(name="Todo 3") todo1.save(), todo2.save(), todo3.save()