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 index(request): """ weibo 首页的路由函数 """ u = current_user(request) ws = Weibo.find_all(user_id=u.id) return html_response('weibo_index.html', weibos=ws, user=u)
def f(request): log('comment_owner_required') u = current_user(request) # 查询评论id,识别用户 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)) w_id = c.weibo_id w = Weibo.one(id=int(w_id)) # 判断评论用户 if c.user_id == u.id: comment_owner = True else: comment_owner = False # 判断微博用户 if w.user_id == u.id: weibo_owner = True else: weibo_owner = False if comment_owner or weibo_owner: return route_function(request) else: return redirect('/weibo/index')
def comment_add(request): u = current_user(request) form = request.form() form['user_id'] = u.id c = Comment.insert(form) log('comment add', c, u, form) return redirect('/weibo/index')
def route_register_view(request): user = current_user(request) result = request.query.get('result', '') result = urllib.parse.unquote_plus(result) return html_response('register.html', result=result, username=user.username)
def index(request): """ todo 首页的路由函数 """ u = current_user(request) ts = Todo.find_all(user_id=u.id) return html_response('todo_index.html', todos=ts)
def add(request): """ 用于增加新 weibo 的路由函数 """ u = current_user(request) form = request.form() Weibo.add(form, u.id) return redirect('/weibo/index')
def index(request): """ weibo 首页的路由函数 """ u = current_user(request) weibos = Weibo.all(user_id=u.id) # 替换模板文件中的标记字符串 return html_response('weibo_index.html', weibos=weibos, user=u)
def add(request): """ 用于增加新 todo 的路由函数 """ form = request.form() u = current_user(request) Todo.add(form, u.id) return redirect('/todo/index')
def comment_add(request): u = current_user(request) form = request.form() weibo_id = int(form['weibo_id']) c = Comment.add(form, u.id, weibo_id) log('comment add', c, u, form) return redirect('/weibo/index')
def route_index(request): """ 主页的处理函数, 返回主页的响应 """ header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' body = template('index.html') u = current_user(request) body = body.replace('{{username}}', u.username) r = header + '\r\n' + body return r.encode()
def add(request): """ 用于增加新 weibo 的路由函数 """ u = current_user(request) form = request.form() Weibo.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/weibo/index')
def add(request): """ 用于增加新 todo 的路由函数 """ form = request.form() u = current_user(request) Todo.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo')
def index(request): """ weibo 首页的路由函数 """ if 'user_id' in request.query: user_id = int(request.query['user_id']) else: u = current_user(request) user_id = u.id weibos = Weibo.all(user_id=user_id) return html_response('weibo_index.html', weibos=weibos)
def comment_add(request): u = current_user(request) form = request.form() weibo_id = int(form['weibo_id']) c = Comment(form) c.user_id = u.id c.weibo_id = weibo_id c.save() log('comment add', c, u, form) return redirect('/weibo/index')
def add(request): """ 用于增加新 todo 的路由函数 """ form = request.json() u = current_user(request) Todo.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 data = dict(message='成功添加 todo') return json_response(data)
def comment_add(request): u = current_user(request) form = request.form() form['user_id'] = u.id # weibo_id = int(form['weibo_id']) # c = Comment(form) # c.user_id = u.id # c.weibo_id = weibo_id # c.save() Comment.new(form) log('comment add', form) 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: return route_function(request) else: return redirect('/weibo/index')
def index(request): """ weibo 首页的路由函数 """ if 'user_id' in request.query: user_id = int(request.query['user_id']) else: u = current_user(request) user_id = u.id weibos = Weibo.all(user_id=user_id) for w in weibos: print('w.dict', w.__dict__) # 替换模板文件中的标记字符串 return html_response('weibo_index.html', weibos=weibos)
def route_login_view(request): """ 登录页面视图 """ log('login, headers', request.headers) log('login, cookies', request.cookies) user = current_user(request) log('current user', user) result = request.query.get('result', '') result = urllib.parse.unquote_plus(result) return html_response('login.html', result=result, username=user.username)
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('same_user_required', route_function) u = current_user(request) if request.method == 'GET': todo_id = int(request.query['id']) elif request.method == 'POST': todo_id = int(request.form()['id']) else: raise Exception('不支持的请求方法', request.method) t = Todo.find_by(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('/login/view', result=result, headers=headers)
def comment_add(request): u = current_user(request) form = request.form() Comment.add(form, u.id) log('comment add') return redirect('/weibo/index')
def index(request): """ todo 首页的路由函数 """ u = current_user(request) return html_response('ajax_todo_index.html')