def post(self): """ Actions to post, edit, and update comments on the post. """ user_name = self.user.name # Post a comment if self.request.get('comment'): comment = self.request.get('comment') # comment content author = user_name post_id = self.request.get('post_id') post = Post.by_id(int(post_id)) comment = Comment(author=author, comment=comment, parent=post.key) comment.put() self.redirect('/post?post_id=' + post_id) # Delete an existing comment elif self.request.get('delete'): post_id = self.request.get('post_id') post = Post.by_id(int(post_id)) comment_id = int(self.request.get('delete')) comment_to_delete = Comment.get_by_id(comment_id, parent=post.key) comment_to_delete.key.delete() self.redirect('/post?post_id=' + post_id) # Edit an existing comment elif self.request.get('edit'): post_id = self.request.get('post_id') post = Post.by_id(int(post_id)) comment_id = int(self.request.get('edit')) comment_to_edit = Comment.get_by_id(comment_id, parent=post.key) if self.user.name == comment_to_edit.author: # Verify user is author self.render('editcomment.html', comment=comment_to_edit, user_name=user_name, comment_id=comment_id, post_id=post_id) return else: self.redirect('/post?post_id=' + post_id) # Wish I could flash error msg elif self.request.get('update'): post_id = self.request.get('post_id') post = Post.by_id(int(post_id)) comment_id = int(self.request.get('update')) comment_to_update = Comment.get_by_id(comment_id, parent=post.key) updated_comment_contents = self.request.get('updated-comment') updated_comment_contents = cgi.escape(updated_comment_contents) comment_to_update.comment = updated_comment_contents comment_to_update.put() self.redirect('/post?post_id=' + post_id) else: self.redirect('/')
def post(self): """ Form action for likes. """ post_id = self.request.get('liked') post = Post.by_id(int(post_id)) if not self.user.name == post.author: # Must not be author if not self.already_liked(post): self.like(post) self.redirect('/') else: error = "You already liked that item." self.redirect('/?error=' + error) else: error = "You can't like your own posts." self.redirect('/?error=' + error)
def get(self, **kw): post_id = self.request.get('post_id') post = Post.by_id(int(post_id)) comments = Comment.query(ancestor=post.key) comments = comments.order(-Comment.created) comments = comments.fetch(5) user_name = "" user_is_author = False if self.user: user_name = self.user.name if post.author and user_name == post.author: user_is_author = True self.render('viewpost.html', post=post, user_name=user_name, user_is_author=user_is_author, comments=comments, **kw)
def post(self): # This POST option to edit is only exposed if logged-in user is author # See template viewpost.html if self.request.get('edit'): post_id = self.request.get('edit') post = Post.by_id(int(post_id)) user_name = "" author = "" user_name = self.user.name if user_name == post.author: # Double checks user is author post.content = post.content.replace('<br>', '') # remove <br>s self.render('editpost.html', post=post) else: self.redirect('/') # This POST option to delete is only exposed if logged-in user is author # See template viewpost.html elif self.request.get('delete'): post_id = self.request.get('delete') post = Post.get_by_id(int(post_id), parent=blog_key()) post.key.delete() self.redirect('/') # POST request to cancel editing (from editpost.html) elif self.request.get('cancel'): post_id = self.request.get('cancel') self.redirect('/post?post_id=' + post_id) # POST request to update post (from editpost.html) elif self.request.get('update'): post_id = self.request.get('update') post = Post.get_by_id(int(post_id), parent=blog_key()) subject = self.request.get('subject') content = self.request.get('content') content = cgi.escape(content) content = content.replace('\n', '<br>') post.subject = subject post.content = content post.put() self.redirect('/post?post_id=' + post_id)