def route_comment_add(request): form = request.form() c = Comment(form) log('commentAdd c', c) c.save() w = Weibo.find(c.weibo_id) return redirect('/weibo?user_id={}'.format(w.user_id))
def func(request): username = current_user(request) log('登录鉴定', username) if username == '游客': # 没登录 不让看 重定向到 /login return redirect('/login') return route_function(request)
def func(request): username = current_user(request) log("登录鉴定", username) if username == "游客": return redirect('/login') else: return function(request)
def route_comment_add(request): form = request.form() c = Comment(form) c.save() w = Weibo.find(c.weibo_id) # 重定向到用户的主页 return redirect('/weibo?user_id={}'.format(w.user_id))
def route_add(request): # headers = { # 'Content-Type': 'text/html', # } form = request.form() o = Todo(form) o.save() return redirect('/todo')
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_header(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user.id) # 任一 user 访问任一 index current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) # format 函数的字典用法 # 注意 u.id 是 current_user # user.username 是博主 w = { 'id': weibo.id, 'user_id': u.id, 'content': weibo.content, 'username': user.username, 'time': weibo.created_time, 'comments': comments, } # 手动处理 weibos 这个 list # 把每个 weibo 以 <p> 的形式展现在页面 return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a></p> <button class="weibo-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="weibo-comment-form weibo-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) # 用 join() 返回 str weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_comment_add(request): # username = current_user(request) # user = User.find_by(username=username) form = request.form() c = Comment(form) c.save() w = Weibo.find(c.weibo_id) # 重定向到用户的主页 return redirect('/weibo?user_id={}'.format(w.user_id))
def route_complete(request): # headers = { # 'Content-Type': 'text/html', # } id = int(request.query.get('id', -1)) t = Todo.find(id) t.toggleComplete() t.save() return redirect('/todo')
def route_complete(request): # headers = { # 'Content-Type': 'text/html', # } id = int(request.query.get('id', -1)) o = Todo.find(id) o.toggleComplete() o.save() return redirect('/todo')
def route_weibo_add(request): username = current_user(request) user = User.find_by(username=username) # 创建微博 form = request.form() w = Weibo(form) w.user_id = user.id w.save() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) log('weibos', weibos) current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) w = { "id": weibo.id, "user_id": u.id, "content": weibo.content, "username": user.username, "time": weibo.created_time, "comments": comments, } log('comments debug', comment_list) return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a> <button class="gua-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="gua-comment-form gua-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_update(request): username = current_user(request) user = User.find_by(username=username) form = request.form() content = form.get('content', '') weibo_id = int(form.get('id', -1)) w = Weibo.find(weibo_id) if user.id != w.user_id: return error(request) w.content = content w.save() return redirect('/weibo?user_id={}'.format(user.id))
def route_complete(request): headers = { 'Content-Type': 'text/html', } # username = current_user(request) # header = response_with_headers(headers) # user = User.find_by(username=username) id = int(request.query.get('id', -1)) o = Todo.find(id) o.toggleComplete() o.save() return redirect('/todo')
def route_weibo_delete(request): username = current_user(request) user = User.find_by(username=username) # 删除微博 weibo_id = request.query.get('id', None) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) if w.user_id == user.id: w.delete() return redirect('/weibo?user_id={}'.format(user.id)) else: return error(request)
def route_weibo_add(request): headers = { 'Content-Type': 'text/html', } username = current_user(request) header = response_with_headers(headers) user = User.find_by(username=username) # 创建微博 form = request.form() w = Weibo(form) w.user_id = user.id w.save() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_delete(request): headers = { 'Content-Type': 'text/html', } username = current_user(request) header = response_with_headers(headers) user = User.find_by(username=username) # 删除微博 weibo_id = request.query.get('id', None) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) w.delete() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_update(request): username = current_user(request) user = User.find_by(username=username) form = request.form() content = form.get('content', '') weibo_id = int(form.get('id', -1)) w = Weibo.find(weibo_id) if user.id != w.user_id: return error(request) w.content = content w.save() # 重定向到用户的主页 return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_delete(request): # headers = { # 'Content-Type': 'text/html', # } username = current_user(request) # header = response_with_header(headers) user = User.find_by(username=username) # 删除微博 weibo_id = request.query.get('id', None) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) w.delete() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_add(request): """ 这个函数相当于一个裸的 API 它提取某个 HTML页面 的数据 处理过后 redirect 到一个页面 """ # headers = { # 'Content-Type': 'text/html', # } username = current_user(request) log('发微博的用户: ', username) # header = response_with_header(headers) user = User.find_by(username=username) # 创建一个新微博实例 # 就是把 weibo_new.html 的数据处理 form = request.form() w = Weibo(form) w.user_id = user.id w.save() return redirect('/weibo?user_id={}'.format(user.id))
def route_complete(request): id = int(request.query.get('id', -1)) o = Todo.find(id) o.complete = not o.complete o.save() return redirect('/todo')
def route_add(request): form = request.form() o = Todo(form) o.save() return redirect('/todo')
def func(request): username = current_user(request) # log('登录验证', username) if username == 'guest': return redirect('/login') return route_function(request)
def func(request): username = current_user(request) log('登录验证', username) if username == 'guest': return redirect('/login') return route_function(request)