def get(self, post_id): """ Renders the post.html template with the requested post """ current_user = self.authenticate_user() if not post_id: self.render("post.html", error="No post id provided") else: post = Post.get_by_id(long(post_id)) if not post: self.render("post404.html") else: user = User.get_by_id(post.user.id()) comments = Comment.query().filter(Comment.post == post.key).fetch() likes = Like.query(Like.post == post.key).fetch() like = Like.query( Like.user == current_user.key, Like.post == post.key).get() self.render( "post.html", post=post, current_user=current_user, user=user, like_count = len(likes), like=like, comments=comments)
def post(self, post_id): ''' Adds or removes likes depending on if a like exists for the post by the current user and the current user did not write the post ''' current_user = self.authenticate_user() post = Post.get_by_id(long(post_id)) like = Like.query(Like.user == current_user.key, Like.post == post.key).get() if post.user == current_user.key: self.redirect(self.request.referer) elif like: remove_user_like(current_user, like) remove_post_like(post, like) like.key.delete() else: like = Like(user=current_user.key, post=post.key) like.put() post.likes.append(like.key) post.put() current_user.likes.append(like.key) current_user.put() self.redirect(self.request.referer)
def post(self, post_id): """ Attempt to create a new blog post """ current_user = self.authenticate_user() if not current_user: self.redirect("/login") else: content = self.request.get("content") title = self.request.get("subject") post = Post.get_by_id(long(post_id)) if not post: self.render("/post404.html") elif current_user.key != post.user: self.redirect("/post/" + str(post.key.id())) elif not content or not title: self.render_front(post_id, error="We need both a title and content") else: post.title = title post.content = content post.put() self.redirect("/post/" + str(post.key.id()))
def post(self, id, update=False): post_id = int(id) res = self.client.get(id) if res is None or update: post = Post.get_by_id(post_id) last_update = datetime.utcnow() res = (post,last_update) self.client.set(id, res) return res
def get_post_vars(post_id): post = Post.get_by_id(post_id) errors = {} template_vars = { 'id': post_id, 'title': post.title, 'content': post.content, 'errors': errors } return template_vars
def post(self, post_id): ''' Deletes the post if the current user was the author and the post exists ''' current_user = self.authenticate_user() post = Post.get_by_id(long(post_id)) if not current_user: self.redirect("/login") elif post.user != current_user.key: self.redirect(self.request.referer) elif post: remove_user_post(current_user, post) remove_likes_for_post(post) post.key.delete() self.redirect("/") else: self.render("post404.html")
def render_front(self, post_id="", error=""): """ Simplify the rendering of the new post template """ current_user = self.authenticate_user() post = Post.get_by_id(long(post_id)) if not current_user: self.redirect("/login") elif not post: self.render("/post404.html") elif current_user.key != post.user: self.write("You are not the author of this post") else: user = post.user.id() self.render("edit_post.html", title=post.title, content=post.content, error=error, current_user=current_user, user=user, post_id=post_id)
def post(self, post_id): """ Handles the post request from the post page. This is the comment submission. """ content = self.request.get("content") current_user = self.authenticate_user() post = Post.get_by_id(long(post_id)) if not post: self.render("post404.html") else: user = User.get_by_id(post.user.id()) comments = Comment.query().filter(Comment.post == post.key).fetch() if not current_user: self.redirect("/login") elif not content: self.render("post.html", comments=comments, post=post, current_user=current_user, user=user, error="No comment text was received") else: comment = Comment(content=content, user=current_user.key, post=post.key) comment.put() current_user.comments.append(comment.key) current_user.put() self.render( "post.html", post=post, user=user, current_user=current_user, comments=comments)