def edit(request): """ todo edit 的路由函数 """ headers = { 'Content-Type': 'text/html', } uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') # 得到当前编辑的 todo 的 id todo_id = int(request.query.get('id', -1)) # if todo_id < 1: # 暂时不考虑边界情况,以后再完善 # return error(404) t = Todo.find_by(id=todo_id) if t.user_id != u.id: # t这条记录属于t.user_id, 看看当前登录的u是否是{t这条记录的权限所有者} return redirect('/login') # 替换模板文件中的标记字符串 body = template('todo_edit.html') body = body.replace('{{todo_id}}', str(t.id)) body = body.replace('{{todo_title}}', str(t.title)) # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中 header = response_with_headers(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def update(request, u): """ 用于增加新 todo 的路由函数 """ # uname = current_user(request) # u = User.find_by(username=uname) # if u is None: # return redirect('/login') if request.method == 'POST': # 修改并且保存 todo form = request.form() #print('debug update', form) todo_id = form.get('id', -1) # 检查 id 是不是数字 if todo_id == -1 or not todo_id.isdigit(): return redirect('/todo') todo_id = int(todo_id) t = Todo.find_by(id=todo_id) # 检验用户权限 if t is None or t.user_id != u.id: return redirect('/todo') t.title = form.get('title', t.title) t.update_time = int(time.time()) t.save() # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo')
def edit(request): """ todo edit 的路由函数 """ headers = { 'Content-Type': 'text/html', } uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') # 5.1 补全 edit 和 update 两个路由函数 # 加上权限控制功能, 如果权限不足, 重定向到todo if u.authority == 0: return redirect('/todo') # 得到当前编辑的 todo 的 id todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) if t.user_id != u.id: return redirect('/login') # if todo_id < 1: # return error(404) # 替换模板文件中的标记字符串 body = template('todo_edit.html') body = body.replace('{{todo_id}}', str(t.id)) body = body.replace('{{todo_title}}', str(t.title)) # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中 header = response_with_headers(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def update(request): """ 用于增加新 todo 的路由函数 """ uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') # 5.1 补全 edit 和 update 两个路由函数 # 加上权限控制功能, 如果权限不足, 重定向到todo if u.authority == 0: return redirect('/todo') if request.method == 'POST': # 修改并且保存 todo form = request.form() print('debug update', form) todo_id = int(form.get('id', -1)) t = Todo.find_by(id=todo_id) t.title = form.get('title', t.title) # 5.4 在todo 界面上显示创建和修改事件 t.updated_time = int(time.time()) t.save() # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo')
def update(request): """ 用于增加新 todo 的路由函数 """ # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') if request.method == 'POST': # 修改并且保存 todo # 用form函数处理请求 # form的用法 """ :param: 'username=gua&password=123' :return: {'username': '******', 'password': '******'} """ form = request.form() print('debug update', form) # 拿到修改的数据的id,有可能用户修改数据的时候也把id改了 todo_id = int(form.get('id', -1)) # t = Todo.find_by(id=todo_id) t.title = form.get('title', t.title) t.save() # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo')
def edit(request): """ _todo edit 的路由函数 """ headers = { 'Content-Type': 'text/html', } uname = current_user(request) u = User.find_by(username=uname) # 得到当前编辑的 _todo 的 id todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) # if todo_id < 1: # return error(404) if u is None: return redirect('/login') if u.id != t.user_id: redirect('/todo') # 替换 模板文件中的标记字符串 body = template('todo_edit.html') body = body.replace('{{todo_id}}', str(t.id)) body = body.replace('{{todo_title}}', str(t.title)) # 把下面 3 行可以修改为一个函数,还把 headers 放入到函数中 header = response_with_headers(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def delete_todo(request): uname = current_user(request) u = User.find_by(username=uname) # 得到当前编辑的 todo 的 id todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) if t.user_id != u.id: return redirect('/login') if t is not None: t.remove() return redirect('/todo')
def todo__del(request): uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) if t.user_id != u.id: return redirect('/login') if t is not None: t.remove() return redirect('/todo')
def update(request): uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') if request.method == 'POST': form = request.form() print('debug update',form) todo_id = int(form.get('id',-1)) t = Todo.find_by(id=todo_id) t.title=form.get('title',t.title) t.save() return redirect('/todo')
def route_admin_users_update(request): uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') # 得到当前编辑的 _todo 的 id todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) if t.user_id != u.id: return redirect('/login') if t is not None: t.remove() return redirect('/todo')
def edit(request): uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') log('这是reqeust', request.query) todo_id = int(request.query.get('id', -1)) log('todo_id', todo_id) t = Todo.find_by(id=todo_id) log('** 这是t:', t) if t.user_id != u.id: return redirect('/login') body = template('jinja_test_edit.html', user=t) return response_http(body)
def update(request): """ 用于增加新的todo的路由函数 """ if request.method == 'POST': # 修改并且保存todo form = request.form() todo_id = int(form.get('id', -1)) t = Todo.find_by(id=todo_id) t.title = form.get('title', t.title) t.save() # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo')
def edit(request): """ /todo/edit 的路由函数 """ headers = { 'Content-Type': 'text/html', } # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') # 得到当前编辑的 todo 的 id,如果获取不到就设置为-1 todo_id = int(request.query.get('id', -1)) # 找到当前被编辑的条目的数据,按照当前编辑的 todo 的 id来寻找, # 比如编辑 {"id": 3, "title": "喝水", "user_id": 1},那么按照"id": 3取数据文件中查询 # 比如 t = < Todo id: (3) title: (喝水) user_id: (1) > t = Todo.find_by(id=todo_id) # log('找到当前登录用户要编辑的数据的对应id', t) # 判断被编辑的这个数据对应的用户id(t.user_id)和编辑这个数据的用户id(u.id)是否一致 # 比如 t.user_id = 1, u.id = 1 # 如果两者相等说明登录用户在修改自己的数据,不相等说明登录用户在修改别人的数据,那这个是不允许的,就重定向到登录页面 if t.user_id != u.id: return redirect('/login') # if todo_id < 1: # return error(404) # 如果 t.user_id 和 u.id 相等,先读todo_edit.html 页面 # 再替换模板文件todo_edit.html中的标记字符串{{todo_id}}为当前被编辑的todo的数据的id, # {{todo_title}}为当前被编辑的todo的数据的title body = template('todo_edit.html') body = body.replace('{{todo_id}}', str(t.id)) body = body.replace('{{todo_title}}', str(t.title)) # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中 # 通过response_with_headers拿到服务器响应,headers上面设置了,headers = {'Content-Type': 'text/html',} # 返回请求头, HTTP/1.1 200 VERY OK\r\nContent-Type: text/html\r\n header = response_with_headers(headers) # 把请求头和body拼接 """ HTTP/1.1 200 VERY OK\r\n Content-Type: text/html\r\n\r\n <html> ......... </html> """ r = header + '\r\n' + body return r.encode(encoding='utf-8')
def delete_todo(request, u): # uname = current_user(request) # u = User.find_by(username=uname) # if u is None: # return redirect('/login') # 得到当前编辑的 todo 的 id todo_id = request.query.get('id', -1) # 检查 id 是不是数字 if todo_id == -1 or not todo_id.isdigit(): return redirect('/todo') todo_id = int(todo_id) t = Todo.find_by(id=todo_id) # 检验用户权限 if t is None or t.user_id != u.id: return redirect('/todo') t.remove() return redirect('/todo')
def update(request): """ 用于更新 todo 的路由函数 """ uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') if request.method == 'POST': # 修改并且保存 todo form = request.form() log('debug update', form) todo_id = int(form.get('id', -1)) t = Todo.find_by(id=todo_id) t.title = form.get('title', t.title) t.save() # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo')
def edit(request): headers = { 'Content-Type': 'text/html', } uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') # 得到当前编辑的 todo 的 id todo_id = int(request.query.get('id',-1)) t = Todo.find_by(id=todo_id) if t.user_id != u.id: return redirect('/login') body = template('todo_edit.html') body = body.replace('{{todo_id}}',str(t.id)) body = body.replace('{{todo_title}}'),str(t.title) header = response_with_headers(headers) r = header + '\r\n' +body return r.encode(encoding='utf-8')
def todo_show(request): headers = { 'Content-Type': 'text/html', } uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') todo_id = int(request.query.get('id', -1)) # 通过id查找整个字典 t = Todo.find_by(id=todo_id) # 别的用户不能删除别人的微博 if t.user_id != u.id: return redirect('/login') body = template('todo_edit.html') body = body.replace('{{todo_id}}', str(t.id)) body = body.replace('{{todo_title}}', str(t.title)) header = with_response_headers(headers) r = header + '\r\n' + body return r.encode('utf-8')
def update(request): """ 用于增加新的 _todo 的路由函数 """ uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') if request.method == 'POST': # 修改并保存 todo_ form = request.form() log('debug update ', form) todo_id = int(form.get('id', -1)) t = Todo.find_by(id=todo_id) if u.id != t.user_id: redirect('/todo') t.title = form.get('title', t.title) t.update_time = form.get('update_time', change_time(time.time())) t.save() # 浏览器发送数据过来被处理后,重定向到首页 # 浏览器在亲求新首页地址的时候,就能看到新增的数据了 return redirect('/todo')
def edit(request, u): """ todo edit 的路由函数 """ # uname = current_user(request) # u = User.find_by(username=uname) # if u is None: # return redirect('/login') # 得到当前编辑的 todo 的 id todo_id = request.query.get('id', -1) # 检查 id 是不是数字 if todo_id == -1 or not todo_id.isdigit(): return redirect('/todo') todo_id = int(todo_id) todo = Todo.find_by(id=todo_id) if todo is None or todo.user_id != u.id: return redirect('/todo') # if todo_id < 1: # return error(404) # 替换模板文件中的标记字符串 header = response_with_headers(headers) return http_response(template_render('todo_edit.html', todo=todo))
def delete_todo(request): # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') # 得到当前删除的 todo 的 id todo_id = int(request.query.get('id', -1)) # 找到当前被删除的条目的数据,按照当前编辑的 todo 的 id来寻找, # 比如编辑 {"id": 3, "title": "喝水", "user_id": 1},那么按照"id": 3取数据文件中查询 # 比如 t = < Todo id: (3) title: (喝水) user_id: (1) > t = Todo.find_by(id=todo_id) # log('找到当前登录用户要编辑的数据的对应id', t) # 判断被删除的这个数据对应的用户id(t.user_id)和编辑这个数据的用户id(u.id)是否一致 # 比如 t.user_id = 1, u.id = 1 # 如果两者相等说明登录用户在修改自己的数据,不相等说明登录用户在修改别人的数据,那这个是不允许的,就重定向到登录页面 if t.user_id != u.id: return redirect('/login') # 如果t不是None,意思就是说被删除的这个数据是存在的,那么就用remove函数把t删除掉 if t is not None: t.remove() # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo')