def f(request): log('comment_or_weibo_owner_required') if request.method == 'GET': data = request.query elif request.method == 'POST': data = request.form() else: raise ValueError('不支持的方法', request.method) comment_key = 'comment_id' weibo_key = 'weibo_id' if comment_key in data: c = Comment.one(id=int(data[comment_key])) if c is None: return redirect('/weibo/index') else: user_id = c.user_id elif weibo_key in data: w = Weibo.one(id=int(data[weibo_key])) if w is None: return redirect('/weibo/index') else: user_id = w.user_id else: raise ValueError('不支持的参数', data) u = current_user(request) if user_id == u.id: log('不是评论或者微博的作者', user_id, u.id) return route_function(request) else: return redirect('/weibo/index')
def delete(request): weibo_id = int(request.query['weibo_id']) Weibo.delete(weibo_id) cs = Comment.all(weibo_id=weibo_id) for c in cs: c.delete() return redirect('/weibo/index')
def comment_update(request): """ 用于增加新 weibo 的路由函数 """ form = request.form() comment_id = int(form['comment_id']) Comment.update(comment_id, content=form['content']) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/weibo/index')
def add(request): """ 用于增加新 weibo 的路由函数 """ u = current_user(request) form = request.form() Weibo.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/weibo/index')
def update(request): """ 用于增加新 todo 的路由函数 """ form = request.form() log('todo update', form, form['id'], type(form['id'])) todo_id = int(form['id']) Todo.update(todo_id, title=form['title']) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo/index')
def add(request): """ 用于增加新 todo 的路由函数 """ form = request.form() u = current_user(request) Todo.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo/index')
def f(request): log('comment_owner_required') u = current_user(request) id_key = 'comment_id' if id_key in request.query: comment_id = request.query[id_key] else: comment_id = request.form()[id_key] c = Comment.one(id=int(comment_id)) if c.user_id == u.id: log('不是评论作者', c) return route_function(request) else: return redirect('/weibo/index')
def f(request): log('weibo_owner_required') u = current_user(request) id_key = 'weibo_id' if id_key in request.query: weibo_id = request.query[id_key] else: weibo_id = request.form()[id_key] w = Weibo.one(id=int(weibo_id)) if w.user_id == u.id: log('不是微博作者', w) return route_function(request) else: return redirect('/weibo/index')
def f(request): log('same_user_required', route_function) u = current_user(request) log('same_user_required', request.method) if request.method == 'GET': todo_id = int(request.query['id']) elif request.method == 'POST': todo_id = int(request.form()['id']) else: raise ValueError('不支持的请求方法', request.method) t = Todo.one(id=todo_id) if t.user_id == u.id: return route_function(request) else: return redirect('/todo')
def route_login(request): """ 登录页面的路由函数 """ log('login, headers', request.headers) log('login, cookies', request.cookies) user_current = current_user(request) log('current user', user_current) form = request.form() user_login, result = User.login_user(form) result = urllib.parse.quote_plus(result) if user_login is not None: session_id = Session.add(user_login.id) headers = {'Set-Cookie': 'session_id={}'.format(session_id)} else: headers = {} # return redirect('/todo') return redirect('/login/view', result=result, headers=headers)
def comment_delete(request): comment_id = int(request.query['comment_id']) Comment.delete(comment_id) return redirect('/weibo/index')
def comment_add(request): u = current_user(request) form = request.form() weibo_id = int(form['weibo_id']) Comment.add(form, u.id, weibo_id) return redirect('/weibo/index')
def delete(request): todo_id = int(request.query['id']) Todo.delete(todo_id) return redirect('/todo/index')
def route_register(request): form = request.form() result = User.register_user(form) # 换行符需要编码,不然显示不全 result = urllib.parse.quote_plus(result) return redirect('/register/view', result=result)