def comment_add(request): user = current_user(request) form = request.form() c = Comment.new(form, user_id=user.id, user_name=user.username) # c.save() uid = c.tweet().user().id return redirect('/tweet/index?user_id={}'.format(uid))
def comment_delete(request): u = current_user(request) comment_id = request.query.get('id', -1) comment_id = int(comment_id) c = Comment.find(comment_id) if u.id == c.user_id: c.remove(comment_id) return redirect('/tweet/index?user_id={}'.format(u.id))
def add(request): u = current_user(request) form = request.form() c = Comment(form) c.user_id = u.id c.created_time = c.change_time() c.save() return redirect('/weibo')
def add(request): user = current_user(request) form = request.form() t = Tweet.new(form, user_id=user.id, user_name=user.username) # t.user_id = u.id # t.save() # redirect有必要加query吗 return redirect('/tweet/index?user_id={}'.format(user.id))
def delete(request): weibo_id = int(request.query.get('id', -1)) w = Weibo.find_by(id=weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) Weibo.delete(weibo_id) return redirect('/weibo/detail')
def add(request): u = current_user(request) form = request.form() w = Weibo(form) w.user_id = u.id w.created_time = w.change_time() w.save() return redirect('/weibo')
def edit(request): weibo_id = int(request.query.get('id', -1)) w = Weibo.find_by(id=weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) body = template('weibo_edit.html', weibo=w) return http_response(body)
def delete(request): comment_id = int(request.query.get('id', -1)) c = Comment.find_by(id=comment_id) w = Weibo.find_by(id=c.weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) Comment.delete(comment_id) return redirect('/weibo/detail')
def update(request): weibo_id = int(request.query.get('id', -1)) w = Weibo.find_by(id=weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) form = request.form() w.content = form.get('content') w.save() return redirect('/weibo/detail')
def edit(request): u = current_user(request) tweet_id = int(request.query.get('id', -1)) t = Tweet.find(tweet_id) if u.id == t.user_id: body = template('tweet_edit.html', tweet_id=t.id, tweet_content=t.content) return http_response(body) return redirect('/tweet/index?user_id={}'.format(u.id))
def comment_edit(request): u = current_user(request) comment_id = int(request.query.get('id', -1)) c = Comment.find(comment_id) if u.id == c.user_id: body = template('comment_edit.html', comment_id=c.id, comment_content=c.content) return http_response(body) return redirect('/tweet/index?user_id={}'.format(u.id))
def delete(request): u = current_user(request) tweet_id = int(request.query.get('id')) t = Tweet.find(tweet_id) if u.id == t.user_id: # 这里只是删除了tweet,但是其所拥有的comment的deleted字段变成False t.remove(tweet_id) for c in t.comments(): c.deleted = True c.save() # redirect有必要加query吗 return redirect('/tweet/index?user_id={}'.format(u.id))
def index(request): # 从query取到user_id可以到该用户界面评论 user_id = int(request.query.get('user_id', -1)) if user_id == -1: u = current_user(request) user_id = u.id user = User.find(user_id) if user is None: return redirect('/login') else: tweets = Tweet.find_all(user_id=user.id, deleted=False) body = template('tweet_index.html', tweets=tweets, user=user) return http_response(body)
def add(request): """ 接受浏览器发过来的 添加todo 请求 添加数据并发一个 302 定向给浏览器 浏览器就会去请求 /todo 从而回到主页 """ # 得到浏览器发送的表单 form = request.form() uid = current_user(request) # 创建一个 todo Todo.new(form, uid) # 让浏览器刷新页面到主页去 return redirect('/todo')
def delete_todo(request): """ todo delete 的路由函数 """ # 得到当前编辑的 todo的 id todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) uid = current_user(request) u = User.find_by(id=uid) # 验证用户id是否相同 if t.user_id != u.id: return redirect('/login') if t is not None: # 删除 todo t.remove() return redirect('/todo')
def edit(request): """ todo edit 的路由函数 """ # 得到当前编辑的 todo的 id todo_id = int(request.query.get('id', -1)) t = Todo.find_by(id=todo_id) uid = current_user(request) u = User.find_by(id=uid) # 验证用户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_task }}', str(t.task)) return http_response(body)
def index(request): """ todo 首页 的路由函数 """ # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login uid = current_user(request) todo_list = Todo.find_all(user_id=uid) todo_html = '' if todo_list is not None: # 下面生成一个 html 字符串 todos = [] for t in todo_list: edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id) delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id) s = '<h3>{} : {} {} {}</h3>'.format(t.id, t.task, edit_link, delete_link) todos.append(s) todo_html = ''.join(todos) # 替换模板文件中的标记字符串 body = template('todo_index.html') body = body.replace('{{ todos }}', todo_html) return http_response(body)
def detail(request): u = current_user(request) weibo_list = Weibo.find_all(user_id=u.id) body = template('weibo_detail.html', weibos=weibo_list) return http_response(body)