def post(self): post_id = int(self.request.get('post_id')) user_id = int(self.request.get('user_id')) post_username = self.request.get('username') message = '' u = self.current_user() if u: # check if user is not liking own post if u.username != post_username: # check if post was already liked likes = Like.all().filter('post_id =', post_id).filter( 'username ='******'/view?p=%s&u=%s&#likes' % (post_id, user_id)) else: message = 'You can only like a post once' else: message = 'You cannot like your own post' self.redirect('/view?p=%s&u=%s&m=%s&#likes' % (post_id, user_id, message)) else: self.redirect('/login')
def get(self, post_id): if self.good_cookie_exists(): # put the user id and post_id into the Like entity # go to success page, which will have link to home h = self.request.cookies.get('user') curusor = h.split("|")[0] # make sure user doesn't own post post_key = db.Key.from_path( 'Post', int(post_id)) # returning something, but what??? if db.get(post_key) is not None: # if post exists, continue owns = self.user_owns_post(post_key) if owns: # if user owns the post, end self.redirect("/") return else: # otherwise post exists and user is not owner likes = Like.all().filter("post_id = ", str(post_id)).filter( "user = "******"/") # UI blocks multiple liking as well return else: # new Like entity l = Like(post_id=post_id, user=curusor) l.put() # commit entry self.render("successlike.html", loul="liked") else: self.redirect("/") return else: self.redirect("/login") return
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 post(self, post_id): # Prevent users from liking their own posts post = self.secure_interaction(post_id, error_type="like", match_user=False) if post: user_id = self.check_for_user() # Determine if the user has liked the post before like = Like.by_user_post(user_id, str(post.key().id())) # If they have liked the post before remove the like and decrease # the post likes if like: post.likes -= 1 like.delete() post.put() else: # If they have not liked the post before then add a like and # increase the post likes post.likes += 1 l = Like(parent=blog_key(), user_id=user_id, post_id=str(post.key().id())) l.put() post.put() self.redirect('/post/%s' % str(post.key().id()))
def get(self, post_id): # check if logged in and get username. username = self.check_login(self.request.cookies.get('user_id')) if not username: self.redirect('/login') else: # get the post. post = db.get(db.Key.from_path('Post', int(post_id))) # if post belongs to user, show alert. if post.username == username: self.render('alerts.html', alert='You cannot like your own post', path='/%s' % post_id) else: already_like = Like.all().filter('username='******'post_id=', post_id).get() # if user hasn't liked the post, create like. if not already_like: l = Like(username=username, post_id=post_id) l.put() self.render('alerts.html', alert='Like added to post', path='/%s' % post_id) # if user has already liked the post, show alert. else: self.render('alerts.html', alert='You can only like a post once', path='/%s' % post_id)
def post(self, post_id): post = Post.get_by_id(int(post_id)) if post and post.author.key().id() == self.user.key().id(): self.error(404) else: like = Like(post=post, user=self.user) like.put() time.sleep(1) self.redirect('/{}'.format(post_id))
def post(self, post_id): key = db.Key.from_path('Post', int(post_id)) post = self.get_post(key) # check that # post exists, # user is not liking his own post, # user didn't like this post if post and self.user.username != post.author.username and\ not post.liked(self.user): like = Like(user = self.user, post = post) like.put() self.response.out.write(json.dumps({}))
def post(self, post_id): """ handles post request """ action = self.request.get("like-button") user = self.logged_in_user() post = Post.post_by_id(post_id) if action == 'like': like = Like(post=post, user=user) like.put() else: like = user.likes.filter("post =", post).get() like.delete() self.redirect('/' + post_id)
def like_update(cls, post_id, user_id): error_msg = '' post = Post.get_by_id(post_id) current_like = Like.all().filter('post_id =', post_id).filter('user_id =', int(user_id)).get() if user_id != post.created_by: if not current_like: # if there is no record, adding a record with value=True l = Like(user_id=int(user_id), post_id=int(post_id), like_value=True) l.put() return l, error_msg current_like.like_value = not current_like.like_value current_like.put() else: error_msg = 'You cannot like your own post' return current_like, error_msg
def post(self, post_id): key = db.Key.from_path('Post', int(post_id), parent=blog_key()) post = db.get(key) if not post: self.error(404) return c = "" if (self.user): if (self.request.get('like') and self.request.get('like') == "update"): likes = db.GqlQuery("select * from Like where post_id = " + post_id + " and user_id = " + str(self.user.key().id())) if self.user.key().id() == post.user_id: self.redirect("/blog/" + post_id + "?error=You cannot like your own " + "post.!!") return elif likes.count() == 0: l = Like(parent=blog_key(), user_id=self.user.key().id(), post_id=int(post_id)) l.put() if (self.request.get('comment')): c = Comment(parent=blog_key(), user_id=self.user.key().id(), post_id=int(post_id), comment=self.request.get('comment')) c.put() else: self.redirect("/login?error=You need to login before " + "performing edit, like or commenting.!!") return comments = db.GqlQuery("select * from Comment where post_id = " + post_id + "order by created desc") likes = db.GqlQuery("select * from Like where post_id=" + post_id) self.render("permalink.html", post=post, comments=comments, noOfLikes=likes.count(), new=c)
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, action, post_id): if self.validate(action, post_id): params = {} like = Like(post=self.blog_post, liked_by=self.user) key = like.put() # get updated object params['post'] = db.get(key).post self.render("permalink.html", **params)
def like_boker(user_key, boker_key, explicitly_shared=False): """Like a boker""" user = User.get_by_key_name(user_key) boker = Boker.get(boker_key) boker_owner = boker.user if user and boker and not Like.already_like(user, boker): # Create like like = Like(user=user, boker=boker) like.put() # Update like numbers boker.num_like += 1 boker.put() if not settings.DEBUG: # Post to FB try: boker_url = "%s/boker/%s" % (settings.APP_DOMAIN, boker.key().id()) graph = facebook.GraphAPI(user.access_token) graph.request('me/og.likes', post_args={'object': boker_url, 'fb:explicitly_shared': str(explicitly_shared).lower()}) except Exception as e: print e # Notify Boker owner if user != boker_owner: try: boker_url = encode_url("/boker/%s" % boker.key().id()) graph = facebook.GraphAPI(settings.FACEBOOK_APP_ACCESS_TOKEN) graph.request('%s/notifications' % boker_owner.id, post_args={ 'href': '?to=' + boker_url, 'template': '@[%s] menyukai boker anda.' % user.id, }) except Exception as e: print e else: logging.info('Runtask: post_like_story...')
def get(self, post_id): if user_logged_in(self): key = db.Key.from_path('Post', int(post_id), parent=post_key()) post = db.get(key) if not post: return self.error(404) if not user_owns_post(self, post): self.redirect('/') if post.liked(self.user): l = db.GqlQuery( "SELECT * FROM Like WHERE author=:user AND parent_post=:post", user=self.user, post=post).get() l.delete() self.redirect('/blog') else: l = Like(author=self.user, parent_post=post) l.put() self.redirect('/blog') else: return self.redirect('/')
def set_like(self,branch_urlsafe_key,like_value): if branch_urlsafe_key is None or branch_urlsafe_key == '': return False, ['invalid_parameters'] userinfo = self.current_user_info() if userinfo is None: return False, ['unauthenticated'] branch_key = ndb.Key(urlsafe=branch_urlsafe_key) like_key = Like.create_key(branch_key,userinfo.username) like = like_key.get() if like is None: branch = branch_key.get() like = Like(key=like_key,username=userinfo.username,branch=branch_key,branchauthorname=branch.authorname) like.value = int(like_value) like.put() return True, like
def post(self, post_id, post): # If user made post, don't allow them to like it if self.user.key().id() == post.user_key.key().id(): return self.redirect('/post/%s' % str(post.key().id())) # Retrieve likes by the user for the post likes = Like.all() likes.filter("post_key =", post.key()) likes.filter("user_key =", self.user.key()) # Check if user already liked the post liked = likes.count() > 0 # If user hasn't liked, create like else redirect to post page if not liked: like = Like(parent=blog_key(), post_key=post.key(), user_key=self.user.key()) like.put() self.redirect('/post/%s' % str(post.key().id())) else: self.redirect('/post/%s' % str(post.key().id()))
def post(self, username, post_slug): if not self.logged_in(): return self.redirect('/login') value = self.request.get('like') post = Post.find_by_user_and_slug(username, post_slug) same_user = self.user.username == username if same_user or not (value in ['Like', 'Dislike'] or post): return self.redirect('/dashboard') bool_val = True if value == 'Like' else False like_exist = post.find_like_by_user(self.user) if like_exist: if like_exist.value != bool_val: like_exist.value = like_exist.value != True #Swap T/F values if like_exist.put(): post.likes += 1 if bool_val else -1 post.dislikes -= 1 if bool_val else -1 post.put() else: like_exist.key.delete() if bool_val: post.likes -= 1 else: post.dislikes -= 1 post.put() else: like = Like(value=bool_val, parent=post.key, author=self.user.key) if like.put(): if bool_val: post.likes += 1 else: post.dislikes += 1 post.put() self.write(post.render_likes(self.user))
def post(self, post_id, b, err): if self.user: blog_comment = self.request.get("blog-comment") blog_comment = blog_comment.replace('\n', '<br>') like = self.request.get('like') dislike = self.request.get('dislike') comment_edit = self.request.get('edit-comment') #comment_del = self.request.get('delete-comment') # Allows the user to like or dislike the posts only once. # But user can dislike after liking the post and vice-versa. # User cannot like, dislike their own posts. if like or dislike: likes = ndb.gql( " SELECT * FROM Like" " WHERE like_blog = :num1" " AND like_user = :num2", num1=b.key, num2=self.user.key).get() if b.blog_author.get().name != self.user.name: if likes: if likes.like_dislike == 1: if like: post_id += '-1' self.redirect('/edit/%s' % post_id) else: for l in b.blog_likes: if l == likes.key: b.blog_likes.remove(l) b.put() likes.key.delete() post_id += '-0' self.redirect('/edit/%s' % post_id) else: if like: for l in b.blog_likes: if l == likes.key: b.blog_likes.remove(l) b.put() likes.key.delete() post_id += '-0' self.redirect('/edit/%s' % post_id) else: post_id += '-2' self.redirect('/edit/%s' % post_id) else: like_blog = b.key like_user = self.user.key if like: like_dislike = 1 else: like_dislike = 0 likes = Like(like_blog=like_blog, like_user=like_user, like_dislike=like_dislike) likes.put() b.blog_likes.append(likes.key) b.put() post_id += '-0' self.redirect('/edit/%s' % post_id) else: post_id += '-3' self.redirect('/edit/%s' % post_id) # edit a comment elif comment_edit: self.redirect('/edit-comment/%s' % str(comment_edit)) # Post new comments else: if blog_comment: comments = Comment(comment_blog=b.key, user_name=self.user.key, comment_content=blog_comment) comments.put() comments.comment_id = str(comments.key.id()) comments.put() b.blog_comments.append(comments.key) b.put() post_id += '-0' self.redirect('/edit/%s' % post_id) else: post_id += '-0' self.redirect('/edit/%s' % post_id) # Users visting this page, if not logged in, have the option to login. else: self.redirect('/')