def post(self): """ Post method to like and unlike blogs """ if self.user: if self.request.get("like_blog"): _blog_id = self.request.get("like_blog") user = self.user user_id = user.key.id() _user = User.by_id(int(user_id)) _blog = Blog.get_by_id(int(_blog_id)) like = Like(user=user.key, blog=_blog.key) like.like = 1 like_key = like.put() blog = _blog.key.get() blog.likes.append(like_key) blog.put() self.redirect('/') if self.request.get("dislike"): _blog_id = self.request.get("dislike") user = self.user user_id = user.key.id() _user = User.by_id(int(user_id)) _blog = Blog.get_by_id(int(_blog_id)) like = Like.query(Like.user == user.key, Like.blog == _blog.key).get() like_key = like.key blog = _blog.key.get() blog.likes.remove(like_key) blog.put() like_key.delete() self.redirect('/') else: self.redirect('/login')
def get(self): qry = Topic.query().order(Topic.topic) topics = [] for row in qry: likes = Like.query().filter(Like.topic == row.key).count() topics.append(TOPIC(key=row.key, topic=row.topic, description=row.description, archived=row.archived, likes=likes)) self.render_template('home.html', {'topics': topics})
def get(self, post_id): key = ndb.Key('Post', int(post_id), parent=models.blog_key()) post = key.get() if not post: self.error(404) return if not self.user: self.redirect('/login') return like_obj = Like.query(Like.post == post.key).get() if like_obj: authors = like_obj.author for author in authors: if author == self.user.key: like_obj.author.remove(author) flag = True if not flag: self.redirect('/blog/%s' % str(post.key.id())) else: self.write("user doesn't exist") else: self.write("No Like object created")
def get(self, post_id): if not self.user: return self.redirect('/login') key = ndb.Key('Post', int(post_id), parent=models.blog_key()) post = key.get() if not post: self.error(404) return like_obj = Like.query(Like.post == post.key).get() if post.author == self.user.key: self.write("You can not like your own post") else: if like_obj: authors = like_obj.author for author in authors: if (author == self.user.key): return self.redirect("/blog/%s" % str(post.key.id())) like_obj.like_count += 1 authors.append(self.user.key) like_obj.put() self.redirect('/') else: like_obj = Like(post=post.key, like_count=1) like_obj.author.append(self.user.key) like_obj.put() self.redirect('/')
def get(self, topic_id): key = getKey(topic_id) likes = Like.query().filter(Like.topic == key, Like.createdBy == self.user_info['user_id']).fetch(1) if len(likes) == 0: Like(topic=key, createdBy=self.user_info['user_id'], modifiedBy=self.user_info['user_id']).put() else: likes[0].key.delete() self.redirect(self.uri_for('home'))
def get(self, post_id): """ Renders Posts to home page """ key = ndb.Key('Post', int(post_id), parent=models.blog_key()) post = key.get() like_obj = Like.query(Like.post == post.key) if not post: self.error(404) return self.render("permalink.html", post=post)
def get(self): """ Get method to render blog.html """ if self.user: user_key = self.user user_id = user_key.key.id() _user = User.by_id(int(user_id)) _blogs = Blog.query_blogs().fetch() likes = Like.query().fetch() self.render('blog.html', blogs=_blogs, user=_user, likes=likes) else: _blogs = Blog.query_blogs().fetch() self.render('blog.html', blogs=_blogs, user=None, likes=None)