def post(self, post_id): # if no signed in user, redirect to the login page if not self.user: self.redirect('/login') post = Post.get_by_id(int(post_id)) if post: query = Like.query(Like.post == post.key) like = query.get() else: self.render_404(error_message="Post {} not found.".format(post_id)) if like: post_id = post.key.id() like.key.delete() # redirect to the post page self.redirect_after_delay('/{}'.format(post_id)) else: # create a dictionary to hold any error messages params = dict(post=post, comments=post.get_comments(), owner=post.user.get()) # user has not liked this post, so show an error. params['error'] = "Cannot unlike a post which has not been liked." self.render("permalink.html", **params)
def render(self): # replace new line characters with breaks self.__render_text = self.content.replace('\n', '<br>') comments_query = Comment.query(Comment.post == self.key)\ .order(-Comment.created) num_comments = comments_query.count() if num_comments == 1: text = "comment" else: text = "comments" comment_count = "{} {}".format(num_comments, text) likes_query = Like.query(Like.post == self.key) num_likes = likes_query.count() if num_likes == 1: text = "like" else: text = "likes" like_count = "{} {}".format(num_likes, text) user = self.user.get() return utils.render_str('post.html', post=self, author=user, comment_count=comment_count, like_count=like_count)
def is_liked_by(self, user): # get all likes for this post query = Like.query(Like.post == self.key) results = query.get() q = query.filter(Like.user == user.key) result = q.fetch() # print "result = {}".format(result) return result
def get_likes(self): """ queries the datastore for likes on this post """ likes_query = Like.query(Like.post == self.key) likes = likes_query.fetch() return likes