def get(self, post_id): post = Post.getById(post_id) if not post: self.error(404) self.write( "<h1>404 Not Found</h1>The resource could not be found.") return comments = Comment.getByPostID(post_id) comments_count = Comment.countByPostID(post_id) likes_num = Like.countByPostID(post_id) # Get post status (liked or not) by a specific user like_btn_txt = "Like" if self.user: if Like.getByPostAndAuthor(post_id, self.user.name): like_btn_txt = "Unlike" self.render("post.html", post=post, comments_count=comments_count, comments=comments, likes_num=likes_num, like_text=like_btn_txt)
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): # check post state (liked or unliked) like = Like.getByPostAndAuthor(post_id, self.user.name) # You must be authenticated in order to Like or Unlike the post if not self.user: return self.redirect('/login') if like: # Unlike the post Like.unlike(like.key.id()) else: # Like the post Like.like(post_id, self.user.name) time.sleep(0.1) self.redirect('/post/%s' % post_id)
def remove_likes_for_post(post): ''' removes the likes for the post ''' likes = Like.query(Like.post == post.key) if likes: for like in likes.fetch(): like.key.delete()
def from_dict(source): username = source['username'] likes = source['likes'] uid = source['uid'] likes_list = [Like.from_dict(like) for like in source['likes_list']] content = source['content'] return Comment(username=username, likes=likes, likes_list=likes_list, uid=uid, content=content)
def from_dict(source): image_url = source['image_url'] username = source['username'] likes = source['likes'] tags = source['tags'] uid = source['uid'] amount_of_comments = source['amount_of_comments'] comments = [Comment.from_dict(comment) for comment in source['comments']] likes_list = [Like.from_dict(like) for like in source['likes_list']] return Post(image_url=image_url,username=username, amount_of_comments=amount_of_comments, comments=comments,likes=likes,likes_list=likes_list, tags=tags,uid=uid)