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 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 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 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 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_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')