def post(self): cookie_user = self.get_user_from_cookie() if cookie_user: postID = self.request.get('postID') post_obj = Post.get_by_id(int(postID)) like_obj = Likes.query(Likes.post == post_obj.key).get() if post_obj.user == cookie_user.key: self.write(json.dumps(({'like_count': -1}))) else: if like_obj: liked_by = like_obj.user_id for u_id in liked_by: if u_id == cookie_user.key.id(): self.write(json.dumps(({'like_count': -2}))) return like_obj.like_count += 1 liked_by.append(cookie_user.key.id()) like_obj.put() self.write( json.dumps(({ 'like_count': like_obj.like_count }))) else: like_obj = Likes(post=post_obj.key, like_count=1) like_obj.user_id.append(cookie_user.key.id()) like_obj.put() time.sleep(0.2) self.write( json.dumps(({ 'like_count': like_obj.like_count }))) else: return None
def get(self): posts = [] cookie_user = self.get_user_from_cookie() if cookie_user: loggedin_user = cookie_user else: loggedin_user = None posts = Post.query(Post.is_draft == False).order(-Post.created) all_users = User.query() likes = Likes.query() list_dict = [] for p in posts: p_dict = {} for u in all_users: if p.user == u.key: p_dict['p_id'] = p.key.id() p_dict['p_title'] = p.title p_dict['p_content'] = p.content p_dict['p_created'] = p.created p_dict['a_name'] = u.fullname p_dict['a_key'] = u.key p_dict['a_id'] = u.key.id() for l in likes: if l.post == p.key: p_dict['like_count'] = l.like_count comment_count = Comments.query(Comments.post == p.key).count() p_dict['c_count'] = comment_count list_dict.append(p_dict) return self.render('home.html', user=loggedin_user, list_dict=list_dict)
def post(self): cookie_user = self.get_user_from_cookie() if cookie_user: postID = self.request.get('postID') post_obj = Post.get_by_id(int(postID)) like_obj = Likes.query(Likes.post == post_obj.key).get() if post_obj.user == cookie_user.key: self.write(json.dumps(({'like_count': -1}))) else: if like_obj: liked_by = like_obj.user_id for u_id in liked_by: if u_id == cookie_user.key.id(): self.write(json.dumps(({'like_count': -2}))) return like_obj.like_count += 1 liked_by.append(cookie_user.key.id()) like_obj.put() self.write(json.dumps(({'like_count': like_obj.like_count}))) else: like_obj = Likes(post=post_obj.key, like_count=1) like_obj.user_id.append(cookie_user.key.id()) like_obj.put() time.sleep(0.2) self.write(json.dumps(({'like_count' : like_obj.like_count}))) else: return None
def get(self, post_id): # user_id = check_for_valid_cookie(self) # cookie_user = User.get_by_id(int(user_id)) cookie_user = self.get_user_from_cookie() post = Post.get_by_id(int(post_id)) if post: comments = Comments.query(Comments.post == post.key).order(-Comments.comment_date) likes = Likes.query(Likes.post == post.key).get() list_dict = [] comment_count = comments.count() if likes: no_of_likes = likes.like_count else: no_of_likes = 0 for c in comments: c_dict = {} if c.post == post.key: c_dict['c_id'] = c.key.id() c_dict['c_comment'] = c.comment c_dict['c_date'] = c.comment_date user = User.query(User.key == c.user).get() c_dict['c_u_name'] = user.fullname c_dict['c_u_key'] = user.key c_dict['c_u_id'] = user.key.id() list_dict.append(c_dict) if cookie_user: if not post: self.error(404) return if cookie_user.key == post.user: self.render("blog.html", post=post, user=cookie_user, comments=list_dict, like_count=no_of_likes, comment_count=comment_count, is_author=True) else: self.render("blog.html", post=post, user=cookie_user, comments=list_dict, like_count=no_of_likes, comment_count=comment_count, is_author=False) else: self.render("blog.html", post=post, user=None, comments=list_dict, like_count=no_of_likes, comment_count=comment_count, is_author=False) else: self.error(404) return
def get(self, post_id): # user_id = check_for_valid_cookie(self) # cookie_user = User.get_by_id(int(user_id)) cookie_user = self.get_user_from_cookie() post = Post.get_by_id(int(post_id)) if post: comments = Comments.query( Comments.post == post.key).order(-Comments.comment_date) likes = Likes.query(Likes.post == post.key).get() list_dict = [] comment_count = comments.count() if likes: no_of_likes = likes.like_count else: no_of_likes = 0 for c in comments: c_dict = {} if c.post == post.key: c_dict['c_id'] = c.key.id() c_dict['c_comment'] = c.comment c_dict['c_date'] = c.comment_date user = User.query(User.key == c.user).get() c_dict['c_u_name'] = user.fullname c_dict['c_u_key'] = user.key c_dict['c_u_id'] = user.key.id() list_dict.append(c_dict) if cookie_user: if not post: self.error(404) return if cookie_user.key == post.user: self.render("blog.html", post=post, user=cookie_user, comments=list_dict, like_count=no_of_likes, comment_count=comment_count, is_author=True) else: self.render("blog.html", post=post, user=cookie_user, comments=list_dict, like_count=no_of_likes, comment_count=comment_count, is_author=False) else: self.render("blog.html", post=post, user=None, comments=list_dict, like_count=no_of_likes, comment_count=comment_count, is_author=False) else: self.error(404) return
def showCount(post_key): like_obj = Likes.query(Likes.post == post_key).get() if like_obj: return like_obj.like_count else: return "0"