def comment_edit(request): comment_id = int(request.query.get('id', -1)) c = Comment.find(comment_id) user = current_user(request) # 生成一个 edit 页面 body = template('comment_edit.html', comment=c, user=user) return http_response(body)
class PostModel(Model): def __init__(self, db, collection, obj): super(PostModel, self).__init__(db, collection, obj) self.user = obj['user'] self.title = obj['title'] self.body = obj['body'] self.tags = obj['tags'] self.upvotes = obj['upvotes'] self.comments = Comment() # Increases the upvotes on a post def vote_up(self): self.upvotes += 1 self.collection.objects.update({'_id': self.get_id()}, {'$inc': {'upvotes': 1}}) # Adds a comment on this post def add_comment(self, **kwargs): return self.comments.insert(post_id=self.get_id(), **kwargs) # Gets comments on this post def get_comments(self): return self.comments.find(post_id=self.get_id()) # Use for removing comments by the user who created them, or by the poster def remove_comments(self, **kwargs): self.comments.remove(**kwargs) # Removes self and the comments def remove(self): self.remove_comments(post_id=self.get_id()) super(PostModel, self).remove()
def delike(comment_id): user = current_user() c = Comment.find(comment_id) if Comment.check_token(): c.delike(user.id) user.delike_comment(comment_id) return redirect(url_for('tweet.detail', tweet_id=c.tweet().id, token=gg.token[user.id]))
def comment_update(): form = request.form.to_dict() c_id = form['id'] Comment.update(form) c = Comment.find(c_id) b = Blog.find(c.blog_id) return redirect(url_for('routes_blog.detail', blog_id=b.id))
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 delete(): comment_id = int(request.args.get('id')) item = Comment.find(comment_id) if same_user_required(item): t = Comment.delete(comment_id) return jsonify(t.json()) else: pass
def f(): data, u = request_data() comment_id = data.get('id') c = Comment.find(comment_id) if u.id == c.user_id: return route_function() else: blog = Blog.find(c.blog_id) return redirect(url_for('routes_blog.detail', blog_id=blog.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(comment_id): u = current_user() # comment_id = request.args.get('id', -1) # comment_id = int(comment_id) token = request.args.get('token') if Comment.check_token(token, gg.csrf_tokens): c = Comment.find(comment_id) if u.id == c.user_id: c.remove(comment_id) return redirect(url_for('tweet.index'))
def comment_delete(request): u = current_user(request) # 删除微博 comment_id = int(request.query.get('id', None)) c = Comment.find(comment_id) log('comment_delete 1', c.user_id) w = Weibo.find(c.weibo_id) log('comment_delete 2', w.user_id) if c.user_id == u.id or w.user_id == u.id: Comment.delete(comment_id) return redirect('/weibo/index')
def comment_delete(): u = current_user() comment_id = request.args.get('id', None) c = Comment.find(comment_id) b_user_id = request.args.get('b_id', None) a = (u.id == c.user_id) b = (u.id == b_user_id) blog = Blog.find_by(user_id=b_user_id) if a or b: Comment.delete(comment_id) return redirect(url_for('routes_blog.detail', blog_id=blog.id))
def edit(comment_id): u = current_user() # comment_id = int(request.args.get('id', -1)) token = request.args.get('token') if Comment.check_token(token, gg.csrf_tokens): c = Comment.find(comment_id) if u.id == c.user_id: body = render_template('comment_edit.html', comment_id=c.id, comment_content=c.content, token=token) return make_response(body) return redirect(url_for('tweet.index'))
def f(request): log('comment same user required', request) u = current_user(request) if request.method == 'GET': comment_id = int(request.query.get('id')) else: comment_id = int(request.form().get('id')) c = Comment.find(comment_id) if c.is_owner(u.id): return route_function(request) else: return redirect('/login')
def f(request): log('comment user required', request) u = current_user(request) if request.method == 'GET': comment_id = int(request.query.get('id')) else: comment_id = int(request.form().get('id')) c = Comment.find(comment_id) if route_function == comment_delete: if c.is_owner(u.id) or Weibo.find(c.weibo_id).user_id == u.id: return route_function(request) else: return redirect('/login') else: if c.is_owner(u.id): return route_function(request) else: return redirect('/login')
class UserModel(Model): def __init__(self, db, collection, obj): super(UserModel, self).__init__(db, collection, obj) self.username = obj['username'] self.password = obj['password'] self.voted = obj['voted'] self.posts = Post() self.comments = Comment() # Change password with authentication--username auth to be done # in middleware def change_password(self, oldpass, newpass): if oldpass == self.password: self.password = newpass self.collection.update({'_id': self.get_id()}, password=newpass) return True return False # Vote a post up, cannot do this more than once for any given post def vote_up(self, post_id): if not post_id in self.voted: p = self.posts.find_one(_id=post_id) p.vote_up() self.voted.append(post_id) self.collection.update({'_id': self.get_id()}, voted=self.voted) # Adds a post under the users page def add_post(self, **kwargs): return self.posts.insert(user=self.username, **kwargs) # Get blog posts made by this user, and with other arguments def get_posts(self, **kwargs): return self.posts.find(user=self.username, **kwargs) # Get comments made by the user, with other parameters def get_comments(self, **kwargs): return self.comments.find(user=self.username, **kwargs)
def delete(comment_id): if Comment.check_token(): c = Comment.find(comment_id) Comment.check_id(id=comment_id) c.remove(comment_id) return redirect(url_for('tweet.detail', tweet_id=c.tweet_id, token=gg.token[current_user().id]))
def get_comments(self, **kwargs): comments = Comment() return comments.find(img_id=self.get_id(), **kwargs)
def detail(): comment_id = int(request.args.get('id')) b = Comment.find(comment_id) return jsonify(b.json())
def comment_edit(): u = current_user() comment_id = request.args.get('id', -1) c = Comment.find(comment_id) return render_template('routes_blog/comment.html', comment=c, u=u)
def comment_edit(request): comment_id = int(request.query.get('id', -1)) c = Comment.find(comment_id) # 替换模板文件中的标记字符串 body = template('comment_edit.html', comment=c) return http_response(body)
def edit(comment_id): user = current_user() if Comment.check_token(): c = Comment.find(comment_id) Comment.check_id(id=comment_id) return render_template('tweet/comment_edit.html', c=c, token=gg.token[user.id], user=user)